Bug 34074 - Exception cultureInfo
Summary: Exception cultureInfo
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 9.0 (iOS9)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-09-18 15:55 UTC by microcircuitssigloxxi
Modified: 2015-09-23 15:51 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description microcircuitssigloxxi 2015-09-18 15:55:37 UTC
Using http://developer.xamarin.com/guides/cross-platform/xamarin-forms/localization/#iOS_Application_Project

   public System.Globalization.CultureInfo GetCurrentCultureInfo ()
  {
      var netLanguage = "en";
      if (NSLocale.PreferredLanguages.Length > 0) {
          var pref = NSLocale.PreferredLanguages [0];
          netLanguage = pref.Replace ("_", "-"); // turns es_ES into es-ES

          if (pref == "pt")
              pref = "pt-BR"; // get the correct Brazilian language strings from the PCL RESX
          //(note the local iOS folder is still "pt")
      }
      return new System.Globalization.CultureInfo(netLanguage);


Returns this error:

Culture name en-ES is not supported.
Parameter name: name
Comment 1 Sebastien Pouliot 2015-09-19 00:36:57 UTC
That code is not always correct when used with recent versions of iOS, c.c. Craig to fix the web documentation.

iOS supports almost any language in any region (it's tables are separate) so the string "en-ES" (i.e. using English in Spain) is perfectly legal in iOS API but this is not a supported culture in .NET (where the tables are merged).

For localization using the .NET API a better logic is to try to create the "full" (two parts) CultureInfo, e.g. "en-ES", and if it fails try again with only the first part, i.e. "en".

OTOH if you're looking for regional information then you should use RegionInfo in your code. This will get you close to what iOS provides using the .NET API.

Of course you can opt to use the iOS API directly but that makes it a bit harder to re-use code across platforms (and it's uncommon to find cases where .NET cannot localize an application correctly).
Comment 2 CraigD 2015-09-23 15:51:27 UTC
I've updated the method as follows:

		public System.Globalization.CultureInfo GetCurrentCultureInfo ()
		{
			var netLanguage = "en";
			var prefLanguageOnly = "en";
			if (NSLocale.PreferredLanguages.Length > 0) {
				var pref = NSLocale.PreferredLanguages [0];

				// HACK: Apple treats portuguese fallbacks in a strange way
				// https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/LocalizingYourApp/LocalizingYourApp.html
				// "For example, use pt as the language ID for Portuguese as it is used in Brazil and pt-PT as the language ID for Portuguese as it is used in Portugal"
				prefLanguageOnly = pref.Substring(0,2);
				if (prefLanguageOnly == "pt")
				{
					if (pref == "pt")
						pref = "pt-BR"; // get the correct Brazilian language strings from the PCL RESX (note the local iOS folder is still "pt")
					else
						pref = "pt-PT"; // Portugal
				}
				netLanguage = pref.Replace ("_", "-");
				Console.WriteLine ("preferred language:" + netLanguage);
			}

			// this gets called a lot - try/catch can be expensive so consider caching or something
			System.Globalization.CultureInfo ci = null;
			try {
				ci = new System.Globalization.CultureInfo(netLanguage);
			} catch {
				// iOS locale not valid .NET culture (eg. "en-ES" : English in Spain)
				// fallback to first characters, in this case "en"
				ci = new System.Globalization.CultureInfo(prefLanguageOnly);
			}
				
			return ci;
		}

Link to page: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/localization/#iOS_Application_Project
and to code: https://github.com/xamarin/xamarin-forms-samples/blob/master/UsingResxLocalization/iOS/Localize.cs#L29-L63