Introduce
Most people know Internationalization and localization, maybe talk it later.
Install
===================================================================
if you need Chinese guide, here. I followed it, very useful, without error.
Download
-------------------------------------------------------------------------------------------------------------------------
ftp://ftp.gnu.org/gnu/gettext/
choose a version you like.
Install
-------------------------------------------------------------------------------------------------------------------------
tar -xzf gettext-<version>.tar.gz
./configure --prefix=/usr
cd gettext-<version>/
make
make check ---------just test, take a long time ,could skip
make install
Demo
===================================================================
refer several blogs and howto, each has some tiny things to fix I will list the link at the bottom.
Most people know Internationalization and localization, maybe talk it later.
===================================================================
if you need Chinese guide, here. I followed it, very useful, without error.
Download
-------------------------------------------------------------------------------------------------------------------------
ftp://ftp.gnu.org/gnu/gettext/
choose a version you like.
Install
-------------------------------------------------------------------------------------------------------------------------
tar -xzf gettext-<version>.tar.gz
./configure --prefix=/usr
cd gettext-<version>/
make
make check ---------just test, take a long time ,could skip
make install
Demo
===================================================================
refer several blogs and howto, each has some tiny things to fix I will list the link at the bottom.
We could learn the whole flow here, from overview of gettext
Original C Sources ───> Preparation ───> Marked C Sources ───╮
│
╭─────────<─── GNU gettext Library │
╭─── make <───┤ │
│ ╰─────────<────────────────────┬───────────────╯
│ │
│ ╭─────<─── PACKAGE.pot <─── xgettext <───╯ ╭───<─── PO Compendium
│ │ │ ↑
│ │ ╰───╮ │
│ ╰───╮ ├───> PO editor ───╮
│ ├────> msgmerge ──────> LANG.po ────>────────╯ │
│ ╭───╯ │
│ │ │
│ ╰─────────────<───────────────╮ │
│ ├─── New LANG.po <────────────────────╯
│ ╭─── LANG.gmo <─── msgfmt <───╯
│ │
│ ╰───> install ───> /.../LANG/PACKAGE.mo ───╮
│ ├───> "Hello world!"
╰───────> install ───> /.../bin/PROGRAM ───────╯
or a easier one here, guide from Wylmer Wang
A simple example : hello.c===================================================================1 #include <libintl.h> 2 #include <locale.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 int main(void) 6 { 7 setlocale( LC_ALL, "" ); 8 bindtextdomain( "hello", "/usr/share/locale" ); 9 textdomain( "hello" ); 10 printf( gettext( "Hello, Gettext!\n" ) ); 11 exit(0); 12 }The programmer's viewpoint===================================================================
===================================================================
- locale.h defines C data structures used to hold locale information, and is needed by the setlocale function. libintl.h prototypes the GNU text utilities functions, and is needed here bybindtextdomain, gettext, and textdomain.
- The call to setlocale () on line 7, with LC_ALL as the first argument and an empty string as the second one, initializes the entire current locale of the program as per environment variables set by the user. In other words, the program locale is initialized to match that of the user. For details see ``man setlocale.''
- The bindtextdomain function on line 8 sets the base directory for the message catalogs for a given message domain. A message domain is a set of translatable messages, with every software package typically having its own domain. Here, we have used ``hello'' as the name of the message domain for our toy program. As the second argument, /usr/share/locale, is the default system location for message catalogs, what we are saying here is that we are going to place the message catalog in the default system directory. Thus, we could have dispensed with the call tobindtextdomain here, and this function is useful only if the message catalogs are installed in a non-standard place, e.g., a packaged software distribution might have the catalogs under a po/ directory under its own main directory. See ``man bindtextdomain'' for details.
- The textdomain call on line 9 sets the message domain of the current program to ``hello,'' i.e., the name that we are using for our example program. ``man textdomain'' will give usage details for the function.
- Finally, on line 10, we have replaced what would normally have been,
printf( "Hello, world!\n" );with,printf( gettext( "Hello, world!\n" ) );Extracting translatable stringsxgettext -d hello -s -o hello.pot hello.c
msginit -l zh_CN -o zh_CN.po -i hello.pot
hello.pot
===================================================================# SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR Free Software Foundation, Inc. # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "POT-Creation-Date: 2014-02-02 15:33+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <LL@li.org>\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" #: hello.c:11 msgid "Hello,GetText!\n" msgstr ""zh_CN.po===================================================================# Chinese translations for PACKAGE package. ----attention # Copyright (C) 2014 Free Software Foundation, Inc. # sphinx <yishanj13@gmail.com>, 2014. # msgid "" msgstr "" "Project-Id-Version: PACKAGE\n" ----attention "POT-Creation-Date: 2014-02-02 16:22+0800\n" "PO-Revision-Date: 2014-02-02 18:53+0800\n" "Last-Translator: sphinx <yishanj13@gmail.com>\n" "Language-Team: Chinese (simplified)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=ASCII\n" ----attention "Content-Transfer-Encoding: 8bit\n" "Language: zh_CN\n" #: hello.c:11 msgid "Hello,GetText!\n" msgstr ""attention===================================================================The po file need to be modified, replace "PACKAGE" with the string in textdomain(textdomain( "hello" );),and check the charset, if charset equal to your current system language(echo $LANG),if not msgfmt will come to an error.zh_CN.po(after modify and translate)===================================================================# Chinese translations for hello example package. ----attention # Copyright (C) 2014 Free Software Foundation, Inc. # sphinx <yishanj13@gmail.com>, 2014. # msgid "" msgstr "" "Project-Id-Version: hello\n" ----attention "POT-Creation-Date: 2014-02-02 16:22+0800\n" "PO-Revision-Date: 2014-02-02 18:53+0800\n" "Last-Translator: sphinx <yishanj13@gmail.com>\n" "Language-Team: Chinese (simplified)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" ----attention "Content-Transfer-Encoding: 8bit\n" "Language: zh_CN\n" #: hello.c:11 msgid "Hello,GetText!\n" msgstr "你好,GetText!\n"Message catalogs===================================================================
msgfmt -c -v -o hello.mo zh_CN.po
cp hello.mo /usr/share/locale/or_IN/LC_MESSAGESOutput===================================================================gcc hello.c -o hello./hello===================================================================>你好,GetText!Program upgradeThe previous section presented a simple example of how Chinese simple language support could be added to a C program. Like all programs, we might now wish to further enhance it. For example, we could include a greeting to the user by adding another printf statement after the first one. Our new hello.c source code might look like this:1 #include <libintl.h> 2 #include <locale.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 int main(void) 6 { 7 setlocale( LC_ALL, "" ); 8 bindtextdomain( "hello", "/usr/share/locale" ); 9 textdomain( "hello" ); 10 printf( gettext( "Hello, world!\n" ) ); 11 printf( gettext( "How are you\n" ) ); 12 exit(0); 13 }Merging old and new translations===================================================================
xgettext -d hello -s -o hello-new.pot hello.c
msgmerge -s -U hello.po hello-new.pot (I tested, this one won't work with gettext 0.10.40, no U option, and won't merge po and pot.)msginit -l zh_CN -o zh_CN-new.po -i hello-new.pot
msgmerge -s -o zh_CN.po zh_CN.po zh_CN-new.po
now the po zh_CN.po===================================================================# Chinese translations for hello example package. # Copyright (C) 2014 Free Software Foundation, Inc. # sphinx <yishanj13@gmail.com>, 2014. # msgid "" msgstr "" "Project-Id-Version: hello\n" "POT-Creation-Date: 2014-02-02 16:22+0800\n" "PO-Revision-Date: 2014-02-02 18:53+0800\n" "Last-Translator: sphinx <yishanj13@gmail.com>\n" "Language-Team: Chinese (simplified)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Language: zh_CN\n" #: hello.c:11 msgid "Hello,GetText!\n" msgstr "你好,GetText!\n" #: hello.c:12 msgid "How are you\n" msgstr ""===================================================================Message catalogs(after you tranlate :))msgfmt -c -v -o hello.mo zh_CN.po
cp -f hello.mo /usr/share/locale/or_IN/LC_MESSAGESOutput===================================================================gcc hello.c -o hello./hello>你好,GetText!>你好吗Optimize===================================================================
1 #include <libintl.h> 2 #include <locale.h> 3 #include <stdio.h> 4 #include <stdlib.h>5 #define _(STRING) gettext(STRING)6 #define PACKAGE "hello" 8 int main(void) 9 { 10 setlocale(LC_ALL, ""); 11 bindtextdomain(PACKAGE, "/usr/share/locale"); 12 textdomain(PACKAGE); 13 14 printf(_("Hello,GetText!n")); 15 return 0; 16 }Reference===================================================================----excellent!I follow it,just little problems, the explanation and catalog are so good.----A Chinese guide, optimized, without merge.
留言
張貼留言