Forums

Using gettext in script

Auteur Réponses
marco_g Mardi 18 Mai 2010 à 8:26
marco_gAnonymous

Hi,

Although POL does use gettext for its translations and some scripts even support two languages, this does not scale well. Has anyone considered using gettext for bash scripts? What are the objections?

There are some documents on using gettext in bash, for instance:

http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/localization.html

Using this makes it easier for people to translate scripts to their native language. However, from what I have seen there are security risks, it is possible to inject code from a translation. However, if translations are checked for code this should not be any problem. Unfortunately I am not an expert (yet :-)), even though I am dutch, my computer is in english ;-).

--
Marco
Quentin PÂRIS Jeudi 20 Mai 2010 à 19:26
Quentin PÂRIS
Admin

The problem with gettext, is that you need to build po file, and the scripts are simple text file
marco_g Jeudi 20 Mai 2010 à 21:04
marco_gAnonymous

The problem with gettext, is that you need to build po file, and the scripts are simple text file

Quote from Tinou


What do you mean? It is possible to use gettext in bash scripts and build po files. It is explained here:

http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/localization.html

This is an example from this website:

bash$ bash --dump-po-strings localized.sh
#: a:6
 msgid "Can't cd to %s."
 msgstr ""
 #: a:7
 msgid "Enter the value: "
 msgstr ""
Quentin PÂRIS Vendredi 21 Mai 2010 à 19:19
Quentin PÂRIS
Admin

Yes, but the po file need to be separated from the script
marco_g Vendredi 21 Mai 2010 à 20:45
marco_gAnonymous

Yes, but the po file need to be separated from the script

Quote from Tinou


Yes, that is the idea of a .po file. Is it a problem for you to have multiple files?
Quentin PÂRIS Vendredi 21 Mai 2010 à 22:44
Quentin PÂRIS
Admin

Yes because script are one-text file stored in a database
marco_g Vendredi 21 Mai 2010 à 23:25
marco_gAnonymous

Yes because script are one-text file stored in a database

Quote from Tinou


Is it possible to change this at some time?

You might even consider distributing the .po files separately with POL. That way, the user has access to the scripts immediatly, but translations can be included when a new POL is released.

If you are interested at some point in time, here is a proof of concept. As you can see it is easy to integrate, if there is a way to distribute the .po files.

[ "$PLAYONLINUX" = "" ] && exit 0
source "$PLAYONLINUX/lib/sources"

export TEXTDOMAIN=Test.sh
export TEXTDOMAINDIR="$REPERTOIRE/locale"

. gettext.sh

POL_SetupWindow_Init

POL_SetupWindow_message $"Hello, World!"

POL_SetupWindow_Close
exit


Here is some code to create the .po file, etc for testing. Notice it is for dutch, which is easy to change.

#!/bin/bash

# Extract the strings
bash --dump-po-strings Test.sh  > Test.sh.po
# It is also possible to dump strings using xgettext

# Replace the test by some dutch text
sed 's/msgstr ""/msgstr "Hallo, Wereld!"/' < Test.sh.po >Test_nl.sh.po

# Install the translation
mkdir ~/.PlayOnLinux/locale
mkdir ~/.PlayOnLinux/locale/nl
mkdir ~/.PlayOnLinux/locale/nl/LC_MESSAGES
msgfmt -o ~/.PlayOnLinux/locale/nl/LC_MESSAGES/Test.sh.mo Test_nl.sh.po

Quentin PÂRIS Samedi 22 Mai 2010 à 20:34
Quentin PÂRIS
Admin

I know, i'm using po file for all fixed part of POL (even in bash scripts)

But if I separate .po file from scripts, users will not be able to translate their scripts anymore