mirror of
https://github.com/nix-community/home-manager.git
synced 2025-11-08 19:46:05 +01:00
home-manager: add basic i18n support
The support for translated strings is, for now, limited to strings generated in Bash code.
This commit is contained in:
parent
3d46c011d2
commit
9bcad20013
13 changed files with 634 additions and 68 deletions
85
lib/bash/home-manager.sh
Normal file
85
lib/bash/home-manager.sh
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
#
|
||||
# This file contains a number of utilities for use by the home-manager tool and
|
||||
# the generated Home Manager activation scripts. No guarantee is made about
|
||||
# backwards or forward compatibility.
|
||||
#
|
||||
|
||||
# Sets up colors suitable for the `errorEcho`, `warnEcho`, and `noteEcho`
|
||||
# functions.
|
||||
#
|
||||
# The check for terminal output and color support is heavily inspired by
|
||||
# https://unix.stackexchange.com/a/10065.
|
||||
#
|
||||
# The setup respects the `NO_COLOR` environment variable.
|
||||
function setupColors() {
|
||||
normalColor=""
|
||||
errorColor=""
|
||||
warnColor=""
|
||||
noteColor=""
|
||||
|
||||
# Enable colors for terminals, and allow opting out.
|
||||
if [[ ! -v NO_COLOR && -t 1 ]]; then
|
||||
# See if it supports colors.
|
||||
local ncolors
|
||||
ncolors=$(tput colors)
|
||||
|
||||
if [[ -n "$ncolors" && "$ncolors" -ge 8 ]]; then
|
||||
normalColor="$(tput sgr0)"
|
||||
errorColor="$(tput bold)$(tput setaf 1)"
|
||||
warnColor="$(tput setaf 3)"
|
||||
noteColor="$(tput bold)$(tput setaf 6)"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
setupColors
|
||||
|
||||
function errorEcho() {
|
||||
echo "${errorColor}$*${normalColor}"
|
||||
}
|
||||
|
||||
function warnEcho() {
|
||||
echo "${warnColor}$*${normalColor}"
|
||||
}
|
||||
|
||||
function noteEcho() {
|
||||
echo "${noteColor}$*${normalColor}"
|
||||
}
|
||||
|
||||
function _i() {
|
||||
local msgid="$1"
|
||||
shift
|
||||
|
||||
# shellcheck disable=2059
|
||||
printf "$(gettext "$msgid")\n" "$@"
|
||||
}
|
||||
|
||||
function _ip() {
|
||||
local msgid="$1"
|
||||
local msgidPlural="$2"
|
||||
local count="$3"
|
||||
shift 3
|
||||
|
||||
# shellcheck disable=2059
|
||||
printf "$(ngettext "$msgid" "$msgidPlural" "$count")\n" "$@"
|
||||
}
|
||||
|
||||
function _iError() {
|
||||
echo -n "${errorColor}"
|
||||
_i "$@"
|
||||
echo -n "${normalColor}"
|
||||
}
|
||||
|
||||
function _iWarn() {
|
||||
echo -n "${warnColor}"
|
||||
_i "$@"
|
||||
echo -n "${normalColor}"
|
||||
}
|
||||
|
||||
function _iNote() {
|
||||
echo -n "${noteColor}"
|
||||
_i "$@"
|
||||
echo -n "${normalColor}"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue