Translating Linux with `gettext`
29 gennaio 2008, 15:54 Location , Shell , Tips & Tricks January 29, 2008, 15:54The function `gettext` is designed to allow the translation of the entire operating system GNU / Linux. Precisely for this reason it is 100% compatible with a large number of programming languages and scripting languages: C, C + +, C #, Bash, Python, GNU clsip, Emacs Lisp, GNU Smalltalk, Java, GNU awk, Pascal, WxWidgets, YCP , Tcl, PHP, Pike, and others.
First let's see how to use `gettext` within the source code. The implementing rules vary depending on the programming language used for your project, but the syntax is almost always the same, at least for popular programming languages: the function simply contains the message string to be translated. It should be noted that almost always uses the short form, which simply corresponds to an underscore. In this way, the following code in C:
printf(gettext("My name is %s\n"), mio_nome);
becomes:
printf(_("My name is %s\n"), mio_nome);
Later, the command xgettext will filter the code to find the function gettext just inserted, to create the template file. Pot on the basis of the extracted strings from source. An example of a file. Pot for a code containing the above lines is as follows:
#: include/myname.php:36
msgid "My name is %s\n"
msgstr ""
To start the translation is used in the program `` msginit. Moving the file. Little `to` msginit is possible to generate a new file containing the skeleton of the new. Bit in the target language. For example, to translate the file into Italian en_EN.po just run the following command:
locale = en_US-msginit-input = en_EN.po
The program will create the file it_IT.po, whose contents will be similar to the following:
#: include/myname.php:36
msgid "My name is %s\n"
msgstr "My name is %s\n"
At this point, the translator does not have to do is translate the string following the msgstr. The translation can be done with any editor directly from the command line, or using specific programs like poEdit . The result will look like the following:
#: include/myname.php:36
msgid "My name is %s\n"
msgstr "Mi chiamo %s\n"
Once the translation files. Po should be compiled into the binary file. Mo through the `msgfmt`:
msgfmt it_IT.po















