Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #!/bin/bash
  2. # I used this in a context where gettext couldn't be used.
  3. # I tested it on Bash version 3 and 4 on the following systems:
  4. # MacOS, Debian, MSYS (Mingw), cygwin.
  5.  
  6. # Available languages
  7. languages=(EN FR)
  8.  
  9. # Strings
  10. msgExample=(
  11. "This is an example message."
  12. "Ceci est un message exemple..."
  13. )
  14.  
  15. msgExampleArgs=(
  16. "Please refer to the document #{0} for details on this problem."
  17. "Veuillez vous référer au document n°{0} pour des détails concernant ce problème."
  18. )
  19.  
  20. # =========
  21. # FUNCTIONS
  22.  
  23. ## getLanguage()
  24. # Usage: getLanguage
  25. # Description:
  26. # Function that attempts to return the uppercase two (or three)
  27. # letters language code corresponding to the current system locale. If it
  28. # fails, it will return its default value (EN).
  29. function getLanguage() {
  30. local defaultLang="EN"
  31.  
  32. local lang=""
  33. local systemName="$(uname)"
  34.  
  35. case $(uname) in
  36. Darwin)
  37. # MacOS
  38. lang="$(defaults read -g AppleLocale \
  39. | cut -d '_' -f1 \
  40. | tr '[:lower:]' '[:upper:]')"
  41. ;;
  42. *)
  43. lang=$(echo "$LANG" | cut -d '_' -f1 | tr '[:lower:]' '[:upper:]')
  44. ;;
  45. esac
  46.  
  47. # Fallback to our default language if we were unable to get a langauge
  48. if [[ -z "$lang" ]]; then
  49. lang="$defaultLang"
  50. fi
  51.  
  52. echo "$lang"
  53. }
  54.  
  55. ## getString()
  56. # Usage: getString STRING_NAME [ARGS...]
  57. # Description:
  58. # Returns the string corresponding to the given STRING_NAME. That name should
  59. # match the name of the shell variable that contains the strings array.
  60. #
  61. # If args are given, they will be used as substitutes to the {x} values in the
  62. # string. Eg: first arg will replace the {0} value, second one the {1}, etc.
  63. #
  64. # The previous declaration of a 'languages' array is expected. It should
  65. # contain the uppercase two (or three) letters language identifier (such as
  66. # FR). The strings arrays should be organized using that same order for the
  67. # translations of a string.
  68. function getString() {
  69. # Declare the lang index according to the user language, if it was not
  70. # done yet.
  71. if [[ -z "${langIndex+x}" ]]; then
  72. # Find out user's language and use it to set the strings to be used
  73. # for the runtime of this script.
  74. local usrLang="$(getLanguage)"
  75.  
  76. # Now get the index that we are going to use for the strings out of
  77. # the position of our language in the 'languages' array.
  78. local currentIndex=0
  79. langIndex=0
  80. for lang in "${languages[@]}"; do
  81. if [[ "$usrLang" = "$lang" ]]; then
  82. langIndex=$currentIndex
  83. break
  84. fi
  85. currentIndex=$(($currentIndex + 1))
  86. done
  87. fi
  88.  
  89. # Now get the string according to the index we set to be used.
  90. msgName="${1}[$langIndex]"
  91. str="${!msgName}"
  92. shift
  93.  
  94. # Substitute its placeholders using our arguments.
  95. paramNo=0
  96. while [[ $# -gt 0 ]]; do
  97. str="$(echo "$str" | sed "s/{${paramNo}}/${1}/g")"
  98. paramNo=$(($paramNo + 1))
  99. shift
  100. done
  101.  
  102. echo "$str"
  103. }
  104.  
  105. # =======
  106. # EXAMPLE
  107.  
  108. echo "$(getString msgExample)"
  109. echo "$(getString msgExampleArgs 54671)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement