Localization of Xamarin iOS using .NET.

I am trying to use .NET localization in a portable class library for a Xamarin iOS / Android project. I have completed the following steps:

How to use localization in C #

And get a code that looks like this:

string sText = strings.enter_movie_name; Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr"); sText = strings.enter_movie_name; lblEnterMovieName.Text = sText; 

I also tried:

 ResourceManager resman = new ResourceManager(typeof(MyApplication.strings)); string sText = resman.GetString("enter_movie_name", new CultureInfo("fr")); 

I created strings.resx with enter_movie_name equal to "Enter movie name:" and strings.fr.resx equal to "Entre la movie name:"

However, sText always ends up as "Enter movie name:". I can't seem to get the version of "Entre la movie name:".

I also saw the message in Cross-platform localization , but could not process it. I also do not want to use a specific version of iOS under Localization on Xamarin.iOS , since I would like to be able to use my strings from a platform-independent library.

Can someone please indicate where I am going wrong?

+6
source share
3 answers

I created a slightly ugly solution, but it works. My portable class library (PCL) has a folder called "strings" inside which files are created:

  • strings.resx
  • strings_fr.resx
  • strings_ja_JP.resx

I installed all this as an Embedded Resource using a special tool like ResXFileCodeGenerator. This means that I have a separate resource DLL for each language.

In iOS, I can get the locale by calling:

 string sLocale = NSLocale.CurrentLocale.LocaleIdentifier; 

I would suggest that there is an Android equivalent using Locale , but I don't know what it is.

This gives me a string like "ja_JP" or "fr_FR" or "en_GB" (note that they underline, not dashes). Then I use this with the following static class that I created to get the appropriate resource manager and get a string from it.

If the locale aa_BB is specified, it first searches for strings_aa_BB, then for strings_aa, then for strings.

 public static class Localise { private const string STRINGS_ROOT = "MyPCL.strings.strings"; public static string GetString(string sID, string sLocale) { string sResource = STRINGS_ROOT + "_" + sLocale; Type type = Type.GetType(sResource); if (type == null) { if (sLocale.Length > 2) { sResource = STRINGS_ROOT + "_" + sLocale.Substring(0, 2); // Use first two letters of region code type = Type.GetType(sResource); } } if (type == null) { sResource = STRINGS_ROOT; type = Type.GetType(sResource); if (type == null) { System.Diagnostics.Debug.WriteLine("No strings resource file when looking for " + sID + " in " + sLocale); return null; // This shouldn't ever happen in theory } } ResourceManager resman = new ResourceManager(type); return resman.GetString(sID); } } 

An example of how this will be used (with reference to the code above) would be:

 string sText = Localise.GetString("enter_movie_name", sLocale); lblEnterMovieName.Text = sText; 

A significant drawback of this is that all presentations will have to have their own typing programmatically, but have growth potential, that translations can be performed once, and then reused on many platforms. They also remain separate from the main code in their own DLLs and therefore can be recompiled individually if necessary.

+7
source

I created a similar solution for the accepted answer, but using txt files instead of Resx and nuget is ready to work: https://github.com/xleon/I18N-Portable . Blog post here .

Other improvements:

  • "anyKey".Translate(); // from anywhere
  • Auto detect current crop
  • Get a list of supported translations: List<PortableLanguage> languages = I18N.Current.Languages;
  • Support for data binding within Mvvm / Xamarin.Forms
  • etc.
+3
source

You can use this app. Translation generator for uwp, xamarin, android strings.xml and JSON dictionary.

TTPS: //microsoft.com/en-us/p/multilingual-app-toolkit-app-localization/9pm98t5cx376

0
source

All Articles