3
votes

My app has been translated to a number of languages, including French, Traditional Chinese, and Simplified Chinese. Now, when the app first launches, I'd like to automatically select the correct translation based on the user locale.

For French, evaluating the TwoLettersIsoLanguageName property of the InstalledUiCulture worked well, but for Chinese ISO codes are not sufficient. I thought of resorting to the Name property, which separates zh-Hans and zh-Hant, but that breaks French, since it gives fr-FR on certain systems, and fr-CA on others, for example.

Is there a simple way to get the current culture's two-letter language code if it's not chinese, and otherwise get zh-Hans/zh-Hant?

I'm searching for an elegant solution ; I've of course thought of parsing the culture's Name property and making a special case for chinese, but that's not really pretty :/

In short, I think I'm searching for a way to retrieve a "neutral" culture from the current culture: from fr-Fr get fr, from zh-TW get zh-Hant, and from zh-CN get zh-Hans. Ideas?

Thanks!

1
Why are you not using satellite assemblies? - Hans Passant
Because I'm fixing on a rather old codebase :/ - Clément
Don't forget Norwegian and Portuguese, for which there are also multiple dialects. And what happens if your company decides to create a special Canadian-French translation? - Raymond Chen
@RaymondChen: That's right. In that case I'll load the specific translation. - Clément
Then the question isn't really "I want a function that returns the subculture if the subculture is Chinese, but the neutral culture otherwise." It's "I want a function that returns the subculture if I have a translation for that subculture, and the neutral culture otherwise." And there is naturally no built-in OS function for this, since the OS doesn't know what languages you've been translated into. - Raymond Chen

1 Answers

2
votes

It seems that Parent property of CultureInfo class is what you are looking for.
It seems it was supported from the very beginning.

BTW. In case of translations, you should use CurrentUICulture rather than CurrentCulture (the latter is meant for formatting) or InstalledUiCulture, which is Operating System language version and not necessary user preference.