有没有办法在应用程序运行时更改语言 [英] Is there way to change language while app running

查看:24
本文介绍了有没有办法在应用程序运行时更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家.:) 我有一个多布局的应用程序,我试图在运行应用程序时更改语言.我发现此代码可以帮助我更改语言

everyone. :) I have App with a multi-layout and I'm trying to change the language while running the app. I find this code to help me change the language

using Android.Content;
using Android.OS;

using Java.Util;

namespace RuntimeAppLanguage
{
    internal class LanguageManager
    {
        private const string MYLANGUAGE = "myLanguage";
        private const string MYPREF = "myPreference";

        public static Context LoadLanguage(Context context)
        {
            var loadedLanguage = GetLanguage(context, Locale.Default.Language);
            return ChangeLanguage(context, loadedLanguage);
        }

        public static Context ChangeLanguage(Context context, string language)
        {
            SaveLanguage(context, language);
            if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
            {
                return ChangeForAPI24(context, language);
            }
            return ChangeForLegacy(context, language);
        }

        private static string GetLanguage(Context context, string Language)
        {
            var privatePreference = context.GetSharedPreferences(MYPREF, FileCreationMode.Private);
            return privatePreference.GetString(MYLANGUAGE, Language);
        }

        private static void SaveLanguage(Context context, string language)
        {
            var privatePreference = context.GetSharedPreferences(MYPREF, FileCreationMode.Private);
            var editor = privatePreference.Edit();
            editor.PutString(MYLANGUAGE, language);
            editor.Apply();
        }

        private static Context ChangeForAPI24(Context context, string language)
        {
            // for api >= 24
            var locale = new Locale(language);
            Locale.Default = locale;
            var configuration = context.Resources.Configuration;
            configuration.SetLocale(locale);
            configuration.SetLayoutDirection(locale);

            return context.CreateConfigurationContext(configuration);
        }

        private static Context ChangeForLegacy(Context context, string language)
        {
            var locale = new Locale(language);
            Locale.Default = locale;
            var resources = context.Resources;
            var configuration = resources.Configuration;
            configuration.Locale = locale;
            resources.UpdateConfiguration(configuration, resources.DisplayMetrics);
            return context;
        }
    }
}

我正试图让它在这个布局下工作,但我最终得到了循环:(

and I'm trying to make it work at this layout but I end up with loop :(

我想要做的是,当用户选中英语时,阿拉伯语开关转为取消选中等等.

what I'm trying to do is when the user checks English the Arabic switch turn to uncheck and so on.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="386.5dp"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        android:background="#ffb39ddb">
        <Button
            android:text="Back"
            android:layout_width="80.0dp"
            android:layout_height="match_parent"
            android:id="@+id/back"
            android:background="#ffb39ddb"
            android:drawableLeft="@drawable/arrowangle"
            android:textSize="18dp" />
        <TextView
            android:text="Languagus"
            android:layout_width="222.0dp"
            android:textColor="#FF010101"
            android:textSize="20dp"
            android:layout_height="match_parent"
            android:id="@+id/textView1"
            android:gravity="center"
        />
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="134.5dp"
        android:id="@+id/linearLayout2"
        android:layout_marginTop="0.0dp"
        android:layout_below="@id/linearLayout1"
        android:background="#ffbdbdbd"
        >
        <Switch
            android:layout_width="match_parent"
            android:layout_height="59.0dp"
            android:id="@+id/switch1"
            android:background="#ffffffff"
            android:layout_marginTop="5dp"
            android:text="  English"
            android:textSize="25dp"
            android:textStyle="normal"
            android:checked="false" />
        <Switch
            android:layout_width="match_parent"
            android:layout_height="59.0dp"
            android:id="@+id/switch2"
            android:background="#ffffffff"
            android:layout_marginTop="5dp"
            android:text="  Arabic"
            android:textSize="25dp" />
    </LinearLayout>
</RelativeLayout>

请帮帮我:(

推荐答案

我用中文"、英文"和泰国"写了一个简单的样例,是否满足你的需求:

i write a simple sample with "Chinese","English" and "Thailand",whether it meets your needs:

1.LanguageManager类,代码同上.

2.MainActivity,其中包括一个 TextView 和一个 Button;

2.MainActivity which includes a TextView and a Button;

3.在BaseActivity

public class BaseActivity: AppCompatActivity
{
    protected override void AttachBaseContext(Context @base)
    {
        base.AttachBaseContext(LanguageManager.LoadLanguage(@base));
    }

}

4.在SettingActivity里面可以设置语言,类似你的axml

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 包括每种语言的strings

5.create values-en, values-th which include strings for each language

  • a.值/字符串

  • 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

  • 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

  • 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:Text的所有内容都应该使用@string/***,并且每种语言在values/string中使用相同的名称>

    ps: all the content of Text should use @string/*** and each language use the same name in the values/string

    喜欢这个效果:

    这篇关于有没有办法在应用程序运行时更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