Xamarin.Android - Android Localization

Introduction

The localization is using the alternate resources to the particular region target or locale. For the example, we might provide the localized language strings for the various countries in the input for the resources folder. Android is using the resources appropriate for the device's locale at the runtime without any code changes. For more details, refer here.

Let’s start

Step 1

Open Visual Studio->New Project->Templates->Visual C#->Android->Blank app.

Select Blank app. Give the Project Name and Project Location.

Step 2

Open Solution Explorer-> Project Name-> Resources. Create values folder for the various languages. Here, I created English, Tamil, Hindi, Spanish, Swedish.

Step 3

Open Solution Explorer-> Project Name-> Resources-> Layout-> Main.axml. Design the page for the language changes options. Here, I created Check box and textview.

Step 4

Open Solution Explorer-> Project Name-> Resources -> values-en. Declare the string id and the values for English. 

English

XML 

  1. <string name="TxtWelcome">Welcome to New Technology</string>   

Tamil

XML 

  1. <string name="TxtWelcome">புதிய தொழில்நுட்பத்திற்கு வரவேற்கிறோம் </string>   

Hindi

XML 

  1. <string name="TxtWelcome">नई प्रौद्योगिकी में आपका स्वागत है</string>   

Spanish

XML 

  1. <string name="TxtWelcome">Bienvenido a la nueva tecnología</string>   

Swedish

XML 

  1. <string name="TxtWelcome">Välkommen till ny teknik</string>   

Step 5

Open Solution Explorer-> Project Name->Resources-> MainActicity.cs and add namespace. 

  1. using Android.Util;  
  2. using Java.Util;   

Step 6

Declare the button and Checkbox variables. Afterwards, go to Oncreate() and give the code given below.

C# code 

  1. <policies>  
  2.     <inbound>  
  3.         <base />  
  4.         <cache-lookup vary-by-developer="false" vary-by-developer-groups="false">  
  5.             <vary-by-header>Accept</vary-by-header>  
  6.             <vary-by-header>Accept-Charset</vary-by-header>  
  7.         </cache-lookup>  
  8.         <rewrite-uri template="/resource" /> </inbound>  
  9.     <outbound>  
  10.         <base />  
  11.         <cache-store caching-mode="cache-on" duration="3600" /> </outbound>  
  12. </policies>static string LanguageCodevalue; CheckBox ChkEnglish, ChkTamil, ChkHindi, ChkSwidesh, ChkSpinesh; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Change locale settings in the app. Android.Content.Res.Resources res = this.Resources; DisplayMetrics Dm = res.DisplayMetrics; Android.Content.Res.Configuration conf = res.Configuration; if (LanguageCodevalue != null) { conf.SetLocale(new Locale(LanguageCodevalue)); } else { conf.SetLocale(new Locale("en")); //Default Language } res.UpdateConfiguration(conf, Dm); SetContentView(Resource.Layout.Main); ChkEnglish = FindViewById  
  13. <CheckBox>(Resource.Id.ChkEnglish); ChkTamil = FindViewById  
  14.     <CheckBox>(Resource.Id.ChkTamil); ChkHindi = FindViewById  
  15.         <CheckBox>(Resource.Id.ChkHindi); ChkSwidesh = FindViewById  
  16.             <CheckBox>(Resource.Id.ChkSwidesh); ChkSpinesh = FindViewById  
  17.                 <CheckBox>(Resource.Id.ChkSpinesh); ChkEnglish.Click += ChkEnglish_Click; ChkTamil.Click += ChkTamil_Click; ChkHindi.Click += ChkHindi_Click; ChkSwidesh.Click += ChkSwidesh_Click; ChkSpinesh.Click += ChkSpinesh_Click; }   

Step 7

After implementation Oncreate() to Create Checkbox, proceed, as shown below.

  1. private void ChkSpinesh_Click(object sender, System.EventArgs e) {  
  2.     ChkEnglish.Checked = false;  
  3.     ChkTamil.Checked = false;  
  4.     ChkHindi.Checked = false;  
  5.     ChkSwidesh.Checked = false;  
  6.     ChkSpinesh.Checked = true;  
  7.     LanguageCodevalue = "sp"//Spinesh  
  8.     this.Recreate(); //Refresh Current Activity  
  9. }  
  10. private void ChkSwidesh_Click(object sender, System.EventArgs e) {  
  11.     ChkEnglish.Checked = false;  
  12.     ChkTamil.Checked = false;  
  13.     ChkHindi.Checked = false;  
  14.     ChkSwidesh.Checked = true;  
  15.     ChkSpinesh.Checked = false;  
  16.     LanguageCodevalue = "sv"//Swidesh  
  17.     this.Recreate(); //Refresh Current Activity  
  18. }  
  19. private void ChkHindi_Click(object sender, System.EventArgs e) {  
  20.     ChkEnglish.Checked = false;  
  21.     ChkTamil.Checked = false;  
  22.     ChkHindi.Checked = true;  
  23.     ChkSwidesh.Checked = false;  
  24.     ChkSpinesh.Checked = false;  
  25.     LanguageCodevalue = "hi"//Hindi  
  26.     this.Recreate(); //Refresh Current Activity  
  27. }  
  28. private void ChkTamil_Click(object sender, System.EventArgs e) {  
  29.     ChkEnglish.Checked = false;  
  30.     ChkTamil.Checked = true;  
  31.     ChkHindi.Checked = false;  
  32.     ChkSwidesh.Checked = false;  
  33.     ChkSpinesh.Checked = false;  
  34.     LanguageCodevalue = "ta"//Tamil  
  35.     this.Recreate(); //Refresh Current Activity  
  36. }  
  37. private void ChkEnglish_Click(object sender, System.EventArgs e) {  
  38.     ChkEnglish.Checked = true;  
  39.     ChkTamil.Checked = false;  
  40.     ChkHindi.Checked = false;  
  41.     ChkSwidesh.Checked = false;  
  42.     ChkSpinesh.Checked = false;  
  43.     LanguageCodevalue = "en"//English  
  44.     this.Recreate(); //Refresh Current Activity  
  45. }   

Step 8

Press F5 or Build and Run the Application.

Finally, we successfully created Xamairn Android app.


Similar Articles