应用Android Studio中的语言未更改 [英] Language not changing in app Android Studio
问题描述
因此,基本上我有一个微调器,可以在三种语言之间进行选择,这是代码,我的语言确实发生了变化,但似乎正在加载该语言,而不仅仅是我为其设置的资源.
So basically i have a spinner that chooses between three languages, here's the code, my language does change but it appears it is loading the language just not the resources i have set for it.
我的代码
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());
}
一种语言strings.xml文件
One of the languages strings.xml file
<?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>
推荐答案
创建一个将所有活动的语言设置为一个的应用程序类
Create One Application Class which is set language to all activity
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());
}
}
LocalteUtils类用于在共享的首选项中设置所选语言,并默认设置英语
LocalteUtils Class is used to set the selected language in shared preferences and set the english language by default
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");
}
}
单击微调器选择的语言时,只需将语言设置为共享的首选项,然后重新启动活动即可
Onclick of spinner selected language just set language to shared preferences and restart activity like this
LocaleUtils.setSelectedLanguageId("fr");
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
startActivity(i);
使用应用程序实例最后将偏好语言设置为MainActivity.
Last set Preference language to MainActivity using Application Instance.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Application.getInstance().initAppLanguage(this);
setContentView(R.layout.activity_main);
}
在清单文件中添加应用程序
In manifest file Add Application
<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>
此代码在android pie中也可以正常工作. 编码愉快...
This code is working fine in android pie also. Happy Coding...
这篇关于应用Android Studio中的语言未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!