C#本地化不起作用

时间:2011-12-01 15:03:20

标签: c# winforms localization

-EDIT 2-想出来,检查我的答案。我应该停止在StackOverflow上提问,我总是在几分钟后发现它。)

我有以下代码(好吧,粗略地,检测和一切都在顶部):

Manager = new ResourceManager("EM250.Localization.Application", 
                              Assembly.GetExecutingAssembly());

// CurrentCulture being fr-CA in this case
return Manager.GetString(Key, CurrentCulture); 

因此,我使用我制作的自定义类管理我的本地化资源。

我有两个文件,一个名为“Application.resx”,另一个名为“Application.fr-CA.resx”。两者都设置为“嵌入式资源”。当我编译我的代码时,我得到一个名为“fr-CA”的文件夹,其中包含文件“Library.resources.dll”。这完全没问题。

任何人都知道为什么我的代码无效?我到处都是英文字符串。

请注意,这不是ASP.NET项目,而是实际的WinForms项目。

-EDIT-应该发布剩下的代码,这是我的应用程序的初始化:

        String LocaleLoad;

        if(Configuration.GetString("Language") == null)
        {

            // Detect which if the computer is set in french.
            switch(System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName)
            {
            case "fr":
                LocaleLoad = "fr-CA";
                break;

            default:
                LocaleLoad = "en-CA";
                break;
            }

        }
        else
        {

            LocaleLoad = Configuration.GetString("Language");

        }

        // Initialize the localization class.
        Localization.Initialize(LocaleLoad);

这是我的本地化课程:

    /// <summary>
    /// Initialize the culture info with the specific culture.
    /// </summary>
    /// <param name="Culture">The culture to load.</param>
    public static void Initialize(string Culture)
    {

        CultureInfo NewCulture = new CultureInfo(Culture);

        if(!(Localization.CurrentCulture == null || CurrentCulture.TwoLetterISOLanguageName == NewCulture.TwoLetterISOLanguageName))
        {

            CurrentCulture = NewCulture;

            Thread.CurrentThread.CurrentCulture = NewCulture;
            Thread.CurrentThread.CurrentUICulture = NewCulture;

            OnCultureChanged();

        }

        Thread.CurrentThread.CurrentCulture = NewCulture;
        Thread.CurrentThread.CurrentUICulture = NewCulture;

        CurrentCulture = NewCulture;

        foreach(Language Lang in ListLanguages)
        {
            if(Lang.DisplayName == CurrentCulture.NativeName)
            {
                CurrentLanguageValue = Lang;    
            }
        }

        Manager = new ResourceManager("EM250.Localization.Application", Assembly.GetExecutingAssembly());

    }

    /// <summary>
    /// Get a single entry from the ressource file.
    /// </summary>
    /// <param name="Key">The value to get from the ressource file.</param>
    public static string GetString(string Key)
    {

        return Manager.GetString(Key, CurrentCulture);  
    }

2 个答案:

答案 0 :(得分:2)

如果手动设置您的文化,请不要忘记设置CurrentUICulture:

Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-CA");

之后,检索到正确的资源。

答案 1 :(得分:0)

我的资源ID未正确设置为我的fr-CA文件。我的en-CA文件设置为“EM250.Localization.Application.resources”,fr-CA为“Library.Localization.Application.fr-CA.resources”