diff options
author | EuAndreh <eu@euandre.org> | 2021-06-17 22:22:22 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2021-06-17 22:31:49 -0300 |
commit | bf41672f72ee3ff7679c93e0d5a8f8646c19db3e (patch) | |
tree | 33ea422f4bf63f56ae6b33874f3a9a21991bd79d /src/git-permalink.sh | |
parent | Makefile: Use mkdir+cp over install (diff) | |
download | git-permalink-bf41672f72ee3ff7679c93e0d5a8f8646c19db3e.tar.gz git-permalink-bf41672f72ee3ff7679c93e0d5a8f8646c19db3e.tar.xz |
src/git-permalink.sh: Translate program output to pt, fr and eo
The choice of implementation was very basic: Use strings in the script
itself rather than relying on external tools.
Compared to a compiled C program using, say, gettext.3, an sh script
could also depend on it, but at runtime. An equivalent C code would
require the gettext.3 dependency, but at compile time. After
compilation, the code required for doing the translation is already on
the binary, while an sh script would need gettext.1 to run gettext.1
commands while it executes, such as "gettext -s 'MSG_ABC'".
Bash has a very appealing feature: using $"" does a lookup and
translates the string. In other words, $"" runs gettext for you,
without the script requiring gettext.1, just Bash.
As tempting as it was, I chose not to rely on Bash. I preferred the
cost of an ad-hoc solution over requiring Bash over POSIX sh.
The final solution is simple enough for the git-permalink.1 program,
but wouldn't scale for bigger scripts. Strings are placed together for
translation, and no external tool is used for this.
Due to the way that strings are now given to printf.1, ShellCheck
complains a lot about those strings, alonside saying that the
string translation variables are unused. They actually are used, but
hidden behind an eval.
Overall I'm satisfied with the solution, but I probably won't follow
the same path for the manpages, and will choose something like po4a for
it, instead.
Diffstat (limited to 'src/git-permalink.sh')
-rwxr-xr-x | src/git-permalink.sh | 95 |
1 files changed, 82 insertions, 13 deletions
diff --git a/src/git-permalink.sh b/src/git-permalink.sh index c70ca5e..f83bab3 100755 --- a/src/git-permalink.sh +++ b/src/git-permalink.sh @@ -1,22 +1,90 @@ #!/bin/sh +# shellcheck disable=2034 set -eu +MSG_USAGE_EN='Usage: git permalink [-phV] FILE [LINENO]' +MSG_USAGE_PT='Uso: git permalink [-phV] ARQUIVO [NOLINHA]' +MSG_USAGE_FR='Usage: git permalink [-phV] FICHIER [LINENO]' +MSG_USAGE_EO='Uzmaniero: git permalink [-phV] ARKIVO [LINIONO]' + +MSG_HELP_EN='Options: + -p only print the link, don'"'"'t try to open it + -h, --help show this help message + -V, --version print the version number' +MSG_HELP_PT='Opções: + -p somemente imprime o link, não tenta abrí-lo + -h, --help mostra esta mensagem de ajuda + -V, --version imprime o número de versão' +MSG_HELP_FR='Options: + -p seulement imprimez le lien, n'"'"'essayez pas de l'"'"'ouvrir + -h, --help afficher ce message d'"'"'aide + -V, --version imprime le numeró de version' +MSG_HELP_EO='Ebloj: + -p nur presas la ligon, ne provas malfermi ĝin + -h, --help montras ĉi tiun helpmesaĝon + -V, --version presas la versian numeron' + +MSG_MISSING_FILE_EN="Missing FILE argument." +MSG_MISSING_FILE_PT="Faltando argumento ARQUIVO." +MSG_MISSING_FILE_FR="L'argument FICHIER manque." +MSG_MISSING_FILE_EO="La argumento ARKIVO mankas." + +MSG_UNSUPPORTED_ORIGIN_EN='Unsupported origin: %s. + +Add an template override to use git-permalink (see "man git-permalink.1" for instructions).' + +MSG_UNSUPPORTED_ORIGIN_PT='Origem sem suporte: %s. + +Adicione um modelo de substituição para usar o git-permalink (veja "man git-permalink.1" para mais instruções).' +MSG_UNSUPPORTED_ORIGIN_FR='Origine n'"'"'es pas supporté: %s. + +Ajouter un modèle de remplacement pour utilisér git-permalink (regarde "man git-permalink.1" pour les instructions).' + +MSG_UNSUPPORTED_ORIGIN_EO='Origo ne estas subtenata: %s. + +Aldoni anstataŭan ŝablonon por uzi git-permalink (vidu "man git-permalink.1" por instrukcioj).' + +MSG_OPEN_EN='Opening %s' +MSG_OPEN_PT='Abrindo %s' +MSG_OPEN_FR='Ouverture de %s' +MSG_OPEN_EO='Malfermado de %s' + +set_lang() { + lang="$1" + eval " +MSG_USAGE=\$MSG_USAGE_$lang +MSG_HELP=\$MSG_HELP_$lang +MSG_MISSING_FILE=\$MSG_MISSING_FILE_$lang +MSG_UNSUPPORTED_ORIGIN=\$MSG_UNSUPPORTED_ORIGIN_$lang +MSG_OPEN=\$MSG_OPEN_$lang +" +} + +case "${LANG:-}" in + pt_BR.UTF-8*) + set_lang PT + ;; + fr_FR.UTF-8*) + set_lang FR + ;; + eo.UTF-8*) + set_lang EO + ;; + *) + set_lang EN + ;; +esac + usage() { - printf 'Usage: %s [-phV] FILE [LINENO]\n' "$0" + printf '%s\n' "$MSG_USAGE" } help() { - cat <<EOF - -Options: - -p only print the link, don't try to open it - -h, --help show this help - -V, --version print the version number -EOF + printf '\n%s\n' "$MSG_HELP" } version() { - echo 'git-permalink-@VERSION@ @DATE@' + printf 'git-permalink-@VERSION@ @DATE@\n' } PRINTONLY=false @@ -25,7 +93,7 @@ if [ "${1:-}" = '-p' ]; then shift fi if [ -z "${1:-}" ]; then - printf "Missing \$FILE argument\n\n" >&2 + printf '%s\n\n' "$MSG_MISSING_FILE" >&2 usage >&2 exit 2 fi @@ -121,8 +189,8 @@ guess_permalink() { github ;; *) - printf "Unsupported origin: %s.\n\n" "$ORIGIN" >&2 - printf 'Add an template override to use git-permalink (see "man git-permalink.1" for instructions).\n' >&2 + # shellcheck disable=2059 + printf "$MSG_UNSUPPORTED_ORIGIN\n" "$ORIGIN" >&2 exit 1 ;; esac @@ -133,6 +201,7 @@ LINK="$(guess_permalink)" if [ "$PRINTONLY" = true ]; then echo "$LINK" else - echo "Opening $LINK" >&2 + # shellcheck disable=2059 + printf "$MSG_OPEN\n" "$LINK" >&2 xdg-open "$LINK" fi |