Skip to content

Instantly share code, notes, and snippets.

@ChaseFlorell
Created August 31, 2016 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChaseFlorell/8341e126e234aa11afdca330785c01ed to your computer and use it in GitHub Desktop.
Save ChaseFlorell/8341e126e234aa11afdca330785c01ed to your computer and use it in GitHub Desktop.
Xamarin Forms Localization
// iOS
public class Localize : ILocalize
{
private static CultureInfo _currentCulture;
public CultureInfo GetCurrentCultureInfo()
{
if (_currentCulture != null)
{
return _currentCulture;
}
var netLanguage = "en";
if (NSLocale.PreferredLanguages.Length > 0)
{
var pref = NSLocale.PreferredLanguages[0];
if (pref == "pt")
pref = "pt-BR"; // get the correct Brazilian language strings from the PCL RESX
//(note the local iOS folder is still "pt")
netLanguage = pref.Replace("_", "-"); // turns es_ES into es-ES
}
if (CultureInfo.GetCultures(CultureTypes.AllCultures).Any(x => x.Name == netLanguage))
{
return _currentCulture = new CultureInfo(netLanguage);
}
return _currentCulture = new CultureInfo("en-US");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment