Simple solution for building a translation function:
f() {
eval 'local msg=$"'"${1//[\"\$\`]}"\"
shift
printf "${msg}" "$@"
}
Test:
TEXTDOMAIN=coreutils
LANG="fr_CH.utf8"
f system boot
démarrage système
f $'Written by %s, %s, %s,\nand %s.\n' Athos Portos Aramis Shreck
Écrit par Athos, Portos, Aramis
et Shreck.
But as I prefer setting variables instead of forking function:
f() {
eval 'local msg=$"'"${1//[\"\$\`]}"\"
local -n variable=$2
shift 2
printf -v variable "$msg" "$@"
}
Then
f $'Written by %s, %s, %s,\nand %s.\n' string Huey Dewey Louie Batman
echo ${string@Q}
$'Écrit par Huey, Dewey, Louie\net Batman.\n'
echo "$string"
Écrit par Huey, Dewey, Louie
et Batman.
Or even better as a full translation function:
f() {
local store=false OPTIND OPTARG OPTERR varname
while getopts 'd:v:' opt ;do
case $opt in
d ) local TEXTDOMAIN=$OPTARG ;;
v ) varname=$OPTARG ;;
esac
done
shift $((OPTIND-1))
eval 'local msg=$"'"${1//[\"\$\`]}"\"
shift
printf ${varname+-v} $varname "$msg" "$@"
}
Then
f -d libc -v string "Permission denied"
echo $string
Permission non accordée
f -d coreutils $'Written by %s, %s, %s,\nand %s.\n' Riri Fifi Loulou Georges
Écrit par Riri, Fifi, Loulou
et Georges.
Old answer (Jan 2013)
Well, there is my self answer:
This seem not well implemented now. Work in many situations, but, while
echo "$(gettext 'missing character class name `[::]'\')"
caractère de nom de classe « [::] » manquant
work simply, the same string seem impossible to translate using this bashism:
echo $"missing character class name `[::]'"
>
the console stay locked (waiting for such an end of string) adding ``" ` would immerse bash in a complex interpretation process :->>
> `"
bash: command substitution: line 1: Caractère de fin de fichier (EOF) prématuré lors de la recherche du « ' » correspondant
bash: command substitution: line 2: Erreur de syntaxe : fin de fichier prématurée
missing character class name
And, of course:
echo $"missing character class name \`[::]'"
missing character class name `[::]'
make no translation. :-p
While translating this string containing two backticks work finely:
echo $"%s}: integer required between `{' and `}'"
%s} : entier requis entre « { » et « } »
There is a script where you may see some of mine unsuccessfull trys.
#!/bin/bash
echo "Localized tests"
export TEXTDOMAIN=coreutils
export LANG=fr_CH.UTF-8
export WRITTERS=(Athos Portos Aramis Dartagnan\ Le\ Beau)
echo '#First method# whitout eval'
declare -A MyMessages;
MyMessages[sysReboot]=$"system boot"
MyMessages[writtenBy]=$"Written by %s, %s, %s,
and %s.
"
MyMessages[intReq]=$"%s}: integer required between `{' and `}'"
MyMessages[trClass]=$"when translating, the only character classes that may appear in
string2 are `upper' and `lower'"
# MyMessages[missClass]=$"missing character class name `[::]'"
for msgIdx in ${!MyMessages[@]} ;do
printf "\n--- Test chain '%s' ---\n" $msgIdx
case $msgIdx in
writ* )
printf "${MyMessages[$msgIdx]}\n" "${WRITTERS[@]}"
;;
intReq )
printf "ARRAY{${MyMessages[$msgIdx]}\n" NaN
;;
* )
printf "${MyMessages[$msgIdx]}\n"
;;
esac
done
echo $'###\n#Second method# whith limited eval'
unset MyMessages;
declare -A MyMessages;
lPrintf() {
local sFormat="$(
eval 'echo $"'"${1}"'"'.
)"
shift
printf "${sFormat%.}" "$@"
}
MyMessages[sysReboot]="system boot"
MyMessages[writtenBy]=$'Written by %s, %s, %s,\nand %s.\n'
MyMessages[intReq]="%s}: integer required between \`{' and \`}'"
MyMessages[trClass]="when translating, the only character classes that "
MyMessages[trClass]+=$'may appear in\nstring2 '
MyMessages[trClass]+="are \`upper' and \`lower'"
MyMessages[missClass]="missing character class name \`[::]'"
for msgIdx in ${!MyMessages[@]} ;do
printf "\n--- Test chain '%s' ---\n" $msgIdx
case $msgIdx in
writ* )
lPrintf "${MyMessages[$msgIdx]}" "${WRITTERS[@]}"
;;
intReq )
lPrintf "${MyMessages[$msgIdx]}" NaN
;;
* )
lPrintf "${MyMessages[$msgIdx]}"
;;
esac
done
and his output:
Localized tests
#First method# whitout eval
--- Test chain 'trClass' ---
à la traduction, les seules classes de caractères qui peuvent apparaître
dans string2 sont « upper » ou « lower »
--- Test chain 'intReq' ---
ARRAY{NaN} : entier requis entre « { » et « } »
--- Test chain 'sysReboot' ---
démarrage système
--- Test chain 'writtenBy' ---
Écrit par Athos, Portos, Aramis,
et Dartagnan Le Beau.
###
#Second method# whith limited eval
--- Test chain 'trClass' ---
à la traduction, les seules classes de caractères qui peuvent apparaître
dans string2 sont « upper » ou « lower »
--- Test chain 'missClass' ---
./localized.sh: eval: line 44: Caractère de fin de fichier (EOF) prématuré lors de la recherche du « ` » correspondant
./localized.sh: eval: line 45: Erreur de syntaxe : fin de fichier prématurée
--- Test chain 'intReq' ---
NaN} : entier requis entre « { » et « } »
--- Test chain 'sysReboot' ---
démarrage système
--- Test chain 'writtenBy' ---
Écrit par Athos, Portos, Aramis,
et Dartagnan Le Beau.
If anyone could help my to remove comments and/or error message in this script!? ... (in less then 8 hours?!)
At all, thanks to everyone. (My bounty will go to @gniourf_gniourf unless best answer in 8 hours. But thanks to @techno too, I like your lPrintf! )
\n
, tryecho -e
. It is still not very clear what exactly is being asked in other parts of the question. Why are you usingecho
at all? What's wrong withprintf $"message key" $var1 $var2
? – Designing$"$msg"
will work fine only if$msg
don't contain a\n
. If so, need to write ugly thing likeeval... \$\""$msg"\"
... – Grantgranta$"..."
! Take a look atman gettext
andman bash
about$"
(not$''
). – Grantgrantait's obscure
, there are no more than 5 lines in manpage and approx 10 in the Bash Reference Manual! – Grantgrantadpkg-reconfigure locales
. – GrantgrantaLANG=ru_RU ; echo $"..."
! – Grantgranta