i write a simple sample with "Chinese","English" and "Thailand",whether it meets your needs:
1.the LanguageManager class,the code is the same as above.
2.MainActivity which includes a TextView and a Button;
3.in BaseActivity
public class BaseActivity: AppCompatActivity
{
protected override void AttachBaseContext(Context @base)
{
base.AttachBaseContext(LanguageManager.LoadLanguage(@base));
}
}
4.in SettingActivity which could set the language,the axml similar to yours
public class SettingActivity : BaseActivity, CompoundButton.IOnCheckedChangeListener
{
private Switch swCh;
private Switch swEn;
private Switch swTh;
private Bundle s;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.setting);
// Create your application here
initView();
}
private void initView()
{
Button back = FindViewById<Button>(Resource.Id.back);
back.Click += delegate { Finish(); };
swCh = FindViewById<Switch>(Resource.Id.switch1);
swEn = FindViewById<Switch>(Resource.Id.switch2);
swTh = FindViewById<Switch>(Resource.Id.switch3);
var s = GetSharedPreferences("myPreference", FileCreationMode.Private).GetString( "myLanguage", Locale.Default.Language);
switch (s)
{
case "ch":
swCh.Checked = true;
break;
case "en":
swEn.Checked = true;
break;
case "th":
swTh.Checked = true;
break;
}
swCh.SetOnCheckedChangeListener(this);
swEn.SetOnCheckedChangeListener(this);
swTh.SetOnCheckedChangeListener(this);
}
public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
{
if (isChecked)
{
switch (buttonView.Id)
{
case Resource.Id.switch1:
swEn.Checked = false;
swTh.Checked = false;
LanguageManager.ChangeLanguage(this, "ch");
break;
case Resource.Id.switch2:
swCh.Checked = false;
swTh.Checked = false;
LanguageManager.ChangeLanguage(this, "en");
break;
case Resource.Id.switch3:
swEn.Checked = false;
swCh.Checked = false;
LanguageManager.ChangeLanguage(this, "th");
break;
}
//restart application to change language
Intent intent = new Intent(this, typeof(MainActivity));
intent.SetFlags(ActivityFlags.ClearTask | ActivityFlags.NewTask);
StartActivity(intent);
}
}
}
5.create values-en, values-th which include strings
for each language
a.values/strings
<string name="change_language">改变语言</string>
<string name="setting">设置</string>
<string name="chinese">中文</string>
<string name="english">英语</string>
<string name="thailand">泰语</string>
b.values-en/strings
<string name="change_language">change language</string>
<string name="setting">setting</string>
<string name="chinese">chinese</string>
<string name="english">english</string>
<string name="thailand">thailand</string>
c.values-th/strings
<string name="change_language">???????????</string>
<string name="setting">??????</string>
<string name="chinese">??????</string>
<string name="english">??????</string>
<string name="thailand">?????????</string>
ps: all the content of Text should use @string/***
and each language use the same name in the values/string
like this effect:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…