Mono C# Set CultureInfo not working on Windows

2019-08-08 02:10发布

问题:

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.