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