aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2021-06-17 22:22:22 -0300
committerEuAndreh <eu@euandre.org>2021-06-17 22:31:49 -0300
commitbf41672f72ee3ff7679c93e0d5a8f8646c19db3e (patch)
tree33ea422f4bf63f56ae6b33874f3a9a21991bd79d
parentMakefile: Use mkdir+cp over install (diff)
downloadgit-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.
-rwxr-xr-xsrc/git-permalink.sh95
-rwxr-xr-xtests/cli-opts.sh8
2 files changed, 86 insertions, 17 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
diff --git a/tests/cli-opts.sh b/tests/cli-opts.sh
index bbec933..35f2347 100755
--- a/tests/cli-opts.sh
+++ b/tests/cli-opts.sh
@@ -10,7 +10,7 @@ test_help_flags() {
OUT="$(mktemp)"
ERR="$(mktemp)"
- ./git-permalink -h 1>"$OUT" 2>"$ERR"
+ LANG=en_US.UTF-8 ./git-permalink -h 1>"$OUT" 2>"$ERR"
STATUS=$?
assert_status 0
assert_empty_stderr
@@ -19,7 +19,7 @@ test_help_flags() {
OUT="$(mktemp)"
ERR="$(mktemp)"
- ./git-permalink --help 1>"$OUT" 2>"$ERR"
+ LANG=en_US.UTF-8 ./git-permalink --help 1>"$OUT" 2>"$ERR"
STATUS=$?
assert_status 0
assert_empty_stderr
@@ -29,7 +29,7 @@ test_help_flags() {
OUT="$(mktemp)"
ERR="$(mktemp)"
- ./git-permalink --something somethign -h 1>"$OUT" 2>"$ERR"
+ LANG=en_US.UTF-8 ./git-permalink --something somethign -h 1>"$OUT" 2>"$ERR"
STATUS=$?
assert_status 0
assert_empty_stderr
@@ -39,7 +39,7 @@ test_help_flags() {
OUT="$(mktemp)"
ERR="$(mktemp)"
- ./git-permalink --help more things 1>"$OUT" 2>"$ERR"
+ LANG=en_US.UTF-8 ./git-permalink --help more things 1>"$OUT" 2>"$ERR"
STATUS=$?
assert_status 0
assert_empty_stderr