[Mono-osx] MonoMac CurrentCulture

Johan Hammar johanhmr at gmail.com
Mon Dec 17 21:16:00 UTC 2012


I am adding support for multiple languages in my MonoMac application
(english and swedish). Some of my resources are based on resx-files,
hence Thread.CurrentThread.CurrentUICulture determines which language is
used for these resources.

I was expecting Thread.CurrentThread.CurrentUICulture to be automatically
set up to the appropriate culture based on the user preferred language
(i.,e NSLocale.PreferredLanguages) or fall back to the neutral culture if
not supported. However Thread.CurrentThread.CurrentUICulture does always
seem to be set to the same culture regardless of user preferred language.

I currently have the created the following workaround (see code below) to
programmatically setup CurrentCulture and CurrentUICulture. The most
annoying part about it is that Thread.CurrentThread.CurrentCulture (used
for number formatting etc) does not accept a neutral culture (e.g, "sv"),
if it did we could have mapped directly
from NSBundle.MainBundle.PreferredLocalizations[0] to a CultureInfo
instance.

- However I would appreciate if anyone got a better solution to setup the
current culture based on the user preferred language and the culture
supported by the included resource files (i.e, resx).
- Also I would like to know what is determining the
default Thread.CurrentThread.CurrentUICulture ?

string[] preferredLocalizations =
NSBundle.MainBundle.PreferredLocalizations; //Since we also use lproj
directories for XIB internationalization we can get the most appropriate
neutral language from here.

string preferredLocalization = preferredLocalizations[0];

string cultureName = "en-US";

if (preferredLocalization.Equals("sv")) {

  cultureName = "sv-SE";

 }

 CultureInfo culturInfo = new CultureInfo(cultureName);

Thread.CurrentThread.CurrentCulture = culturInfo;   //CurrentCulture does
not accept a neutral culture e.g, "sv", when specifying a neutral culture
an exception is thrown with the message:"Culture 'sv' is a neutral culture.
It cannot be used in formatting and parsing and therefore cannot be set as
the thread", hence we workaround this by setting it to en-US or sv-SE in
the if statement above.

 Thread.CurrentThread.CurrentUICulture = culturInfo; //CurrentUICulture
accepts a neutral culture, but CurrentCulture does not, hence the if
statement above.

Any input would be appreciated

Best Regards,

Johan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-osx/attachments/20121217/148c73f1/attachment.html>


More information about the Mono-osx mailing list