私のアプリは3つ(まもなく4つ)の言語をサポートします。いくつかのロケールは非常に似ているので、アプリケーションでロケールを変更するオプションをユーザーに提供したいと思います。たとえば、イタリア人は英語よりもスペイン語を好むかもしれません。
ユーザーがアプリケーションで使用可能なロケールから選択して、使用するロケールを変更する方法はありますか?基本クラスで実行するのは簡単なタスクなので、各アクティビティのロケールを設定することは問題ではないと思います。
私のアプリは3つ(まもなく4つ)の言語をサポートします。いくつかのロケールは非常に似ているので、アプリケーションでロケールを変更するオプションをユーザーに提供したいと思います。たとえば、イタリア人は英語よりもスペイン語を好むかもしれません。
ユーザーがアプリケーションで使用可能なロケールから選択して、使用するロケールを変更する方法はありますか?基本クラスで実行するのは簡単なタスクなので、各アクティビティのロケールを設定することは問題ではないと思います。
この助けを願っています(onResumeで):
Locale locale = new Locale("ru");
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
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
。
@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 日。このアプローチを使用することをお勧めします。
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
/**
* 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
}
}