由于最近要对一个C#环境下的一个项目进行多语言处理,使用的是gettext并把PO文件编译成dll文件。我在调试中发现在windows下用msgfmt命令编译时总是不成功,于是想到在 linux环境下编译,果然可以。
具体操作步骤如下:
1、C#代码编写以及提取字符串运行环境:gettext 0.14.4 for win32; mono 1.9.1 for windows; Poedit 1.4.1; .net 3.5;
2、编译resources.dll linux环境:openSUSE 10.3 ; mono 1.9 for linux ;
3、编译\GnuWin32\src\gettext\0.14.4\gettext-0.14.4-src\gettext-runtime\intl-csharp\intl.cs为GNU.Gettext.dll
4、新建一个工程,并引用GNU.Gettext.dll
5、定义一个类
public class TeculGettext
{
//public delegate string DelegatedGettextMethod(string str); //定义委托,用_代替catalog.GetString方法
public static GettextResourceManager catalog = new GettextResourceManager("l10n");
public TeculGettext()
{
String locale = System.Environment.GetEnvironmentVariable("LC_ALL");
if (locale == null || locale == "")
locale = System.Environment.GetEnvironmentVariable("LANG");
if (!(locale == null || locale == ""))
{
if (locale.IndexOf('.') >= 0)
locale = locale.Substring(0, locale.IndexOf('.'));
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Threading.Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo(locale.Replace('_', '-'));
}
}
}
6、实例化TeculGettext
如:
public static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
TeculGettext teculGettext = new TeculGettext();
}
}
7、在需要翻译的字符串前加上方法TeculGettext.catalog.GetString(".........");
8、在windows环境下运行xgettext *cs -ol10n.pot --from-code=utf-8 提取要翻译的字符
9、用poedit软件把l10n.pot文件翻译并转换成l10n.mo、l10n.po文件,并把l10n.po改名为zh_CN.po
10、把zh_CN.po文件放到linux下用命令 msgfmt --csharp -r l10n -l zh_CN zh_CN.po -d . -v 编译成l10n.resources.dll
11、把l10n.resources.dll拷贝到\bin\Debug\zh-CN路径下
12、运行程序,即可看见翻译后的字符。
浙公网安备 33010602011771号