Skip to content

Commit b2dbdb6

Browse files
committed
1 parent 328dccb commit b2dbdb6

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

UsingResxLocalization/iOS/Localize.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ public void SetLocale ()
2929
public System.Globalization.CultureInfo GetCurrentCultureInfo ()
3030
{
3131
var netLanguage = "en";
32+
var prefLanguageOnly = "en";
3233
if (NSLocale.PreferredLanguages.Length > 0) {
3334
var pref = NSLocale.PreferredLanguages [0];
3435

3536
// HACK: Apple treats portuguese fallbacks in a strange way
3637
// https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/LocalizingYourApp/LocalizingYourApp.html
3738
// "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"
38-
if (pref.Substring(0,2) == "pt")
39+
prefLanguageOnly = pref.Substring(0,2);
40+
if (prefLanguageOnly == "pt")
3941
{
4042
if (pref == "pt")
4143
pref = "pt-BR"; // get the correct Brazilian language strings from the PCL RESX (note the local iOS folder is still "pt")
@@ -45,7 +47,18 @@ public System.Globalization.CultureInfo GetCurrentCultureInfo ()
4547
netLanguage = pref.Replace ("_", "-");
4648
Console.WriteLine ("preferred language:" + netLanguage);
4749
}
48-
return new System.Globalization.CultureInfo(netLanguage);
50+
51+
// this gets called a lot - try/catch can be expensive so consider caching or something
52+
System.Globalization.CultureInfo ci = null;
53+
try {
54+
ci = new System.Globalization.CultureInfo(netLanguage);
55+
} catch {
56+
// iOS locale not valid .NET culture (eg. "en-ES" : English in Spain)
57+
// fallback to first characters, in this case "en"
58+
ci = new System.Globalization.CultureInfo(prefLanguageOnly);
59+
}
60+
61+
return ci;
4962
}
5063
}
5164
}

0 commit comments

Comments
 (0)