Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gettext in bash

How to use gettext in bash script?

I only found this page, but I don't understand it.

Localization

My script is written like this:

 #!/bin/bash
 . lang_file.sh
 echo $LANG_HELLO_WORLD

And lang_file.sh look like that:

 #!/bin/bash
 LANG_HELLO_WORLD="Hello World"

I want to change lang_file.sh to something using gettext, like this:

 #!/bin/bash
 LANG_HELLO_WORLD=`some gettext command to get string in user language`

I want to use the code in Launchpad, so other users can translate it (.po, .pot files)

Sorry for bad English, any suggestions?

like image 243
lauriys Avatar asked Feb 08 '10 12:02

lauriys


4 Answers

You need to preform following steps:

  1. Determine what's your project name, gettext calls it textdomain, you will need it to retrieve the translations for your project. Let's call it "PRJ".
  2. Mark the strings to be translated. Following snippet gives example:

    Let's call it PRJ.sh:

    #!/bin/sh
    alias GETTEXT='gettext "PRJ"'
    
    ## Use GETTEXT to mark the string you want to translate
    HELLO_WORLD=$(GETTEXT "Hello world") 
    
    echo "$HELLO_WORLD"
    
  3. Produce .pot file so translators can work on it.

    Run the following command, it only looks for GETTEXT, the ones you actually want to translate.

    xgettext -o PRJ.pot  -L Shell --keyword --keyword=GETTEXT  PRJ.sh
    
  4. Optional: Generate .po files.

    For each locale you want to cover.

    msginit -i PRJ.pot -l fr.UTF-8
    

    Note that "UTF-8" is sugggested, otherwise msginit may mistakenly choose some obsoleted encoding for you.

  5. Retrieve completed .po files, and convert them to .mo file (the file that machine can read it)

    msgfmt -v  fr.po -o fr.mo
    
  6. Install .mo files. Run:

    sudo install fr.mo /usr/share/locale/fr/LC_MESSAGES/PRJ.mo 
    

    Now you can try the result:

    LANGUAGE=fr  ./PRJ.sh
    

    and you should see French translation for Hello world.

like image 121
Ding-Yi Chen Avatar answered Nov 23 '22 01:11

Ding-Yi Chen


There is a long-lost, never-documented and almost-deprecated builtin solution in bash.

LANG=foo_BAR.utf8
TEXTDOMAIN="test" 
TEXTDOMAINDIR="/usr/share/locale"
echo $"fooMsgid"
# bash --dump-po-strings <scriptfile>
like image 31
Mingyee Zhao Avatar answered Nov 22 '22 23:11

Mingyee Zhao


The gettext translation make use of an editable format *.po to store translation, and a compiled format *.mo for loading.

For info of the files format, reference here: section "3 The Format of PO Files" and "10 Producing Binary MO Files" of https://www.gnu.org/software/gettext/manual/html_node/index.html

Here I focus on how to try the gettext command to get a translation in short.

After you prepared a folder /path/to/your/locale with inside-hierarchy like <lang>/LC_MESSAGES/<textdomain>.mo (where <lang> is e.g. ko_KR for Korean), use the following code in your lang_file.sh:

#!/bin/bash
export LC_ALL=ko_KR.UTF-8   # if LC_ALL not work, you could try also "LANG" and "LANGUAGE"
export TEXTDOMAINDIR=/path/to/your/locale
# export TEXTDOMAIN="<textdomain>"   # <- optional, set this to save the "<textdomain>" argument for `gettext` below
LANG_HELLO_WORLD="$( gettext "<textdomain>" "Your message to translate" )"
like image 23
Johnny Wong Avatar answered Nov 23 '22 01:11

Johnny Wong


What you are looking to do is I think ask the user in the appropriate language? You would probably want the user to pick the language first. The other part of what you are asking is simply just embedding commands like $(get_some_str_func) inside of your variable.

I did not write this code but it might be along the lines of what you are trying to do? I'm not sure, i don't understand fully what you are asking.

    function _configure_locale() { # [profile]
        local profile=${1:-EN}
        case ${profile} in
          DE|DE_DE|de_DE)
              LC_ALL="de_DE.UTF-8"
              LANG="de_DE.UTF-8"
              LANGUAGE="de_DE:de:en_US:en"
              ;;
          EN|EN_US|en|en_US)
              LC_ALL="en_US.UTF-8"
              LANG="en_US.UTF-8"
              LANGUAGE="en_US:en"
              ;;
          *)
              echo "ALERT" "${FUNCNAME}: unknown profile '${profile}'"
              ;;
          esac
          LC_PAPER="de_DE.UTF-8"; # independent from locale
          LESSCHARSET="utf-8";    # independent from locale
          MM_CHARSET="utf-8"      # independent from locale
          echo "locale settings" "${LANG}";
          export LC_ALL LANG LANGUAGE LC_PAPER LESSCHARSET MM_CHARSET
    }
like image 45
Mike Q Avatar answered Nov 23 '22 00:11

Mike Q