Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Localization in iOS not working properly

HortensiaMusetescuHortensiaMusetescu USMember
edited March 2016 in Xamarin.Forms

Dear All,

While trying to add translated strings to my iOS forms project, the translated strings will not display. I have done every step from the tutorials available so far.

The catch is that in my Output I get the language codes as es-US, ro-US, and so on. These are not valid language codes and the resource files do not contain them.

My question is: is this behavior normal? To return the language code as es-US?

This is in the iPhoneSimulator so far. Thank you!

Bellow is a code snipped for the GetCulture and SetLocale methods:

      public System.Globalization.CultureInfo GetCurrentCultureInfo()
      {
        var netLanguage = "en";
        var prefLanguageOnly = "en";
        if (NSLocale.PreferredLanguages.Length > 0)
        {
            var pref = NSLocale.PreferredLanguages[0];
            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);
        }
        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;
    }

     public void SetLocale()
    { 
            var ci = new CultureInfo (GetCurrentCultureInfo());
        Thread.CurrentThread.CurrentCulture = ci;
        Thread.CurrentThread.CurrentUICulture = ci;
    }

Answers

  • Still need some help with this guys ....

  • AllisonTaylorAllisonTaylor USUniversity

    Hello,
    I have noticed that NSLocale.PreferredLanguages contains specific culture values constructed using whatever combination of Language and Location you have set on the device. So if your Language is set to Spanish and your Location is set to United States, then yes, "es-US" is expected. But as you mentioned, this code could very well not exist in the list of .NET Framework culture values. What I do for RESX translations in this case is just use the language only portion (your value prefLanguageOnly). In other words, if the value is not in CultureTypes.AllCultures, I just use the 1st 2 chars for the translation. Hope this helps.

  • TonyDTonyD USMember ✭✭✭

    @HortensiaMusetescu

    We get this as well (various combos of culture). We had to do some hacky things to make it work. I am attaching a snippet below. I'm not proud of this but it worked:

    `public System.Globalization.CultureInfo GetCurrentCultureInfo ()
    {
    var netLanguage = "en";
    System.Globalization.CultureInfo result = new System.Globalization.CultureInfo ("en-us");
    if (NSLocale.PreferredLanguages.Length > 0) {
    var pref = NSLocale.PreferredLanguages [0];
    netLanguage = pref.Replace ("_", "-"); // turns pt_BR into pt-BR
    } else
    return result;

            bool bHadException = false;
            try
            {
                result = new System.Globalization.CultureInfo (netLanguage);
    
            }
            catch (Exception ex) {
                if (!(ex is System.Globalization.CultureNotFoundException)) {
                    App.Report (ex);
    
                }
                bHadException = true;
            }
    
            if (bHadException) {
                try
                {
                    bHadException = false;
                    string[] vParts = netLanguage.Split(new char[] {'-'});
                    if (vParts.Length == 2)
                    {
                        // de-US -> de-de
                        result = new System.Globalization.CultureInfo (vParts[0] + "-" + vParts[0]);
                    }
    
                }
                catch (Exception ex) {
                    if (!(ex is System.Globalization.CultureNotFoundException)) {
                        App.Report (ex);
    
                    }
                    bHadException = true;
                }
            }
    
            return result == null ? new System.Globalization.CultureInfo ("en-us") : result;
        }`
    
  • SteveShaw.5557SteveShaw.5557 USMember ✭✭✭
    edited September 2016

    FWIW, both the answers don't distinguish between Language (English, French) and Region or Culture (date and currency formatting).

    Language is CurrentUICulture in .Net, NSLocale.PreferredLanguages[0] in iOS.

    Region or Culture is CurrentCulture in .Net, NSLocale.CurrentLocale in iOS.

    For example, user could be using formatting appropriate for Germany, but speaking English.

    IMPLICATION: Just because my app lacks support for German language, so I show them English texts, would be no excuse to use the default en-US region when formatting dates for the user!

    (In Xamarin, CurrentCulture is not reflecting the device's CurrentLocale, at least on iOS simulator. This seems to be a Xamarin bug - both CurrentCulture and CurrentUICulture change when user changes language. So I examine the iOS objects directly, and construct .Net CultureInfo objects based on those.)

  • SteveShaw.5557SteveShaw.5557 USMember ✭✭✭
    edited September 2016

    Actually, it may be due to what iOS does: I just realized that CurrentLocale in this case is "en-SE". Not a standard combination for .NET.

    IMHO, Xamarin should only use the COUNTRY part of CurrentLocale, to determine CurrentCulture, as that corresponds to user's Region setting. Country="SE" would pick CurrentCulture="{sv-SE}". Language would only be used to set CurrentUICulture. In my example, Language="en" would pick CurrentUICulture="{en}".

    With this combination, it becomes possible to determine that date/number formatting should be for Sweden, but the language is English.

Sign In or Register to comment.