Develop multi-language software in Linux: HelloGetText!

Source: Internet
Author: User
Develop multi-language software in Linux: HelloGetText! -- Linux general technology-Linux programming and kernel information. The following is a detailed description. Contact information of the author: Li xianjing

Developing multi-language software is very difficult. The encoding methods, currency symbols, date formats, numeric formats, and text performances of character sets vary from country to country, glibc provides a large number of functions to handle these tasks, so we will not explain them any more. What we need to do here is to use a simple example to describe the usage of GetText. GetText is a series of tools and library functions that help programmers and translators develop multilingual software.

GetText is not a mysterious thing. If you do not want to find a under Win32, I think it should be a resource file (. it manages the strings for you and automatically loads the strings of the corresponding language at runtime according to the current language.

Here, we suppose we want to develop a software package named foonly, which has only one source file foonly. c. Its function is to print "Hello, GetText!" on the screen !". If multiple languages are not supported, the content of foonly. c is as follows:

# Include

Int main (int argc, char * argv [])

{

Printf ("Hello, GetText! \ N ");

Return 0;
}

Now let's start our multi-language software development journey:

Create a pot file. pot is the abbreviation of Portable Object Template. po corresponds to mo and mo is the abbreviation of Machine Object. The former refers to the original string file, which is generally used for modification by the translator. The latter is machine-related and generally for the program to read. You can create a pot file manually or use xgettext to extract strings from the code. Xgettext is used here:

Xgettext-a foonly. c-o foonly. pot

After running this command, we found that a file named foonly. pot is generated in the current directory. Open this file and we can see that:

# Some descriptive title.

# Copyright (C) year the package's COPYRIGHT HOLDER

# This file is distributed under the same license as the PACKAGE package.

# FIRST AUTHOR , YEAR.

#

#, Fuzzy


Msgid ""

Msgstr ""

"Project-Id-Version: package version \ n"

"POT-Creation-Date: + 0800 \ n"

"PO-Revision-Date: YEAR-MO-DA HO: MI + ZONE \ n"

"Last-Translator: FULL NAME \ N"

"Language-Team: LANGUAGE \ N"

& Quot; MIME-Version: 1.0 \ n & quot"

"Content-Type: text/plain; charset = CHARSET \ n"

"Content-Transfer-Encoding: 8bit \ n"

#: Foonly. c: 5


Msgid "Hello, GetText! \ N"

Msgstr ""

Po files in different languages are generated based on the pot. Here we first generate a po file in simplified Chinese:

Export LANG = zh_CN.gb2312

Msginit-l zh_CN.gb2312-I foonly. pot


After running this command, we found that a file named zh_CN.po is generated in the current directory. Open this file and we can see that:

# Chinese translations for PACKAGE package.

# Copyright (C) 2005 the package's COPYRIGHT HOLDER

# This file is distributed under the same license as the PACKAGE package.

# Root , 2005.

#

Msgid ""

Msgstr ""

"Project-Id-Version: package version \ n"

"POT-Creation-Date: + 0800 \ n"

"PO-Revision-Date: + 0800 \ n"

"Last-Translator: root \ N"

"Language-Team: Chinese \ N"

& Quot; MIME-Version: 1.0 \ n & quot"

"Content-Type: text/plain; charset = GB2312 \ n"

"Content-Transfer-Encoding: 8bit \ n"

#: Foonly. c: 5

Msgid "Hello, GetText! \ N"

Msgstr ""

Translate the string corresponding to zh_CN.po into Chinese:

# Chinese translations for PACKAGE package.

# Copyright (C) 2005 the package's COPYRIGHT HOLDER

# This file is distributed under the same license as the PACKAGE package.

# Root , 2005.

#

Msgid ""

Msgstr ""

"Project-Id-Version: package version \ n"

"POT-Creation-Date: + 0800 \ n"

"PO-Revision-Date: + 0800 \ n"

"Last-Translator: root \ N"

"Language-Team: Chinese \ N"

& Quot; MIME-Version: 1.0 \ n & quot"

"Content-Type: text/plain; charset = GB2312 \ n"

"Content-Transfer-Encoding: 8bit \ n"

#: Foonly. c: 5


Msgid "Hello, GetText! \ N"

Msgstr "Hello, GetText! \ N"

Generate a mo File Based on the po file.

Msgfmt zh_CN.po-o zh_CN.mo



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

Install the mo file to the system:

Cp-f zh_CN.mo/usr/share/locale/zh_CN/LC_MESSAGES/foonly.mo



Modify the program.

# Include

# Include

# Include

# Define _ (String) gettext (String)

# Define LOCALEDIR "/usr/share/locale /"

# Define PACKAGE "foonly"

Int main (int argc, char * 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 that the screen is printed:

Hello, GetText!

Now let's try it in English:

Export LANG = es_US

./Foonly

You can see that the screen is printed:

Hello, GetText!

It is easy to add other languages. without modifying the program, you only need to generate a mo file and install it to the corresponding directory in the system just like treating Chinese. To switch between different languages, you only need to modify the current locale.
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.