165

私のアプリは3つ(まもなく4つ)の言語をサポートします。いくつかのロケールは非常に似ているので、アプリケーションでロケールを変更するオプションをユーザーに提供したいと思います。たとえば、イタリア人は英語よりもスペイン語を好むかもしれません。

ユーザーがアプリケーションで使用可能なロケールから選択して、使用するロケールを変更する方法はありますか?基本クラスで実行するのは簡単なタスクなので、各アクティビティのロケールを設定することは問題ではないと思います。

4

15 に答える 15

190

この助けを願っています(onResumeで):

Locale locale = new Locale("ru");
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
      getBaseContext().getResources().getDisplayMetrics());
于 2011-02-13T19:54:34.533 に答える
26

Android OS N 以降を搭載したデバイスで、プログラムによるロケールの設定に問題がありました。私にとっての解決策は、ベースアクティビティにこのコードを書くことでした:

(基本アクティビティがない場合は、すべてのアクティビティでこれらの変更を行う必要があります)

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(updateBaseContextLocale(base));
}

private Context updateBaseContextLocale(Context context) {
    String language = SharedPref.getInstance().getSavedLanguage();
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return updateResourcesLocale(context, locale);
    }

    return updateResourcesLocaleLegacy(context, locale);
}

@TargetApi(Build.VERSION_CODES.N)
private Context updateResourcesLocale(Context context, Locale locale) {
    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);
    return context.createConfigurationContext(configuration);
}

@SuppressWarnings("deprecation")
private Context updateResourcesLocaleLegacy(Context context, Locale locale) {
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;
    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    return context;
}

ここでは呼び出すだけでは不十分であることに注意してください

createConfigurationContext(configuration)

また、このメソッドが返すコンテキストを取得してから、このコンテキストをメソッドに設定する必要がありますattachBaseContext

于 2017-06-15T15:10:44.990 に答える
16
@SuppressWarnings("deprecation")
public static void forceLocale(Context context, String localeCode) {
    String localeCodeLowerCase = localeCode.toLowerCase();

    Resources resources = context.getApplicationContext().getResources();
    Configuration overrideConfiguration = resources.getConfiguration();
    Locale overrideLocale = new Locale(localeCodeLowerCase);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        overrideConfiguration.setLocale(overrideLocale);
    } else {
        overrideConfiguration.locale = overrideLocale;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        context.getApplicationContext().createConfigurationContext(overrideConfiguration);
    } else {
        resources.updateConfiguration(overrideConfiguration, null);
    }
}

このヘルパー メソッドを使用して、特定のロケールを強制します。

UDPATE 2017 年 8 月 22 日。このアプローチを使用することをお勧めします。

于 2017-03-27T13:25:37.200 に答える
4

API16 から API28 で有効です。このメソッドを次の場所に配置するだけです。

Context newContext = context;

Locale locale = new Locale(languageCode);
Locale.setDefault(locale);

Resources resources = context.getResources();
Configuration config = new Configuration(resources.getConfiguration());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    config.setLocale(locale);
    newContext = context.createConfigurationContext(config);
} else {
    config.locale = locale;
    resources.updateConfiguration(config, resources.getDisplayMetrics());
}

return newContext;

以下を使用して、すべてのアクティビティにこのコードを挿入します。

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(localeUpdateResources(base, "<-- language code -->"));
    }

または、新しいコンテキストが必要なフラグメント、アダプターなどで localeUpdateResources を呼び出します。

クレジット: Yaroslav Berezanskyi

于 2019-05-18T14:00:06.813 に答える
2
 /**
 * Requests the system to update the list of system locales.
 * Note that the system looks halted for a while during the Locale migration,
 * so the caller need to take care of it.
 */
public static void updateLocales(LocaleList locales) {
    try {
        final IActivityManager am = ActivityManager.getService();
        final Configuration config = am.getConfiguration();

        config.setLocales(locales);
        config.userSetLocale = true;

        am.updatePersistentConfiguration(config);
    } catch (RemoteException e) {
        // Intentionally left blank
    }
}
于 2018-05-09T10:07:31.397 に答える