【问题标题】:Unit testing localized strings单元测试本地化字符串
【发布时间】:2012-01-22 12:35:43
【问题描述】:

我们的应用程序中有几千个本地化字符串。我想创建一个单元测试来遍历所有键和所有我们支持的语言,以确保每种语言的每个键都存在于默认(英语)resx 文件中。

我的想法是使用反射从 Strings 类中获取所有键,然后使用 ResourceManager 比较每种语言中每个键的检索值并进行比较以确保它不匹配英文版,但当然,某些单词在多种语言中是相同的。

有没有办法检查ResourceManager 是否从附属程序集与默认资源文件中获得其值?

调用示例:

string en = resourceManager.GetString("MyString", new CultureInfo("en"));
string es = resourceManager.GetString("MyString", new CultureInfo("es"));

//compare here

【问题讨论】:

    标签: c# .net localization


    【解决方案1】:

    调用ResourceManager.GetResourceSet方法获取中立文化和本地化文化的所有资源,然后比较两个集合:

    ResourceManager resourceManager = new ResourceManager(typeof(Strings));
    IEnumerable<string> neutralResourceNames = resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, false)
        .Cast<DictionaryEntry>().Select(entry => (string)entry.Key);
    IEnumerable<string> localizedResourceNames = resourceManager.GetResourceSet(new CultureInfo("es"), true, false)
        .Cast<DictionaryEntry>().Select(entry => (string)entry.Key);
    
    Console.WriteLine("Missing localized resources:");
    foreach (string name in neutralResourceNames.Except(localizedResourceNames))
    {
        Console.WriteLine(name);
    }
    
    Console.WriteLine("Extra localized resources:");
    foreach (string name in localizedResourceNames.Except(neutralResourceNames))
    {
        Console.WriteLine(name);
    }
    

    【讨论】:

    • 这太完美了,迈克尔!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    相关资源
    最近更新 更多