Quick and Easy localization with Xamarin

Localization is a very good option an app can provide to users to increase the UX. Most apps from enterprise level to the very basic level, localization is very impotent if we need to target more audience with variety of cultures. Microsoft .NET framework provides us a concept called Satellite Assemblies they contains resource files for several cultures and enable developers easy to localize apps. Xamarin also provides this facility, we don’t need to do anything new but the same way to localize apps in .NET framework works for Xamarin too.

Blog images app in FR
In order to localize the apps first we need to create resource files with the respective cultures (“en-US”, “fr-FR”, “ru-RU”, “de”, “sv and etc.. Get the full list here).

The resource files maintain a naming convention that goes like [resource_file_name].[language_culture_name].resx. For instance say, I have a resource file named “Lang” and the US English culture name will come as “Lang.en-US.resx and the French culture for the same file will come as”Lang.fr-FR.resx

eng resx

fr resx

The above two are the files I have created for this demo. You can notice “Name” property in both files remain same and have the translation for respective languages in values.
Now the coding part starts and we have to define these 2 (and optional 2 comments) lines as the fields in the Main class (or whatever classes we are going to localize

1
2
3
4
// Loading the resurce files containing culture info and set culture to "ru-RU"
string sLangCode = "ru-RU";
ResourceManager resxManager = new ResourceManager("App2.Lang", Assembly.GetExecutingAssembly());
//Loaded the culture info

In line 2. I set the culture information (you can get more cultures from the table I have givend the link above in the post) and in Line 3, ResourceManager("App2.Lang", I call the resource with [Project_Name].[Resource_name] and notice I didnt put the culture identifier here.

Finally I can put the below lines to load the respective culture in the page creation method,

1
2
3
4
5
6
7
8
// Setting the language accordin to Language 
// Set culture by changing sLangCode
Thread.CurrentThread.CurrentUICulture = new CultureInfo(sLangCode);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sLangCode);
// End set culture info

button.Text = resxManager.GetString("TextInButton");
text.Text = resxManager.GetString("TextInTextbox");

So the final code will be something like,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Threading;

namespace App2
{
    [Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 1;

        // Loading the resurce files containing culture info and set culture to "ru-RU"
        string sLangCode = "ru-RU";
        ResourceManager resxManager = new ResourceManager("App2.Lang", Assembly.GetExecutingAssembly());
        //Loaded the culture info

        protected override void OnCreate(Bundle bundle)
        {
            


            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            EditText text = FindViewById<EditText>(Resource.Id.editText1);

            // Setting the language accordin to Language 
            // Set culture by changing sLangCode
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(sLangCode);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sLangCode);
            // End set culture info

            button.Text = resxManager.GetString("TextInButton");
            text.Text = resxManager.GetString("TextInTextbox");

            button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
        }
    }
}

for Android.

Happy coding 🙂

Advertisements

4 thoughts on “Quick and Easy localization with Xamarin

    1. I didn’t have a sample project (I cant find the project I have created for this post either since its done 2 years before). But I have explained the steps in details. Try them and let me know any unclear things. I am always happy to help 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s