October 10th, 2008 at 15:48
Hello, I made some time ago servicemenus for kde3.
Now I'm bringing them to kde4 and I wanted to add support to the barbarous languages
In practice, the menu 'is calling a bash script and initially I had entered all the strings already' translated in the same script:
for example
-----------------
en_msg_savefile = "Save file ..."
it_msg_savefile = "Save file ..."
etc. ..
msg_SaveFile eval = \ $ $ (echo $ {lang} _msg_SaveFile)
and then echo "$ {} msg_SaveFile"
----------------
Now I wonder, do you think you can 'easily adopt the procedure you described above to locate the scriptino in bash?
Thanks
October 10th, 2008 at 21:25
I found what I was looking for, thanks.
I would like to ask you one thing, there are some words in English is scivono in a certain way but not in Italian or other languages.
eg: Done
I write in Italian finished or finished, according to the operation carried out previously.
If for example I rotate an image will be 'a window that says "Image rotation casa.jpg" "Completed"
Write in English always "Done"!
You know how I can do to fix this thing?
Thank you in advance.
Many are beginning to use kde4 and I have written several emails that were hoping for a port of the scripts. Except that so far there are 8 languages, but more 'out could increase.
Greetings Goofy
October 13th, 2008 at 10:05 AM
Hello Joseph,
excuse the delay, they are very busy. First answer to your second question: If you choose to use gettext the correspondence between the base language and the various translations must necessarily be unique on all the words and, in the case where the translation language is more articulated than that of departure, is to be bijective, where possible. If you are talking about the biunivocità is impossible, so you just have to translate blocks of phrases rather than single words. Or, as every good translator, you'll just take some small cunning. In your case I would translate "Done" with "... Done" instead of "Completed / to". But I am sure that you have more complex examples where this simple solution is not enough, girameli here that we discuss them together.
Regarding your first question, also know you have found the solution alone, I tell you that using gettext is the most simple and quick and will save to invoke eval and other "tricks"
Find a practical example of very significant at this address:
http://tldp.org/LDP/abs/html/localization.html
But ... I'm sure you've already seen it, then I invite you to publish here a short example of how you've implemented it for the benefit of all
Bye! Ivan
October 13th, 2008 at 10:24
Hello Ivan, thanks for reply.
Give me time to give a good name to the variables and fix some other little thing and then post some more as' complex.
Anyway when I run them on kde-look.org public, as I did with those for kde3 here: http://www.kde-look.org/content/show.php/konq-pdf?content=37321
soon
October 13th, 2008 at 23:55
Speaking of tricks you mentioned ... to maintain a complete translation of all dettaggli a language, just use coma key hash key created by you, and not the translation itself. For example:
in the source file "gettext (msg_saveFile_title)"
and then we create the file en.po, etc. it.po
in this way the message and 'unique and more' allows me to use masculine, feminine or neutral, and other features.
What do you think?
hello
October 15th, 2008 at 12:31
As for the scripts that you publish on kde-look.org, let me know when you do, I'm curious. The second message (using the hash key with gettext) I'm sure it would be virtually impossible to achieve: the languages are very different from one another both morphological and syntactic point of view, the only solution is to produce all the necessary phrases in the source language and generate the corresponding phrases in the languages translated. It is a source of endless redundancies, but believe me, there is no alternative solution using a simple but very effective as gettext. But he can always talk, maybe it's me that I have not caught up in the fullest meaning of your post.
bye
Ivan
October 15th, 2008 at 15:40
Turning a little to the channels of the developers are aware of that would i18nc internationalization contextualised. In practice, it allows you to specify a message based on the context. I have no depth because 'I' was told that it would be better that every software has in sorgento soo en_gb messages in English and then, through lacoalizzazione, other languages. So in the light of this, I can not use the technique mentioned above.
In any case I have also said that you should not use gettext directly, so I can use it to bash.
After this introduction, I decided to do these things with technology "eval", and 'how to use pointers in bash
I just finished today servicemenus and have a sea of functions for working with PDF. I've added some than the previous version. I still miss the translations, so we should 'wait for me riconsegnino various sources by translators translated sentences and I think it will go' a bit of time (there are around 8 languages).
I promised a bit more as a 'complex, then going to take' one of servicemenus I wrote, will cut off 'all functions except one and comment further' summarily only the part relating to internationalization.
Subject:
A servicemenu that extracts the images from a pdf document. (The program that does this is called pdfimages and e 'contained in poppler-utils).
It will serve 'a file. Desktop to put in ~ / .kde/share/kde4/services/ServiceMenus or the equivalent of the system (all users).
Barbatrucco: I noticed you can also create a subfolder, so you have more 'order.
The file we call pdfimages.desktop (arbitrary name) and contains:
When you click on an entry, run kde that there 's in the Exec line [lang], where lang and' the language set for kde itself.
As you see, is called a scriptino sm_pdfimages.sh named and certain parameters are passed: the language, action, the file on which to work.
Let the scriptino, which will contain:
Now let's see what happens when you call the script in question:
first of all I must say that I use the contents of the variable action, directly as a function call
In this way, if savings on houses, etc. ... 

When running a function (eg all), the bash replaces the variables with the value. Analizziamone only one: "$ {} msg_imagesRootName_title"
This variable contains the title of a dialog box, specify the language is however not
The correct sentence is obtained with a little game that has intrigued me at first, brace yourselves! a pointer Elementary (very basic)
First things first, first define the variables of the messages in any language that interests me and I call the variables themselves with a name that contains the string of the language used.
So, for the title:
As you can see the difference of name is only the beginning.
Then define a variable whose name and 'Same as described above but without the "lang" in this way:
msg_imagesRootName_title eval = \ $ $ (echo $ {lang} _msg_imagesRootName_title)If we assume that the language in use is it, with this procedure, the shell tells msg_imagesRootName_title to take the value in $ it_msg_imagesRootName_title and the game and 'done.
In practice, the variable will contain msg_imagesRootName_title 'the correct message based on the language used.
Finished.
After evaluating several alternatives, so this seemed the most 'easy to internationalize the script. Of course if someone has another idea, would be welcome.
Fiuu .. more 'easy to implement it than to explain.
As mentioned above, it will still take some time before publishing, even if I could do it now and translations will be added later.
PS if you want to put the tag for the code to make it more 'readable, go ahead.
Good day to all
Joseph
October 15th, 2008 at 16:13
Casserole dish, this is not a simple comment, but a post with controfiocchi! Compliments
I admit I have not had time to test your code, but the logic is there and the explanation is clear.
You are the first to earn entry in my Blogroll! 
Ivan
October 15th, 2008 at 21:42
Hello Ivan, thanks for the blogroll.
. 
The comment seems long because of the code
I have yet to understand why 'you should not use gettext directly, it seemed very simple and easily upgradeable.
In any case, these service menus are a thing not finished, with little control over the flow of commands.
Now that I put everything in a script I have the chance 'in time to include many more' checks. If you look at the menu I had done, the flow is sequential since each command within the files. Desktop I could not take advantage of if or variables. I actually remember doing somersaults in order not to resort to external files
I hope that the example stimuli posted a bit of people to create context menus. After all, we ourselves that we will improve our kde4, or at least I did with mine and I am fine there.
If I can I try to put this work on kde.org pdf so anyone can work on easily without needing to install anything.
To all:
Tips and suggestions for new functionality are welcome.
I'm not a programmer and you do not even have to be to participate. Despite this many people around the world have thanked me for something I even thought was possible.
I wish everyone good job with their kde4 or whatever else. (Always on open systems).
Joseph
November 24th, 2008 at 1:03
Although with some delay, I published yesterday servicemenu I've worked on.
I placed the ad is on kde-look.org that of http://www.egregorion.net
There 's the tar.gz and deb for Intrepid.
Have fun
Joseph