Mono C# Set CultureInfo not working on Windows

微笑、不失礼 提交于 2019-12-24 14:15:01

问题


I'm currently facing a bug, that the language of my application can not be set to something different than system default. I use the following Code to set the CultureInfo:

Catalog.Init("AudioCuesheetEditor", Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + MainClass.CONST_STR_LOCALE_PATH));
[...]
//Set the locale
log.debug("CultureInfo.CurrentCulture = " + CultureInfo.CurrentCulture);
if (Program.getInstance().getObjOption().getStrSelectedLanguage() != null)
{
    log.debug("Program.getInstance().getObjOption().getStrSelectedLanguage() = " + Program.getInstance().getObjOption().getStrSelectedLanguage());
    Thread.CurrentThread.CurrentCulture = new CultureInfo(Program.getInstance().getObjOption().getStrSelectedLanguage());
    if (Environment.OSVersion.Platform == PlatformID.Unix)
    {
        //Mono Linux hack
        Environment.SetEnvironmentVariable("LANGUAGE", Program.getInstance().getObjOption().getStrSelectedLanguage().Replace("-", "_"));
    }
}
Application.Init();
[...]
Application.Run();

This works perfect on linux, but doesn't work with gtk# on Windows. The CultureInfo is changed, but the language doesn't change. Any ideas why?

I get the string with Code like this:

Catalog.GetString("general ready")

I'm not shure, how to find the bug and what is wrong. Anybody who can help, thanks in advance ;).

Whole code can be found here: http://sourceforge.net/p/audiocuesheet/code/HEAD/tree/trunk/src/AudioCuesheetEditor/MainClass.cs

Sven


回答1:


Since internationalization is based on GNU gettext, my assuption would be that the windows implementation slightly differs form the unix one, so the LANGUAGE variable is read at startup before any of your code is executed. The only work around I can think of is to call your program with a script that sets the LANGUAGE variable.




回答2:


I found out, that changing the environment variable "LANG" on windows works. So if I set "LANG=en" the application starts with english translation.



来源:https://stackoverflow.com/questions/29152289/mono-c-sharp-set-cultureinfo-not-working-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!