c# - Is there a .net function to enumerate the language fallback file extensions -


my application translated several languages.

i can include strings various supported language (strings.fr-be.resx, strings.nl.resx etc).

i have external files (myfile.nl.txt etc) need picked in same way, including fallback if not found.

obviously, can write simple function enumerate language extensions, selected language (myfile.fr-be.tx -> myfile.fr.txt -> myfile.txt), wondered if part of mechanism exposed .net framework, can re-use existing convention.

there's nothing built in i'm aware of it's easy enough construct once you're aware of 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 empty string)


Comments