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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With