xref: /netbsd-src/external/gpl2/gettext/dist/gettext-runtime/src/gettext.sh.in (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1#! /bin/sh
2#
3# Copyright (C) 2003, 2005-2006 Free Software Foundation, Inc.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU Library General Public License as published
7# by the Free Software Foundation; either version 2, or (at your option)
8# any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU Library General Public
16# License along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18# USA.
19#
20
21# Find a way to echo strings without interpreting backslash.
22if test "X`(echo '\t') 2>/dev/null`" = 'X\t'; then
23  echo='echo'
24else
25  if test "X`(printf '%s\n' '\t') 2>/dev/null`" = 'X\t'; then
26    echo='printf %s\n'
27  else
28    echo_func () {
29      cat <<EOT
30$*
31EOT
32    }
33    echo='echo_func'
34  fi
35fi
36
37# This script is primarily a shell function library. In order for
38# ". gettext.sh" to find it, we install it in $PREFIX/bin (that is usually
39# contained in $PATH), rather than in some other location such as
40# $PREFIX/share/sh-scripts or $PREFIX/share/gettext. In order to not violate
41# the Filesystem Hierarchy Standard when doing so, this script is executable.
42# Therefore it needs to support the standard --help and --version.
43if test -z "$ZSH_VERSION"; then
44  # zsh is not POSIX compliant: By default, while ". gettext.sh" is executed,
45  # it sets $0 to "gettext.sh", defeating the purpose of this test. But
46  # fortunately we know that when running under zsh, this script is always
47  # being sourced, not executed, because hardly anyone is crazy enough to
48  # install zsh as /bin/sh.
49  case "$0" in
50    gettext.sh | */gettext.sh | *\\gettext.sh)
51      progname=$0
52      package=@PACKAGE@
53      version=@VERSION@
54      # func_usage
55      # outputs to stdout the --help usage message.
56      func_usage ()
57      {
58        echo "GNU gettext shell script function library version $version"
59        echo "Usage: . gettext.sh"
60      }
61      # func_version
62      # outputs to stdout the --version message.
63      func_version ()
64      {
65        echo "$progname (GNU $package) $version"
66        echo "Copyright (C) 2003-2005 Free Software Foundation, Inc.
67This is free software; see the source for copying conditions.  There is NO
68warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
69        echo "Written by" "Bruno Haible"
70      }
71      if test $# = 1; then
72        case "$1" in
73          --help | --hel | --he | --h )
74            func_usage; exit 0 ;;
75          --version | --versio | --versi | --vers | --ver | --ve | --v )
76            func_version; exit 0 ;;
77        esac
78      fi
79      func_usage 1>&2
80      exit 1
81      ;;
82  esac
83fi
84
85# eval_gettext MSGID
86# looks up the translation of MSGID and substitutes shell variables in the
87# result.
88eval_gettext () {
89  gettext "$1" | (export PATH `envsubst --variables "$1"`; envsubst "$1")
90}
91
92# eval_ngettext MSGID MSGID-PLURAL COUNT
93# looks up the translation of MSGID / MSGID-PLURAL for COUNT and substitutes
94# shell variables in the result.
95eval_ngettext () {
96  ngettext "$1" "$2" "$3" | (export PATH `envsubst --variables "$1 $2"`; envsubst "$1 $2")
97}
98
99# Note: This use of envsubst is much safer than using the shell built-in 'eval'
100# would be.
101# 1) The security problem with Chinese translations that happen to use a
102#    character such as \xe0\x60 is avoided.
103# 2) The security problem with malevolent translators who put in command lists
104#    like "$(...)" or "`...`" is avoided.
105# 3) The translations can only refer to shell variables that are already
106#    mentioned in MSGID or MSGID-PLURAL.
107#
108# Note: "export PATH" above is a dummy; this is for the case when
109# `envsubst --variables ...` returns nothing.
110#
111# Note: In eval_ngettext above, "$1 $2" means a string whose variables set is
112# the union of the variables set of "$1" and "$2".
113#
114# Note: The minimal use of backquote above ensures that trailing newlines are
115# not dropped, not from the gettext invocation and not from the value of any
116# shell variable.
117#
118# Note: Field splitting on the `envsubst --variables ...` result is desired,
119# since envsubst outputs the variables, separated by newlines. Pathname
120# wildcard expansion or tilde expansion has no effect here, since the words
121# output by "envsubst --variables ..." consist solely of alphanumeric
122# characters and underscore.
123