Unit testing localized strings

We have a couple of thousand localized strings in our application. I want to create a unit test to iterate all the keys and all the languages ​​you support so that each language has every key present in the standard (English) resx file.

My idea is to use Reflection to get all the keys from the Strings class, and then use the ResourceManager to compare the resulting value for each key in each language and compare it to make sure it doesn't match the English version, but of course some words are the same in different languages.

Is there a way to check if the ResourceManager got its value from the satellite assembly compared to the default resource file?

Call example:

 string en = resourceManager.GetString("MyString", new CultureInfo("en")); string es = resourceManager.GetString("MyString", new CultureInfo("es")); //compare here 
+6
source share
1 answer

Call the ResourceManager.GetResourceSet method to get all the resources for a neutral and localized culture, and then compare the two collections:

 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); } 
+8
source

Source: https://habr.com/ru/post/906508/


All Articles