Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # I used this in a context where gettext couldn't be used.
- # I tested it on Bash version 3 and 4 on the following systems:
- # MacOS, Debian, MSYS (Mingw), cygwin.
- # Available languages
- languages=(EN FR)
- # Strings
- msgExample=(
- "This is an example message."
- "Ceci est un message exemple..."
- )
- msgExampleArgs=(
- "Please refer to the document #{0} for details on this problem."
- "Veuillez vous référer au document n°{0} pour des détails concernant ce problème."
- )
- # =========
- # FUNCTIONS
- ## getLanguage()
- # Usage: getLanguage
- # Description:
- # Function that attempts to return the uppercase two (or three)
- # letters language code corresponding to the current system locale. If it
- # fails, it will return its default value (EN).
- function getLanguage() {
- local defaultLang="EN"
- local lang=""
- local systemName="$(uname)"
- case $(uname) in
- Darwin)
- # MacOS
- lang="$(defaults read -g AppleLocale \
- | cut -d '_' -f1 \
- | tr '[:lower:]' '[:upper:]')"
- ;;
- *)
- lang=$(echo "$LANG" | cut -d '_' -f1 | tr '[:lower:]' '[:upper:]')
- ;;
- esac
- # Fallback to our default language if we were unable to get a langauge
- if [[ -z "$lang" ]]; then
- lang="$defaultLang"
- fi
- echo "$lang"
- }
- ## getString()
- # Usage: getString STRING_NAME [ARGS...]
- # Description:
- # Returns the string corresponding to the given STRING_NAME. That name should
- # match the name of the shell variable that contains the strings array.
- #
- # If args are given, they will be used as substitutes to the {x} values in the
- # string. Eg: first arg will replace the {0} value, second one the {1}, etc.
- #
- # The previous declaration of a 'languages' array is expected. It should
- # contain the uppercase two (or three) letters language identifier (such as
- # FR). The strings arrays should be organized using that same order for the
- # translations of a string.
- function getString() {
- # Declare the lang index according to the user language, if it was not
- # done yet.
- if [[ -z "${langIndex+x}" ]]; then
- # Find out user's language and use it to set the strings to be used
- # for the runtime of this script.
- local usrLang="$(getLanguage)"
- # Now get the index that we are going to use for the strings out of
- # the position of our language in the 'languages' array.
- local currentIndex=0
- langIndex=0
- for lang in "${languages[@]}"; do
- if [[ "$usrLang" = "$lang" ]]; then
- langIndex=$currentIndex
- break
- fi
- currentIndex=$(($currentIndex + 1))
- done
- fi
- # Now get the string according to the index we set to be used.
- msgName="${1}[$langIndex]"
- str="${!msgName}"
- shift
- # Substitute its placeholders using our arguments.
- paramNo=0
- while [[ $# -gt 0 ]]; do
- str="$(echo "$str" | sed "s/{${paramNo}}/${1}/g")"
- paramNo=$(($paramNo + 1))
- shift
- done
- echo "$str"
- }
- # =======
- # EXAMPLE
- echo "$(getString msgExample)"
- echo "$(getString msgExampleArgs 54671)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement