【问题标题】:How can I change language of whole application by only single click?如何通过单击更改整个应用程序的语言?
【发布时间】:2017-09-03 15:47:54
【问题描述】:

我在登录页面上有一个Spinner,可以选择三种语言。我希望当用户选择一种语言时,假设 "Persian" 语言应该改变整个应用程序的语言。我只能更改当前Activity 的语言。如何更改整个应用程序的语言。

【问题讨论】:

    标签: android android-studio android-activity android-spinner


    【解决方案1】:

    在 Android 中以编程方式更改语言

    以下是更改应用程序区域设置的适当方法:

    要以编程方式更改语言,您必须克服一些困难。

    1.)在配置更改期间关闭或重新创建应用程序后,您的应用程序将不记得您的语言更改

    2.)您应该根据所选语言正确更新当前可见的 UI

    解决方案

    LocaleHelper”是您所需要的解决方案。您只需在应用程序的主类上初始化语言环境。之后,您的所有语言更改都将保留。

    在 Android API 版本 24(牛轧糖)最近发生变化后,我们需要重写 attachBaseContext 以反映变化。

    以下用于更改应用程序语言的方法:

    private static boolean updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);
    
        Resources resources = context.getResources();
    
        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
    
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    
        return true;
    }
    

    在下面的链接中找到更多详细信息:

    Changing Android’s Locale Programmatically

    【讨论】:

    • 我是否需要在每个活动中调用此方法?
    • 不止一次,但您需要处理每个语言值文件夹中的所有字符串资源。
    • 谢谢@Chetan 有用的回答。
    • 这种设置用户区域设置的解决方法是否可以在应用程序类之外工作(我需要在活动中使用它)。我的意思是,如果我将在当前活动中切换到另一个语言环境并调用:getResources().updateConfiguration()。它会立即应用更改吗?或者我只需要重新创建这个 Activity,或者我需要杀死应用程序并重新启动它,同时获取以前保存到共享首选项的区域设置?
    • 当前活动已重新启动,其他活动的区域设置已更改,因此当您移动到其他活动时,它会从选定的语言值文件夹中获取新的字符串值。
    【解决方案2】:

    所以这是我这边的。它非常适合我。

    按照这些步骤更改语言。

    1.将字符串保存在 values-ur(URDU) 等文件夹中

    将所有字符串转换为您必须转换的语言

    2.创建一个 BaseActivity 类并将所有其余的 Activity 扩展到该 Base 类

    public class BaseActivity extends AppCompatActivity
    {
        public BaseActivity context;
        public MySharePreference mySharePreference;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            context = BaseActivity.this;
            mySharePreference = MySharePreference.getInstance(context);
            LocaleHelper.setLocale(context, mySharePreference.getLanguage() );
        }
    
        protected void attachBaseContext(Context base)
        {
            super.attachBaseContext(LocaleHelper.onAttach(base));
        }
    
    } 
    

    3.我有 ExampleActivity,我想翻译它的语言。

    public class ExampleActivity extends BaseActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_example);
        }
    }
    

    只需将您的应用程序扩展到您的 BaseActivity 类。

    3.创建一个LocalHelper类,在其中改变方向和字符串。

    public class LocaleHelper
    {
    
        public static Context setLocale(Context context, String language)
        {
            Locale locale = new Locale(language);
            Locale.setDefault(locale);
    
            Resources resources = context.getResources();
    
            Configuration configuration = new Configuration(resources.getConfiguration());
            configuration.setLayoutDirection(locale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            {
                configuration.setLocale(locale);
                LocaleList localeList = new LocaleList(locale);
                LocaleList.setDefault(localeList);
                configuration.setLocales(localeList);
            }
            else
            {
                configuration.locale = locale;
                configuration.setLocale(locale);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1)
            {
                return context.createConfigurationContext(configuration);
            }
            else
            {
                resources.updateConfiguration(configuration, resources.getDisplayMetrics());
                return context;
            }
        }
    
        public static Context onAttach(Context context)
        {
            return setLocale(context, MySharePreference.getInstance(context).getLanguage());
        }
    } 
    

    4.在您的 App 类中附加基本上下文

    public class AppClass extends MultiDexApplication
    {
    
        private static AppClass appClass;
        public static AppClass getintance()
        {
            return appClass;
        }
    
    
        @Override
        public void onCreate()
        {
            super.onCreate();
            appClass = this;
            MySharePreference.getInstance(this);
        }
    
    
        @Override
        protected void attachBaseContext(Context base)
        {
            super.attachBaseContext(LocaleHelper.onAttach(base));
        }
    
    }
    

    5.在您的 build.gradle 文件中添加最新的 androidx SharedPreferences 依赖项。

    implementation 'androidx.preference:preference:1.1.1'
    

    6.在我的例子中,我创建了自己的 SharedPreferences 类。

    public class MySharePreference
    {
        private static MySharePreference instance;
        private static SharedPreferences pref;
    
        private MySharePreference(Context context)
        {
            if (context != null)
            {
                pref = PreferenceManager.getDefaultSharedPreferences(context);
            }
            else
            {
                pref = PreferenceManager.getDefaultSharedPreferences(App.getintance());
            }
        }
    
        public static MySharePreference getInstance(Context context)
        {
            if (instance == null || pref == null)
            {
                instance = new MySharePreference(context);
            }
            return instance;
        }
    
    
        public String getLanguage()
        {
            return pref.getString("appLanguage", "en");
        }
    
        public void setLanguage(String b)
        {
            pref.edit().putString("appLanguage", b).apply();
        }
    }
    

    7.在我有微调器的情况下,最后将该语言的缩写保存到 SharedPreferences。

    spinner_lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
            {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
                {
                    if (mySharePreference.getLanguagePosition() != position)
                    {
                        mySharePreference.setLanguage(Constants.COUNTRY_LIST.get(position).countryAbbr);
                        mySharePreference.setLanguagePosition(position);
                        startActivity(new Intent(mActivity, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                    }
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parent)
                {
    
                }
            });
    

    现在一切都完成了......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      相关资源
      最近更新 更多