diff options
author | Luke Shumaker <LukeShu@sbcglobal.net> | 2013-09-11 10:18:04 -0400 |
---|---|---|
committer | Luke Shumaker <LukeShu@sbcglobal.net> | 2013-09-11 15:43:36 -0400 |
commit | 4b9f4323abfaa0f4030cc29aed97e0716f9c5852 (patch) | |
tree | 1dca8650a1a5857f180aaa7704b7b7ee3ba77d32 | |
parent | 5bbf15572626010ce6e2bc783d0a980fc24662b2 (diff) |
libremessages: add a few more message routines, and make them gettext-aware.
- Don't set LANG=C in common.sh
- Move TEXTDOMAIN stuff into common.sh; so devtools stuff will use it.
- Add _(): Basically an alias for `gettext`, but falls back if gettext is
not available.
- Add panic(): First showed up in `distcc-tool`, does what it sounds like.
- Add prose(), bullet(), and flag(): they do word wrapping and such to
make it easy to internationalize `--help` text.
- Teach common.mk how to make .pot files based on these routines.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | common.mk | 24 | ||||
-rw-r--r-- | src/lib/Makefile | 11 | ||||
-rw-r--r-- | src/lib/common.sh.bottom | 1 | ||||
-rw-r--r-- | src/lib/common.sh.top | 12 | ||||
-rwxr-xr-x | src/lib/libremessages | 78 |
6 files changed, 112 insertions, 15 deletions
@@ -1,5 +1,6 @@ *~ #* +*.pot *.1 *.2 *.3 @@ -17,6 +17,27 @@ indent = emacs --batch $1 \ --eval '(indent-region (point-min) (point-max) nil)' \ -f save-buffer &>/dev/null +# Usage <INPUT $(pofmt) >OUTPUT +# Normalize a .po(t) file +pofmt = msguniq -Fi + +pot-remove-wrap = tr '\n' '\r' | sed 's/"\r\s*"//g' | tr '\r' '\n' +html-whitespace-collapse = sed -r -e 's/(\\n|\\t|\t)/ /g' -e 's/ +/ /g' +define xgettext-prose = + xgettext --omit-header --from-code=UTF-8 -L shell \ + -k \ + --keyword={prose,bullet,flag:2} \ + -o - $^ | $(pot-remove-wrap) | $(html-whitespace-collapse) | sed '/^#, sh-format/d' +endef +define xgettext-main = + xgettext --omit-header --from-code=UTF-8 -L shell \ + -k \ + --keyword={_,print} \ + --keyword={msg,msg2,warning,error,stat_busy} \ + --keyword={lock,slock}:3 \ + -o - $^ +endef + # Usage: as a variable # relative path to `/` from $(bindir) # used for symlinking libexec files @@ -69,6 +90,9 @@ build: PHONY $(build_files) %.html: %.ronn ronn --html $(RONNFLAGS) < '$<' > '$@' +%.pot: % + { $(xgettext-main); $(xgettext-prose); } | $(pofmt) > '$@' + # Install ###################################################################### install: PHONY $(install_files) diff --git a/src/lib/Makefile b/src/lib/Makefile index d7b4049..255bc05 100644 --- a/src/lib/Makefile +++ b/src/lib/Makefile @@ -10,8 +10,9 @@ include ../../common.mk common.sh: %: %.in %.top Makefile @echo "GEN $@" @{ \ - cat "$*.top" && \ - echo 'if [[ -z $${_INCLUDE_COMMON_SH:-} ]]; then' && \ - echo '_INCLUDE_COMMON_SH=true' && \ - cat "$*.in" && \ - echo 'fi'; } > "$@" + cat '$*.top' && \ + echo && \ + sed -r -e '/encoding problem/d;/LANG=/d' -e 's/mesg=\$$(.)/mesg="$$(_ "$$\1")"/' '$*.in' && \ + echo && \ + cat '$*.bottom' && \ + :; } > '$@' diff --git a/src/lib/common.sh.bottom b/src/lib/common.sh.bottom new file mode 100644 index 0000000..e133fad --- /dev/null +++ b/src/lib/common.sh.bottom @@ -0,0 +1 @@ +fi diff --git a/src/lib/common.sh.top b/src/lib/common.sh.top index d59268d..c335956 100644 --- a/src/lib/common.sh.top +++ b/src/lib/common.sh.top @@ -11,3 +11,15 @@ # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. + +if [[ -z ${_INCLUDE_COMMON_SH:-} ]]; then +_INCLUDE_COMMON_SH=true + +export TEXTDOMAIN='libretools' +export TEXTDOMAINDIR='/usr/share/locale' + +if type gettext &>/dev/null; then + _() { gettext "$@"; } +else + _() { echo "$@"; } +fi diff --git a/src/lib/libremessages b/src/lib/libremessages index c9b6b83..46a45fb 100755 --- a/src/lib/libremessages +++ b/src/lib/libremessages @@ -30,27 +30,85 @@ . $(librelib common.sh) ################################################################################ -# gettext initialization # -################################################################################ - -export TEXTDOMAIN='libretools' -export TEXTDOMAINDIR='/usr/share/locale' - -################################################################################ # Own functions # ################################################################################ -# Usage: print fmt arg1 arg2... +panic() { + echo "$(_ 'panic: malformed call to internal function')" >&2 + exit 1 +} + +# Usage: print MESG ARG1 ARG2... # Like printf, but gettext-aware, and prints a trailing newline print() { - local fmt=$1 + [[ $# -ge 1 ]] || panic + local mesg="$(_ "$1")" shift - printf -- "$(gettext "$fmt")\n" "$@" + printf -- "$mesg\n" "$@" +} + +# Do HTML-style whitespace collapsing on standard IO. It considers newline, +# tab, and space to be whitespace. +_html_whitespace_collapse() { + [[ $# == 0 ]] || panic + tr '\n' ' ' | sed -r -e 's/\t/ /g' -e 's/ +/ /g' +} + + +# Usage: prose MESG +# Do HTML-style whitespace collapsing on the first argument, translate it +# (gettext), then word-wrap it to 75 columns. +# This is useful for printing a paragraph of prose in --help text. +prose() { + [[ $# -ge 1 ]] || panic + local mesg="$(_ "$(_html_whitespace_collapse <<<"$1")")"; shift + printf -- "$mesg" "$@" | fmt -u +} + +# Usage: bullet MESG +# Like prose, but print a bullet "-" before the first line, and indent the +# remaining lines. +bullet() { + [[ $# -ge 1 ]] || panic + local mesg="$(_ "$(_html_whitespace_collapse <<<"$1")")"; shift + # Wrap the text to 71 columns; 75 (the default) minus a 4 column indent + printf -- "$mesg" "$@" | fmt -u -w 71 | sed -e '1s/^/ - /' -e '2,$s/^/ /' +} + +# Usage: flag FLAG DESCRIPTION +# Print a flag and description formatted for --help text. +# ex: flag '-C <FILE>' 'Use this file instead of pacman.conf' +# The description is fed through gettext, the flag is not, so if part of the +# flag needs to be translated, you must do that yourself: +# ex: flag "-C <$(_ FILE)>" 'Use this file instead of pacman.conf' +# If you want to line-break the description in the source, so it isn't +# crazy-long, feel free, it is reflowed/wrapped the same way as prose and +# bullet. +flag() { + [[ $# == 2 ]] || panic + local n=' +' + local flag=$1 + local desc="$(_ "$(_html_whitespace_collapse <<<"$2")")" + + declare -i indent=13 + while [[ $indent -le ${#flag} ]]; do + indent=$((indent+8)) + done + + local lines + IFS=$n lines=($(fmt -u -w $((73-indent)) <<<"$desc")) + local line + for line in "${lines[@]}"; do + printf " %-${indent}s %s\n" "$flag" "$line" + flag='' + done } # Usage: term_title This will be the term title # Sets the terminal title term_title() { + [[ $# -ge 1 ]] || panic local fmt='' case "$TERM" in screen|tmux) fmt='\ek%s\e\\';; |