【问题标题】:.NET windows WPF LOCALIZATION.NET windows WPF 本地化
【发布时间】:2011-09-29 05:24:01
【问题描述】:

我是本地化新手... ..我最初创建了一个资源文件(.resx 文件),然后使用 resgen 命令生成相应的 .resource 文件.. 它已创建.. 然后我使用 al.exe 生成相应的卫星程序集..它也被创建了..但是当我尝试使用资源管理器类从代码隐藏中访问 .resx 文件时,它抛出了类似的错误--

程序集清单异常..

我不明白我错在哪里..请让我知道是否有任何进一步的过程要做(请不要建议使用 LOCBAML 工具)..我想要仅使用资源文件的解决方案..

【问题讨论】:

    标签: .net wpf localization resx


    【解决方案1】:

    你的开始很不错。使用 LocBaml 对我来说也太复杂了。所以我必须想出一些对我来说更容易的事情。这是解决方案:首先,您必须创建资源 .resx 文件(此步骤已在您完成);从资源文件中获取所有字符串的简单方法是将其存储在字典中。您可以使用此方法:

    public Dictionary<string, string> ApplicationStrings(string locale)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
    
            ResourceManager manager = null;
    // Here is two locales (custom and english locale), but you can use more than two if there's a need
                if (locale == "Your locale")
                {
                    manager = new ResourceManager(typeof(your_locale_resource_file));
                }
                else
                {
                    manager = new ResourceManager(typeof(ApplicationStrings_en));
                }
            ResourceSet resources = manager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
    
            IDictionaryEnumerator enumerator = resources.GetEnumerator();
    
            while (enumerator.MoveNext())
            {
                result.Add((string)enumerator.Key, (string)enumerator.Value);
            }
    
            return result;
        }
    

    您必须将 MainWindow 的 DataContext 设置为此方法结果并将所有字符串绑定到字典。 绑定示例:

    Text="{Binding [YourKey]}"
    

    您可以调用此方法,然后随时随地更改 DataContext。多亏了数据绑定,它在运行时工作得很好。 我保证这个解决方案不是最好的,但它的工作方式很简单。 希望对你有帮助。

    【讨论】:

      【解决方案2】:

      查看这些文章,了解有关在 WPF 中使用 Resx 文件进行本地化的详细信息 -

      Localizing a WPF Application with ResX Files

      WPF Localization

      我也建议你通过这些 -

      WPF Globalization and Localization Overview

      WPF Localization Guidance - Whitepaper

      【讨论】:

        猜你喜欢
        • 2013-05-08
        • 1970-01-01
        • 1970-01-01
        • 2015-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-22
        • 1970-01-01
        相关资源
        最近更新 更多