Bug 31897 - NSLocale.PreferredLanguages[0] returning 'pt' instead of 'pt-BR' on iOS
Summary: NSLocale.PreferredLanguages[0] returning 'pt' instead of 'pt-BR' on iOS
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 5.2
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-07-13 16:55 UTC by Eder
Modified: 2015-07-15 14:39 UTC (History)
8 users (show)

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:

Description Eder 2015-07-13 16:55:53 UTC
I'm trying to set up the CultureInfo of my Resouce file to archive localization but the class Foundation.NSLocale.PreferredLanguages[0] is returning the value 'pt' instead of 'pt-BR' while runing this sample  on iOS simulator;

Using Visual Studio 2015 RC + 
Windows 10 Build 10162 + 

Follow the sample to reproduce the issue: 


Steps to reproduce:

1 - Open the iOS simulator (iphone 4 for example)
2 - Before running the application go to settings-> General-> Language & Region-> iPhone Language
and change it to 'Português (Brasil)'. Hit "done" and then "Change to Portuguese (Brazil)" option.

3 - Set a breakpoint on line 20 of file Xamarin.Localize.pt_BR.Bug.iOS.Localize and check the value returned.
it was supposed to be 'pt-BR' not just 'pt';
Comment 1 Eder 2015-07-13 16:58:53 UTC
4 - Run the application on iOS simulator a see the issue;
Comment 2 Eder 2015-07-14 14:52:58 UTC
Just find out that NSLocale.AutoUpdatingCurrentLocale.LocaleIdentifier is correctly returning the value 'pt_BR' on iOS. 
Maybe it is more useful to update de xamaring documentation regarding to iOS Localization (http://developer.xamarin.com/guides/cross-platform/xamarin-forms/localization/) to use NSLocale.AutoUpdatingCurrentLocale.LocaleIdentifier instead of NSLocale.PreferredLanguages [0]
Comment 3 Rolf Bjarne Kvinge [MSFT] 2015-07-15 12:55:06 UTC
Our bindings are just a thin wrapper around the iOS API, which means that if NSLocale.PreferredLanguages do not return the value you expect, you'll have to ask in Apple forums / file bugs with Apple about it.

That said, I believe NSLocate.PreferredLanguages returns the *language*, not the *locale* (i.e. in Brazil Portuguese the language), while NSLocale.AutoUpdatingCurrentLocale returns the *locale* (which is what you want).

I'll send a note to the documentation team to check if it makes sense to update the docs.
Comment 4 CraigD 2015-07-15 14:10:28 UTC
Locale is different (number formatting, etc) and can be set independent of language.

Regarding this feedback:

>it was supposed to be 'pt-BR' not just 'pt';

From Apple's doc

it says "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"

I cannot explain _why_ Apple has implemented it this way, but NSLocate.PreferredLanguages[0] is definitely the correct property to check... and for some reason, they use "pt" for Brazil and "pt-PT" for Portugal.

I have updated the Xamarin.Forms sample
to properly display Portuguese (in Brazil and Portugal). The iOS.Localize class now contains this code

	if (pref == "pt")
		pref = "pt-BR"; // get the correct Brazilian language strings from the PCL RESX (note the local iOS folder is still "pt")
		pref = "pt-PT"; // Portugal

and the Resources folders are named correctly so that iOS loads the images and strings files for Brazil and Portugal.
Comment 5 Eder 2015-07-15 14:39:46 UTC
Thank you guys.