Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing localized strings

We have a couple thousand localized strings in our application. I am wanting to create a unit test to iterate through all the keys and all of our supported languages to make sure that every language has every key present in the default (English) resx file.

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

Is there a way to check if the ResourceManager obtained its value from a satellite assembly vs. the default resource file?

Example call:

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

//compare here
like image 429
Steve Danner Avatar asked Jan 20 '12 22:01

Steve Danner


1 Answers

Call the ResourceManager.GetResourceSet method to get all the resources for the neutral and localized cultures, 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);
}
like image 78
Michael Liu Avatar answered Nov 12 '22 06:11

Michael Liu