【问题标题】:Locale Changing within app is not working在应用程序内更改区域设置不起作用
【发布时间】:2017-09-13 15:44:45
【问题描述】:

我正在尝试更改应用程序的语言,但没有发生。我尝试了 SO 中几乎所有可用的解决方案,但没有一个奏效。有趣的事实是大多数代码都能够更改 Facebook Account Kit 活动的语言,但其他活动的语言没有改变。下面是我的应用程序类和清单文件。

public class MyApplication extends Application {


    private Locale locale = null;

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        if (locale != null)
        {
            newConfig.locale = locale;
            Locale.setDefault(locale);
            getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
        }
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        Configuration config = getBaseContext().getResources().getConfiguration();

        new SharedPrefManager(this).setLanguage("fr");
        String lang = new SharedPrefManager(this).getLanguage();

        locale = new Locale(lang);
        Locale.setDefault(locale);
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        AccountKit.initialize(getApplicationContext());
    }


}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.pullerr.pullerrdriver">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>
    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data android:name="com.facebook.accountkit.ApplicationName"
            android:value="@string/app_name" />
        <meta-data android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/FACEBOOK_APP_ID" />
        <meta-data android:name="com.facebook.accountkit.ClientToken"
            android:value="@string/ACCOUNT_KIT_CLIENT_TOKEN" />
        <meta-data android:name="com.facebook.accountkit.FacebookAppEventsEnabled"
            android:value="false"/>

        <activity
            android:name="com.facebook.accountkit.ui.AccountKitActivity"
            android:theme="@style/AppLoginTheme"
            tools:replace="android:theme"/>
        <activity
            android:configChanges="layoutDirection|locale"
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAINACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:configChanges="layoutDirection|locale"
            android:name=".LoadDetails"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.LOADDETAILS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:configChanges="layoutDirection|locale"
            android:name=".Login"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:configChanges="layoutDirection|locale"
            android:name=".Register"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.REGISTER" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <service android:name="services.LocationService"/>
    </application>

</manifest>

【问题讨论】:

  • 它不会随着语言环境而自动更改,您需要在 string.xml 中添加您的单词
  • 你能给我演示一下吗?
  • 为您要添加翻译字符串文件的语言创建一个新的值文件夹,例如,如果您需要印地语在 res 中创建一个新文件夹,如“values-hi”并在其中添加一个字符串文件这是印地语的默认翻译
  • 是的,我在 Github 上看到一个项目就是这样做的。字符串的名称应该是什么。我的意思是 My App 这意味着每个文本都必须在 strings.xml 文件中提及?可以在任何地方硬编码吗?
  • 检查你会理解的链接

标签: java android locale


【解决方案1】:
【解决方案2】:

您可以如下更改您的语言 (i18N):

public static void changeLanguage(String language, Context context) {
    try {
        if ( null != language) {
            Locale locale = new Locale(language); 
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
        }

        Log.i(TAG, "Language changed to " + language);
    } catch (Exception e) {
        Log.e(TAG, "[changeLanguage]" + e.getMessage());
    }

}

我在GitHub 上有一个开源项目。自己开车去https://github.com/configurer/easy-screen-lock/blob/master/EasyScreenLock/src/com/configurer/easyscreenlock/utils/LanguageUtils.java

【讨论】:

  • 我使用类似的代码。在我的 andrid-4.4 上,只有在调用changeLanguage(..) 之前调用super.onCreate()时它才有效
  • 是的,需要重新启动应用程序才能更改区域设置。
猜你喜欢
  • 2011-01-16
  • 1970-01-01
  • 2023-04-02
  • 2013-01-01
  • 1970-01-01
  • 2019-03-22
  • 1970-01-01
  • 2019-08-11
  • 2015-03-20
相关资源
最近更新 更多