Development of multilingual software under Linux (GetText solution)

Source: Internet
Author: User
Tags gettext

A bug has occurred in the recent project. The project is based on an existing mature open source software modification, the new encryption and decryption library for the mature open source software to add encryption and decryption capabilities. Function added after the effect is very good, but the Chinese can not come out, that is, there is no way to adapt to the multilingual environment (hint information in the Chinese operating system is Chinese, English operating system is English). Using the Strace-o Log [command to debug] to locate the language pack that it called during execution, it found that it did not call its own language pack, but instead called the decryption Library's language pack (but the decryption library did not develop any language packs), Causes the language pack to be missing and the English prompt in the code is displayed by default. After looking for the source of the problem (the following article helped me), found that the Add decryption library defines the package macro, and the variable in the GetText run environment as the role of the language pack name, resulting in all the translation environment has become the decryption library translation environment, naturally because the language package can not find the translation failed. (In detail how the package variable is defined in the Add-decryption library: This variable was introduced by the Automake compilation system.) The original intention is that I want to add the--ENABLE-DEBUG option for configure, and the debug macro will be included in Config.h that configure,configure out under this option condition. I include config.h in the API header file for this library, and I can use this macro to determine if I want to compile code for debugging. However, at the same time, Config.h includes many platform-related macros, as well as the package macros mentioned above. that is, the Config.h file can not be used in the third-party library's external header file, it is platform-related, environment-related, and contains many macro definitions will overwrite the use of the library source in the macro definition. so the question is, how do I add the--ENABLE-DEBUG option to configure and influence the compilation of debug code in the source? pending further information )

The previous development has never focused on the problem of too many languages, this time the bug occurred due to lack of knowledge. The following article provides an explanation of the GetText Multi-language solution:

Transfer from http://blog.csdn.net/absurd/article/details/524767

The development of multi-language software is a very difficult thing, each country's character set encoding, currency symbols, date format, number format, text performance are different, GLIBC provides a lot of functions to deal with these things, no longer aoshou described. The thing to do here is to illustrate the use of gettext with a simple example, GetText is a series of tools and library functions that help programmers and translators develop multilingual software.

GetText is not a mysterious thing, if not to find a Win32 under the corresponding words, I think it should be a resource file (. res), it manages the string for you, in the run can automatically load the corresponding language string according to the current language.

This assumes the development of a package called foonly, which has only one source file foonly.c, which is the function of printing "Hello, gettext!" on the screen. In the absence of support for multiple languages, the content of FOONLY.C is as follows:

#include <stdio.h>int main (intChar* argv[]) {    printf ("  Hello, gettext!/n");    return 0 ;}

OK, here's our multi-lingual software development Journey:

To create a pot file, the pot is the initials of the Portable object template, and the PO corresponds to the MO,MO is the initials of the machine object. The former refers to the original string file, which is generally used to modify the translator, the latter is related to the machine, is generally for the program to read. You can create pot files by hand, or you can extract strings from your code by Xgettext. This is produced using Xgettext:

Xgettext-a Foonly.c-o Foonly.pot

After running the command, we find that in the current directory, a file named Foonly.pot is generated, and the file opens and you can see:

# SOME descriptive TITLE. # Copyright (C) year the package'S COPYRIGHT HOLDER# This file isDistributed under the same license asThe package . # First AUTHOR<[email protected]>, year. # #, Fuzzy MsgId""Msgstr"" "project-id-version:package version/n" "pot-creation-date:2005-11-07 20:06+0800/n" "Po-revision-date:year-mo-da ho:mi+zone/n" "last-translator:full NAME <[email protected]>/n" "language-team:language <[email protected]>/n" "mime-version:1.0/n" "Content-type:text/plain; charset=charset/n" "content-transfer-encoding:8bit/n"#: Foonly.c:5MsgId"Hello, gettext!/n."Msgstr""

According to the pot produced in different languages Po file, here we first produce a simplified Chinese po file:

Export lang=zh_cn.gb2312

msginit-l zh_cn.gb2312-i foonly.pot  

After running the command, we find that in the current directory, a file named Zh_cn.po is generated, and the file opens and you can see:

# Chinese Translations forPackage Package . # Copyright (C)2005The package'S COPYRIGHT HOLDER# This file isDistributed under the same license asThe package . # root<[email Protected]>2005. # MsgId""Msgstr"" "project-id-version:package version/n" "pot-creation-date:2005-11-07 20:06+0800/n" "po-revision-date:2005-11-07 20:09+0800/n" "last-translator:root <[email protected]>/n" "Language-team:chinese <[email protected]>/n" "mime-version:1.0/n" "Content-type:text/plain; charset=gb2312/n" "content-transfer-encoding:8bit/n"#: Foonly.c:5MsgId"Hello, gettext!/n."Msgstr""

The corresponding string in Zh_cn.po is Chinese:

# Chinese Translations forPackage Package . # Copyright (C)2005The package'S COPYRIGHT HOLDER# This file isDistributed under the same license asThe package . # root<[email Protected]>2005. # MsgId""Msgstr"" "project-id-version:package version/n" "pot-creation-date:2005-11-07 20:06+0800/n" "po-revision-date:2005-11-07 20:09+0800/n" "last-translator:root <[email protected]>/n" "Language-team:chinese <[email protected]>/n" "mime-version:1.0/n" "Content-type:text/plain; charset=gb2312/n" "content-transfer-encoding:8bit/n"#: Foonly.c:5MsgId"Hello, gettext!/n."Msgstr"Hello, gettext!/n."

Generates MO files based on PO files.

MSGFMT Zh_cn.po-o Zh_cn.mo

After running the command, we found that in the current directory, a file named Zh_cn.mo was generated. It is binary and cannot be opened with a text editor.

Install the MO file into the system:

Cp-f Zh_cn.mo/usr/share/locale/zh_cn/lc_messages/foonly.mo

Modify the program.

#include <stdio.h>#include<locale.h>#include<libintl.h>#define_ (String) GetText (String)#defineLocaledir "/usr/share/locale/"#definePackage "Foonly"intMainintargcChar*argv[]) {setlocale (Lc_all,"");     Bindtextdomain (package, localedir);     Textdomain (package); printf (_ ("Hello, gettext!/n.")); return 0; }

Compile and run:

Gcc-g Foonly.c-o foonly

./foonly

You can see the print on the screen:

Hello, gettext!.

Now let's try it in English:

Export Lang=es_us

./foonly

You can see the print on the screen:

Hello, gettext!.

Adding other languages is also easy, no need to modify the program, just like Chinese, generate an MO file, and install to the corresponding directory in the system. Switching between different languages is just a modification of the current locale.

Development of multilingual software under Linux (GetText solution)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.