Looking for people's experience with po files.

Dan Coutu coutu at snowy-owl.com
Wed Sep 10 12:14:13 EDT 2008


Steven W. Orr wrote:
> I understand the purpose of po files but I've never used it. I'm currently 
> working on a bash script that's ~10k lines long. It needs to support 
> multiple languages and right now we have a set of .lang files that just 
> define duplicate variables. e.g.
>
> Filename		Content
> lang/English		symbol1=Hello
> lang/German		symbol1='Guten Tag'
>
> I'm thinking there's gotta be something better. Are po files inappropriate 
> for this? Anyone?
>   
po files are a part of the gettext solution for handling 
internationalization (i18n).

They can work quite well. Here's a thumbnail overview.

You create  plain text message files with an extension of .po

The content of these looks like this:

msgid "base language message"
msgstr "This is the base language message"

The idea is that the file contains a list of pairs of msgid and msgstr 
values. Typically I use a brief msgid value, which is what you'd use 
within your code for the message lookup, and a full sentence for the 
msgstr value. Note that using this technique makes it easier to actually 
do translations.

You need one po file per language. The files are typically setup in a 
particular language directory structure. Here's the usual setup:

A locale directory that contains one subdirectory per locale. These are 
usually two letter language codes such as en, fr, de, it, ja, zh, es, 
and so forth. Each locale directory then contains a directory name 
LC_MESSAGES, yes in upper case just like that, and within this directory 
goes your po file.

A po file must be pre-processed into an mo file that is binary data. The 
command to do this is:

msgfmt -o messages.mo messages.po

Once you have this then you can use the examples provided by Derek 
within your code.

Note that Derek's use of placeholders like %s, %d, and friends is not a 
part of what gettext handles by default. The trick there is to use 
gettext to obtain the translation and then do string processing (your 
language's equivalent of sprintf) in order to do the variable 
substitution. There are, however,  potential pitfalls with this logic. 
Not all languages place words in the same order. What is one language 
might be "You have 10 items in your shopping cart worth $50 total" might 
in another language read more like "Your total of $50 is for item count 10".

So that's the brief overview of experienced use of gettext for you. I 
hope that it helps you and others.

Dan


More information about the gnhlug-discuss mailing list