Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@matthewrdev
Last active April 2, 2018 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matthewrdev/cfa89d7a6901ceef52ea3332f6dbae1a to your computer and use it in GitHub Desktop.
Save matthewrdev/cfa89d7a6901ceef52ea3332f6dbae1a to your computer and use it in GitHub Desktop.
iOS - Localise.cs
using System;
using System.Globalization;
using System.Threading;
using Foundation;
using MyApp.il8n;
using MyApp.iOS;
using Xamarin.Forms;
[assembly: Dependency(typeof(Localize))]
namespace MyApp.iOS
{
public class Localize : ILocalize
{
public 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("_", "-");
}
// this gets called a lot - try/catch can be expensive so consider caching or something
CultureInfo ci = null;
try
{
ci = new 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 CultureInfo(prefLanguageOnly);
}
return ci;
}
public void SetLocale()
{
var iosLocaleAuto = NSLocale.AutoUpdatingCurrentLocale.LocaleIdentifier;
var netLocale = iosLocaleAuto.Replace("_", "-");
CultureInfo ci;
try
{
ci = new CultureInfo(netLocale);
}
catch
{
ci = GetCurrentCultureInfo();
}
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment