gettext在windows下成功

来自中国的朋友,欢迎您在本版面使用中文讨论问题。请注意,如果您想得到不懂中文的人的帮助,请同时提供英文译文。
Help and discussions in Chinese.
Post Reply
liqi98136
Posts: 53
Joined: Sun Jan 17, 2010 11:37 pm

gettext在windows下成功

Post by liqi98136 »

过程
1.准备软件.下载从mingw官方
gettext-0.17-1-mingw32-dev.tar.lzma
libiconv-1.13.1-1-mingw32-dev.tar.lzma
libintl-0.17-1-mingw32-dll-8.tar.lzma
libgettextpo-0.17-1-mingw32-dll-0.tar.lzma
libasprintf-0.17-1-mingw32-dll-0.tar.lzma
libcharset-1.13.1-1-mingw32-dll-1.tar.lzma
poedit
解压到mingw目录下bin,lib,include,share等等

2.新建jian_gettext.c

Code: Select all

#include <stdio.h> 
#include <locale.h> 
#include <libintl.h> 
#define _(S) gettext(S) 
#define PACKAGE "jian_gettext" 
int main(int argc, char **argv) 
{ 
	setlocale(LC_ALL,"");
	bindtextdomain(PACKAGE, "locale"); 
	textdomain(PACKAGE); 
	printf(_("Hello,GetText!\n")); 
	
	return 0; 
}
先编译
gcc -o jian_gettext jian_gettext.c
报错
jian_gettext.c:(.text+0x36): undefined reference to `_libintl_bindtextdomain'
jian_gettext.c:(.text+0x46): undefined reference to `_libintl_textdomain'
jian_gettext.c:(.text+0x56): undef ined reference to `_libintl_gettext'
jian_gettext.c:(.text+0x62): undefined reference to `___printf__'
collect2: ld returned 1 exit status
原因是mingw没找到intl
gcc -o jian_gettext jian_gettext.c -intl
网上说增加gcc 的lib路径就可以了
注意:在ECERE IDE中如何实现呢,还不知道
好了程序可以运行了.
我们来做翻译工作吧

Code: Select all

xgettext -o jian_gettext.po jian_gettext.c
生成jian_gettext.po
修改
"Content-Type: text/plain; charset=CHARSET\n"

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

修改
msgstr ""

msgstr "你好,GetText!\n"

保存为UTF-8格式文件

打开poedit 打开jian_gettext.po保存后,自动生成jian_gettext.mo文件
将.mo文件拷贝到jian_gettext所在的 locale\zh_CN\LC_MESSAGES文件下
重新生成一次
gcc -o jian_gettext jian_gettext.c -lintl
好了
运行一下jian_gettext
jian_gettext
你好,GetText!
第一次运行的比较慢,再运行就快了.
liqi98136
Posts: 53
Joined: Sun Jan 17, 2010 11:37 pm

Re: gettext在windows下成功

Post by liqi98136 »

在ecere IDE中实现翻译工作
要下载的文件已经在上一个贴中指出,这里就再不提了.

Code: Select all

#include <locale.h>
#include <libintl.h>

#define PACKAGE "hello"
#define LOCALEDIR "languages"
#define N_(msgid) gettext(msgid)


import "ecere"

class helloApp : Application
{
void Main()
{
   setlocale(LC_ALL, "");
   bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
   printf(N_("this is a string\n"));
   system("pause");
}
}   
同样系统会报错,
找不到...
...
...
解决办法:选择project ->settings,找到linker在ecere边上加上intl
再试试,成功了吧.
用poedit下的
xgettext --keyword=N_ -o hello.po hello.ec
打开hello.po,
修改
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Type: text/plain; charset=UTF-8\n"

msgsid "this is a string\n"
msgstr "这是一个字符串\n"

保存后,用poedit打开,保存,会提示错误,
解决:菜单类目->设置,修改工程名和团队名
保存后就成功了.生成hello.mo
新建目录
languages/zh_CN/LC_MESSAGES
拷贝hello.mo到此目录

将编译完成的hello.exe放在languages的上一级目录即可
看看结果吧,是不是变了. :D
终于成功了.
Attachments
hello.rar
(10.69 KiB) Downloaded 6896 times
liqi98136
Posts: 53
Joined: Sun Jan 17, 2010 11:37 pm

Re: gettext在windows下成功

Post by liqi98136 »

接着上贴的进展
实现多个源文件的翻译工作

********hello2.ec********

Code: Select all

#include <locale.h>
#include <libintl.h>

#define PACKAGE "hello2"
#define LOCALEDIR "languages"
#define N_(msgid) gettext(msgid) 

import "ecere"
[color=#FF0000]import "myprint"  [/color]
class helloApp : Application
{
void Main()
{
   setlocale(LC_ALL, "");
   bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
   printf(N_("this is a string\n"));
[color=#FF0000]   myprint();[/color]
   system("pause");
}
} 
模块文件myprint.ec
********myprint.ec********

Code: Select all

#include <libintl.h>  
#define N_(msgid) gettext(msgid)

import "ecere"
void myprint()
{
   printf(N_("this the myprint function.\n"));
}  
接下生生成源文件列表
dir *.ec /s /b >filelist.txt
在源文件目录生成文件列表,把绝对路径替换为相对路径.
hello2.ec
myprint.ec


生成po文件
xgettext -f filelist.txt -kN_ -o hello2.po --from-code=utf-8

生成了po文件
接着就是用poedit翻译修改,生成mo
后面就跟上贴一样了
Attachments
hello2.rar
(2.16 KiB) Downloaded 6790 times
Post Reply