跳到主要內容

Gettext quickstart---install and demo

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.

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
===================================================================
  1. 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 bybindtextdomaingettext, and textdomain.
  2. 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.''
  3. 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.
  4. 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.
  5. Finally, on line 10, we have replaced what would normally have been,
      printf( "Hello, world!\n" );
    
    with,
      printf( gettext( "Hello, world!\n" ) );
Extracting translatable strings
===================================================================
xgettext -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_MESSAGES
Output
===================================================================
gcc hello.c -o hello
./hello
>你好,GetText!


Program upgrade
===================================================================
The 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_MESSAGES
Output
===================================================================
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.

留言

這個網誌中的熱門文章

Useful Gvim Skills for translation work

As I use linux as normal operating system, and prefer gvim very much. I would like to share some skills to help keep your hands away from mouse bracing the high efficient way of working. Especially for the survive stage vim newbees. I hope the article will help you get into the translation work faster than spending long time learning vim from 0. 因为日常使用linux系统,翻译工作的工具又比较偏爱gvim,所以分享一些小技巧,希望帮助大家远离“鼠标+键盘”的低效工作模式,特别是刚刚处于survive阶段的vim轻度用户。快速在翻译工作中高效使用vim。 1.MOVE    移动 1.1 move between msgstr       msgstr间移动 The basic item of .po file is msgstr, when whose state is untranslated and fuzzy need working on. So jump between the msgstr is very simple: 在翻译中基本单位是msgstr,需要翻译的状态是:未翻译和fuzzy,在这两种状态直接的跳跃方法很简单: In both normal and insert state, SHIFT+F1, next untranslated,  SHIFT+F2, pre untranslated  SHIFT+F5, next fuzzy, SHIFT+F6, pre fuzzy. 两种状态下,SHIFT+F1,下一条未翻译,SHIFT+F2,上一条未翻译。 SHIFT+F5,下一条fuzzy,SHIFT+F6,上一条fuzzy。 If you would lik...

Back to Blogger

Start up my blogger spot again~ After working in a frequent re-organizing business unit in a big company for nearly 10 years, struggling in the seas of system, Bigdata, ML/DL, cloud, etc. On one hand I feel myself like Columbus overrun the seven seas(OSI) dreaming for land, on the other hand I feel like farming the four seasons, harvesting less for climate change or tribes migrate.  This year I have a new plan, to start a journey back to land. To farm on a small, stable land, growing something I like. Ref: little forest   Why I choose back to blogger, my expectation of my renew blog: - To track the thoughts - To incubate articles - To communicate on related topics    My previous ways: - github issue     - Pro: quiet enough for me:)     - Con: no feedback and discussion for the thoughts - local obsidian notes/xmind     - Pro: very efficient to trace and summary     - Con: I am the only reader The only way I c...

Summary about Translation Work

I am so not good at summary, because it is so hard to make it meaningful and worth sharing. But looking back, review the condition of our translation work, will be at least helpful to help newer like me get involved more easily. 我非常不擅长总结,这个工作要做到有意义值的分享很有难度。但是回顾一下,理清现在中文翻译的情况,特别是从新人的角度,希望帮助其他加入者了解怎样成长更快,怎样对用户使用中文 GNOME 帮助更大。 After reading another intern who also works on translation, JuditGyimesi, whose blog is in different style. I changed my mind to share statics and skills. Maybe because I am a Chinese who was educated from a young age that every share need to give reader knowledge. From her blog I have learnt sharing when translating the translator feel happy still make sense. 在我参考了另外一位翻译实习生 JuditGyimesi 的 博客  以后,把这篇文章的风格修改了一下,不知道是不是因为自己是中国人的原因,过于希望在博客里分享知识,怎样做,能学会什么,或者简简单单的分享自己做了些什么,觉得很开心就好了。 Talk about the growth in the translation program, the most important must be understanding the important or urgent situation of each program need translated. And finding out ...