我们的应用程序中有几千个本地化字符串.我想创建一个单元测试来迭代所有键和所有支持的语言,以确保每种语言都有默认(英语)resx文件中的每个键.
我的想法是使用Reflection来获取Strings类中的所有键,然后使用ResourceManager比较每种语言中每个键的检索值并进行比较以确保它与英语版本不匹配,当然,多种语言中的某些词语相同.
有没有办法检查ResourceManager是否从附属程序集中获取其值与默认资源文件?
示例电话:
string en = resourceManager.GetString("MyString",new CultureInfo("en")); string es = resourceManager.GetString("MyString",new CultureInfo("es")); //compare here
解决方法
调用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"),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); }