Score:3

Internationalisation of bash scripts

jp flag

I want to get the language locale so that I can call the relevant help function for the specific language. I can use locale to get the LANG or LANGUAGE variable. But need some assistance to implement the check with a conditional for a specific language.

locale
LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=en_GB.UTF-8
LC_TIME=en_GB.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=en_GB.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=en_GB.UTF-8
LC_NAME=en_GB.UTF-8
LC_ADDRESS=en_GB.UTF-8
LC_TELEPHONE=en_GB.UTF-8
LC_MEASUREMENT=en_GB.UTF-8
LC_IDENTIFICATION=en_GB.UTF-8
LC_ALL=
sudodus avatar
jp flag
What kind of check? What specific language? Should many or all possible languages branch off to their own help functions? I guess you need either a `case` statement of `if` statement.
jp flag
For now I will have help functions in english, french and italian: `help_en`, `help_fr`, `help_it`. Could simply do `comlang="$LANG"`. Would need to match `en`, `fr`, `it` in `comlang` with `case` then.
bac0n avatar
cn flag
I think it closed prematurely.
jp flag
It is something that happens quite often. The curse of moderation and the eagerness to interfere !
bac0n avatar
cn flag
Related: [How to add localization support](http://mywiki.wooledge.org/BashFAQ/098) or you may use `gettext`.
Score:6
jp flag

The following script could be part of your bash shellscript.

#!/bin/bash

function help_fr () {
    echo "Francais"
}
#
function help_it () {
    echo "Italiano"
}
#
function help_en () {
    echo "English"
}

# main

curlang="${LANG:0:2}"

case "$curlang" in
 fr)
  help_fr
  ;;
 it)
  help_it
  ;;
 *) # assuming default English
  help_en
  ;;
esac
jp flag
Many Thanks @sudodus
sudodus avatar
jp flag
@Fatipati, I'm glad I could help you. You are welcome :-)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.