应用程序 Android Studio 中的语言未更改

Kre*_*eam 0 android locale

所以基本上我有一个可以在三种语言之间进行选择的微调器,这是代码,我的语言确实发生了变化,但它似乎正在加载语言,而不是我为其设置的资源。

我的代码

private void setLocale(String localeName){
            if (localeName.equalsIgnoreCase(""))
                return;
            Resources resources = getResources();
            Locale locale = new Locale(localeName);
            Locale.setDefault(locale);
            android.content.res.Configuration config = new
                    android.content.res.Configuration();
            config.locale = locale;
            resources.updateConfiguration(config, resources.getDisplayMetrics());
            //restart base activity
            this.finish();
            this.startActivity(this.getIntent());
        }
Run Code Online (Sandbox Code Playgroud)

语言之一的 strings.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Tabibi</string>
    <string name="already_have_an_account">????? ?????</string>
    <string name="join">????? ????</string>
    <string name="login">????? ??????</string>
    <string name="language">?????</string>
</resources>
Run Code Online (Sandbox Code Playgroud)

May*_*yur 7

创建一个应用程序类,为所有活动设置语言

public class Application extends android.app.Application {

    private static Application applicationInstance;

    public static synchronized Application getInstance() {
        return applicationInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        applicationInstance = this;
    }

    public void initAppLanguage(Context context) {
        LocaleUtils.initialize(context, LocaleUtils.getSelectedLanguageId());
    }

}
Run Code Online (Sandbox Code Playgroud)

LocalteUtils 类用于设置共享首选项中选择的语言,默认设置为英文

public class LocaleUtils {

    public static final String ENGLISH = "en";
    public static final String FRENCH = "fr";
    public static final String SPANISH = "es";


    public static void initialize(Context context, @LocaleDef String defaultLanguage) {
        setLocale(context, defaultLanguage);
    }

    public static boolean setLocale(Context context, @LocaleDef String language) {
        return updateResources(context, language);
    }

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

        return true;
    }

    @Retention(RetentionPolicy.SOURCE)
    @StringDef({ENGLISH, FRENCH, SPANISH})
    public @interface LocaleDef {
        String[] SUPPORTED_LOCALES = {ENGLISH, FRENCH, SPANISH};
    }


    private static SharedPreferences getDefaultSharedPreference(Context context) {
        if (PreferenceManager.getDefaultSharedPreferences(Application.getInstance().getApplicationContext()) != null)
            return PreferenceManager.getDefaultSharedPreferences(Application.getInstance().getApplicationContext());
        else
            return null;
    }

    public static void setSelectedLanguageId(String id){
        final SharedPreferences prefs = getDefaultSharedPreference(Application.getInstance().getApplicationContext());
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("app_language_id", id);
        editor.apply();
    }

    public static String getSelectedLanguageId(){
        return getDefaultSharedPreference(Application.getInstance().getApplicationContext())
                .getString("app_language_id", "en");
    }
}
Run Code Online (Sandbox Code Playgroud)

单击微调器选择的语言只需将语言设置为共享首选项并重新启动这样的活动

LocaleUtils.setSelectedLanguageId("fr");
        Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
        startActivity(i);
Run Code Online (Sandbox Code Playgroud)

最后使用应用程序实例将首选语言设置为 MainActivity。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Application.getInstance().initAppLanguage(this);
        setContentView(R.layout.activity_main);
}
Run Code Online (Sandbox Code Playgroud)

在清单文件中添加应用程序

<application
        android:name=".Application"
        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"
        tools:ignore="AllowBackup,GoogleAppIndexingWarning">
        <activity android:name=".Main2Activity"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
Run Code Online (Sandbox Code Playgroud)

这段代码在 android pie 中也能正常工作。快乐编码...