Using gettext in bash

How to use gettext in bash script?

I just found this page, but I do not understand it.

Localization

My script is written as follows:

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

And lang_file.sh looks like this:

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

I want to change lang_file.sh to something using gettext, for example:

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

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

Sorry for the bad english, any suggestions?

+4
source share
3 answers

You need to follow these steps:

  • Determine that the name of your project, gettext calls it textdomain, you will need it to get the translation for your project. Let me call him "PRJ".
  • Mark the lines you want to translate. The following snippet gives an 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" 
  1. Create a .pot file so that translators can work on it.

Run the following command, it only searches for GETTEXT, the ones you really want to translate.

 xgettext -o PRJ.pot -L Shell --keyword --keyword=GETTEXT PRJ.sh 
  1. (Optional) Create .po files.

For each locale you want to cover.

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

Please note that "UTF-8" is requested, otherwise msginit may mistakenly choose outdated encoding for you.

  1. Get completed .po files and convert them to .mo file

(file that the machine can read)

 msgfmt -v fr.po -o fr.mo 
  1. 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 the French translation for Hello world.

+2
source

In bash, there is a long-lost, never-documented, and almost deprecated, built-in solution.

 LANG=foo_BAR.utf8 TEXTDOMAIN="test" TEXTDOMAINDIR="/usr/share/locale" echo $"fooMsgid" # bash --dump-op-strings <scriptfile> 
+1
source

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

I did not write this code, but it may be the way you are trying to do? I'm not sure, I donโ€™t understand 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 } 
0
source

Source: https://habr.com/ru/post/1300621/


All Articles