アプリケーションから言語を変更して電話の言語を変更する方法はありますか。
つまり、アプリケーションの言語を変更すると、デフォルトの電話言語も変更されます。
これについて何か考えはありますか?ここで共有してください。
前もって感謝します。
アプリケーションから言語を変更して電話の言語を変更する方法はありますか。
つまり、アプリケーションの言語を変更すると、デフォルトの電話言語も変更されます。
これについて何か考えはありますか?ここで共有してください。
前もって感謝します。
Locale locale = new Locale("en_US");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);
それから
日本語は res/values-ja 、アラビア語は res/values-ar にフォルダを作成します。
そしてstring.xmlファイルを作成し、レイアウトに必要な言語を配置します
アラビア語の res/values-ar の例--
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string name="spinner_label">حسب</string>
<string name="app_name">فرق</string>
<string name="search">بحث:</string>
</resource>
プログラムで変更できるかどうかはわかりませんが、アプリの言語を変更した後、ユーザーにデバイスの言語も変更するように依頼できます。
デバイスの言語を変更するようユーザーに依頼する
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.LanguageSettings");
startActivity(intent);
アプリの言語を変更する
<activity
android:name=".ui.SomeActivity"
android:configChanges="locale"
:
:
</activity>
public static void setLanguage(Context context, String languageToLoad) {
Log.d(TAG, "setting language");
Locale locale = new Locale(languageToLoad); //e.g "sv"
Locale systemLocale = SystemLocale.getInstance().getCurrentLocale(context);
if (systemLocale != null && systemLocale.equals(locale)) {
Log.d(TAG, "Already correct language set");
return;
}
Locale.setDefault(locale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
Log.d(TAG, "Language set");
}
AndroidManifect ファイルのアクティビティ宣言にandroid:configChanges="locale"を追加します。
次に、そのアクティビティの onCreate から次のメソッドを呼び出します。
public static void setLanguage(Context context, String languageToLoad) {
Log.d(TAG, "setting language");
Locale locale = new Locale(languageToLoad); //e.g "sv"
Locale systemLocale = SystemLocale.getInstance().getCurrentLocale(context);
if (systemLocale != null && systemLocale.equals(locale)) {
Log.d(TAG, "Already correct language set");
return;
}
Locale.setDefault(locale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());
Log.d(TAG, "Language set");
}