简体   繁体   English

如何在纵向和横向模式下本地更改语言

[英]How to change language locally in portrait AND landscape mode

i have to implement an app prototype that should have the possibility to change the language inside the app. 我必须实现一个应用程序原型,该原型应该可以更改应用程序内部的语言。 My code: 我的代码:

...builder.setItems(langList, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            selectedLanguage = which;
            if (selectedLanguage != -1) {
                Configuration configuration = new Configuration(getResources().getConfiguration());
                String currentLanguage = configuration.locale.getLanguage();
                switch (selectedLanguage) {
                case 0:
                    if (currentLanguage.equalsIgnoreCase("de")) {
                        configuration.locale = Locale.ENGLISH;
                    } else {
                        return;
                    }
                    break;
                case 1:
                    if (currentLanguage.equalsIgnoreCase("en")) {
                        configuration.locale = Locale.GERMAN;
                    } else {
                        return;
                    }
                    break;

                default:
                    break;
                }
                getResources().updateConfiguration(configuration, null);
                Intent intent = new Intent(getActivity(), DashboardActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
            dialog.dismiss();
        }...

and it works nice. 而且效果很好。 But the problem is - as you change the display mode (portrait <=> landscape) loads the current system language (respectively strings.xml in values folder) again. 但是问题是-更改显示模式(纵向<=>横向)时,会再次加载当前系统语言(分别在values文件夹中的strings.xml )。 has anyone already experience with it, or maybe a suggestion how could I solve this problem? 有没有人已经使用过它,或者有建议我该如何解决这个问题?

thanks in advance.. 提前致谢..

you can when the user select its language save it in preferences and in oncreate method check if preferences have value for language or not if not load the default language 您可以在用户选择其语言时将其保存在首选项中,并在oncreate方法中检查首选项是否具有语言价值(如果未加载默认语言)

by default the change orientation will call the oncreate method so fell free to add checking code in it 默认情况下,更改方向将调用oncreate方法,因此可以随意在其中添加检查代码

You can know problematically when the orientation change happens because the onConfigurationChanged() lifecycle event happens. 您会疑惑地知道何时发生方向更改,因为发生了onConfigurationChanged()生命周期事件。

See the following example for how you can detect the orientation change. 有关如何检测方向变化的信息,请参见以下示例。 http://techblogon.com/android-screen-orientation-change-rotation-example/ http://techblogon.com/android-screen-orientation-change-rotation-example/

When the configuration changes you can store any values you need. 当配置更改时,您可以存储所需的任何值。 Please note that you can also store the values in a setting when they are first set. 请注意,您也可以在首次设置值时将其存储在设置中。

When creating the activity (onCreate()) you can read the orientation and set any appropriate values. 创建活动(onCreate())时,您可以读取方向并设置任何适当的值。

In summary, review the Activity lifecycle, there are relevant events there you can trigger on. 总而言之,回顾一下活动生命周期,可以触发一些相关事件。

so, I solved the problem using SharedPreferences : 所以,我使用SharedPreferences解决了这个问题:

... default:
                    break;
                }
                SharedPreferences preferences = getActivity().getSharedPreferences("SETTINGS_FILE", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("currentLanguage", configuration.locale.getLanguage());
                editor.commit();

                getResources().updateConfiguration(configuration, null); ...

then I implemented method setCurrentLanguage() in my MainActivity/MainFragment: 然后我在MainActivity / MainFragment中实现了方法setCurrentLanguage()

... protected void setCurrentLanguage() {
    Configuration configuration = new Configuration(getResources().getConfiguration());
    SharedPreferences preferences = getSharedPreferences("SETTINGS_FILE", Context.MODE_PRIVATE);
    String restoredLanguage = preferences.getString("currentLanguage", null);
    if (restoredLanguage.equalsIgnoreCase("en")) {
        configuration.locale = Locale.ENGLISH;
        getResources().updateConfiguration(configuration, null);
    }
} ...

and finnaly i call this method in every activity/fragment before setContentView(...) : 最后我在setContentView(...)之前的每个活动/片段中都调用此方法:

// in an activity
... protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setCurrentLanguage();
    setContentView(R.layout.activity_dashboard); ...
// or in a fragment
... public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    setCurrentLanguage();
    View rootView = inflater.inflate(R.layout.fragment_preferences,
            container, false); ...

Thanks again for your help ;) 再次感谢你的帮助 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM