gettext.sh 5.1 KB

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