first thing to make note of is our solution, specifically under the Resources->Values folder namely the strings file
now if we open that file, we'll see the following
<resources>
<string name="app_name">Localization
example</string>
<string name="action_one">One</string>
<string name="action_two">Two</string>
<string name="action_three">Three</string>
<string name="action_four">Four</string>
<string name="value_userClicked">You
clicked the button {0}</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:orientation="vertical">
<Button a:id="@+id/ButtonOne" a:text="@string/action_one"
a:layout_width="fill_parent" a:layout_height="wrap_content"/>
<Button a:id="@+id/ButtonTwo" a:text="@string/action_two"
a:layout_width="fill_parent" a:layout_height="wrap_content"/>
<Button a:id="@+id/ButtonThree" a:text="@string/action_three"
a:layout_width="fill_parent" a:layout_height="wrap_content"/>
<Button a:id="@+id/ButtonFour" a:text="@string/action_four"
a:layout_width="fill_parent" a:layout_height="wrap_content"/>
</LinearLayout>
just to make things a bit interesting let's wire the apps up so that we get a toast message when we click our buttons.
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using System;
namespace pav.Localization
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view
from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
base.FindViewById<Button>(Resource.Id.ButtonOne).Click
+= Button_Click;
base.FindViewById<Button>(Resource.Id.ButtonTwo).Click
+= Button_Click;
base.FindViewById<Button>(Resource.Id.ButtonThree).Click
+= Button_Click;
base.FindViewById<Button>(Resource.Id.ButtonFour).Click
+= Button_Click;
}
private void
Button_Click(object sender, EventArgs e)
{
var self = sender as Button;
var msg = base.Resources.GetString(Resource.String.value_userClicked);
var toast = String.Format(msg, self.Text);
Toast.MakeText(this,toast, ToastLength.Short).Show();
}
}
}
now it would be accurate to conclude that if we configured our application to get it's text data from a strings file, then all we'd need is string files for all of our various supported languages to create a multi lingual application.
so let's add a values-fr folder with it's own strings.xml file
and let's open up that strings file and create a french version
<resources>
<string name="app_name">Exemple
de localisation</string>
<string name="action_one">Un</string>
<string name="action_two">Deux</string>
<string name="action_three">Trois</string>
<string name="action_four">Quatre</string>
<string name="value_userClicked">Vous avez
cliqué sur le bouton {0}</string>
</resources>
and voila
our application is not multi lingual, now let's do one last trick and that's let's internationalize some images. so let's add two folders into our solution explorer drawable and drawable-fr; in those folder let's add to images both named flag.png one a UK flag and the other a french flag.
now let's put an ImageView onto our axml file which whose src attribute points to our drawable->flag file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:orientation="vertical">
<Button
a:id="@+id/ButtonOne"
a:text="@string/action_one"
a:layout_width="fill_parent"
a:layout_height="wrap_content" />
<Button
a:id="@+id/ButtonTwo"
a:text="@string/action_two"
a:layout_width="fill_parent"
a:layout_height="wrap_content" />
<Button
a:id="@+id/ButtonThree"
a:text="@string/action_three"
a:layout_width="fill_parent"
a:layout_height="wrap_content" />
<Button
a:id="@+id/ButtonFour"
a:text="@string/action_four"
a:layout_width="fill_parent"
a:layout_height="wrap_content" />
<ImageView
a:src="@drawable/flag"
a:layout_width="match_parent"
a:layout_height="wrap_content" />
</LinearLayout>