A small example of how to implement L10N using gettext

Source: Internet
Author: User

This is an example of an English document. I will share the article translated here with you. I did not translate words by word. I added some of my personal saliva and some nonsense. Haha

This is an example from Chapter10 in Free/Open Source Software Guide to Localization. It is simple and clear. I tried it on RedHat5.3 and finally understood how to use gettext, although it is quite simple, haha, It is of extraordinary significance to me! The younger brother has been thinking about the whole program that can be localized internationally. He does not know how to start with it. "Free/Open Source Software Guide to Localization" gives me considerable guidance, we recommend that you read O (∩ _ ∩) O ~


Don't talk nonsense, go directly to the Code, helloworldintl. c:

650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/>#include <libintl. h>
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#include <locale. h>
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#include <stdio. h>
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
# Define _ (String) gettext (String)
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> # define _ t (String1, String2, n) ngettext (String1, String2, n)
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
Int main ()
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> {
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> int I;
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> setlocale (LC_ALL ,"");
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> bindtextdomain (" helloworldintl ", "/usr/share/locale ");
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> textdomain (" helloworldintl ");
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> printf (_ (" again "));
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> printf (" \ n ");
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> printf (_ (" Hello World "));
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> printf (" \ n ");
650) this. length = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> printf (_ (" These text demonstrate that translation using po files is easier \ n "));
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> scanf (" % d ", & I );
650) this. length = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> printf (_ t (" This number % d is of singular form", "This number % d is of plural form", I), I );
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> printf (" \ n ");
650) this. length = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> printf (_ (" The magic number is % d \ n "), I );
650) this. length = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> printf (_ (" These are additional words "));
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> printf (" \ n ");
650) this. length = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> printf (_ (" I love translation \ n "));
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/>}// main


Step 1:
Run the following command to extract strings in the program:
Xgettext-d helloworldintl-o helloworldintl. pot -- keyword = _ t: 1, 2-k _-s helloworldintl. c
Note that the-k parameter is followed by an underscore, indicating that the following underline needs to be extracted to reference the relevant string of the gettext function. I got it right several times!

Step 2:
Then use the Unicode-supported text editor on Linux. For example, open helloworldintl. pot in gedit and check the following content:

650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> # some descriptive title.
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> # Copyright (C) year the package's COPYRIGHT HOLDER
650) this. length = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> # This file is distributed under the same license as the PACKAGE package.
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/># first author <EMAIL @ ADDRESS>, YEAR.
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> #
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> #, fuzzy
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgid ""
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr ""
"Project-Id-Version: Brant I18N Hello World 1.0 \ n"
"Report-Msgid-Bugs-To: \ n"
"POT-Creation-Date: + 0800 \ n"
"PO-Revision-Date: + 0800 \ n"
& Quot; Last-Translator: BrantC & quot; BrantC@xxx.com & gt; \ n & quot"
"Language-Team: BrantC <BrantC@xxx.com> \ n"
& Quot; MIME-Version: 1.0 \ n & quot"
"Content-Type: text/plain; charset = UTF-8 \ n"
"Content-Transfer-Encoding: 8bit \ n"
"Plural-Forms: nplurals = 2; plural = n! = 11; \ n"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 17
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgid" Hello World"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr ""
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 26
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgid" I love translation \ n"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr" \ n"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 23
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgid" The magic number is % d \ n"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr" % d \ n"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 24
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgid" These are additional words"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr ""
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 19
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. length = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgid" These text demonstrate that translation using po files is easier \ n"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr" po \ n"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 21
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgid" This number % d is of singular form"
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgid_plural" This number % d is of plural form"
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgstr [0]" % d"
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgstr [1]" % d"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 14
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgid" again"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr ""


Step 3:
Copy the pot file as a po file, that is, helloworldintl. po file. If this type of po file is to be sent to the LS team, they will update the string translation to the po file. The translated po file is as follows:

650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> # some descriptive title.
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> # Copyright (C) year the package's COPYRIGHT HOLDER
650) this. length = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> # This file is distributed under the same license as the PACKAGE package.
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/># first author <EMAIL @ ADDRESS>, YEAR.
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> #
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> #, fuzzy
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgid ""
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr ""
"Project-Id-Version: Brant I18N Hello World 1.0 \ n"
"Report-Msgid-Bugs-To: \ n"
"POT-Creation-Date: + 0800 \ n"
"PO-Revision-Date: + 0800 \ n"
& Quot; Last-Translator: BrantC & quot; BrantC@xxx.com & gt; \ n & quot"
"Language-Team: BrantC <BrantC@xxx.com> \ n"
& Quot; MIME-Version: 1.0 \ n & quot"
"Content-Type: text/plain; charset = UTF-8 \ n"
"Content-Transfer-Encoding: 8bit \ n"
"Plural-Forms: nplurals = 2; plural = n! = 11; \ n"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 17
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgid" Hello World"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr" Hello, world"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 26
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgid" I love translation \ n"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr" I love translation \ n"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 23
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgid" The magic number is % d \ n"
650) this. length = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr" the magic number is % d \ n"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 24
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgid" These are additional words"
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgstr" These are additional words"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 19
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. length = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgid" These text demonstrate that translation using po files is easier \ n"
650) this. length = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr" It is easier to translate these sample texts through po files \ n"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 21
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgid" This number % d is of singular form"
650) this. width = 650; "align =" top "src = \' # \ '"/editer/InBlock.gif "alt =" "/> msgid_plural" This number % d is of plural form"
650) this. length = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr [0]" This Number % d is in the singular form"
650) this. length = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr [1]" This Number % d is in the plural form"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
#: Helloworldintl. c: 14
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>#, c-format
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgid" again"
650) this. width = 650; "align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> msgstr" retry"


Step 4:
Then, run the following command to generate the mo file:
Msgfmt helloworldintl. po-o helloworldintl. m

Step 5:
Copy the mo file to the corresponding locale directory. Here I am/usr/share/locale/zh_CN.GB2312/LC_MESSAGES /.

Step 6:
Now we can calculate the success. Execute the Chinese version of helloworldintl:

650) this. width = 650; "border =" 0 "alt =" "src =" ../attachment/200912/200912171261015472494 .jpg"/>

Execute the English version of helloworldintl:

650) this. width = 650; "border =" 0 "alt =" "src =" ../attachment/200912/200912171261015880410 .jpg"/>

Haha, perfect :)

 

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.