My application will be translated into several languages.
I can include strings for the various supported language (strings.fr-BE.resx, strings.nl.resx etc).
I also have some external files (myfile.nl.txt etc) that also need to be picked up in the same way, including fallback if not found.
Obviously, I can write a fairly simple function to enumerate the language extensions, from the selected language (MyFile.fr-BE.tx -> MyFile.fr.txt -> MyFile.txt), but wondered if any part of this mechanism is exposed by the .net framework, so I can re-use the existing convention.
There's nothing built in that I'm aware of but it's easy enough to construct once you're aware of the Parent
property of CultureInfo
:
public static class CultureInfoExtensions
{
public static IEnumerable<CultureInfo> WithParents(this CultureInfo culture)
{
while (true)
{
yield return culture;
if (culture.Parent == culture) yield break;
culture = culture.Parent;
}
}
}
And testing:
var test = new CultureInfo("fr-BE");
foreach(var culture in test.WithParents())
{
Console.WriteLine(culture);
}
Yields:
fr-BE fr
(The last line being the empty string)