我必须制作多语言应用程序。当我将语言从英语转换为阿拉伯语时,应用程序的整个视图会发生变化,例如我的导航抽屉从左向右移动。但我想只翻译文字..
我已经写了这段代码。
if (language.equalsIgnoreCase("English")) {
languageToLoad = "en";
} else if (language.equalsIgnoreCase("Arabic")) {
languageToLoad = "ar";
}
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config,getResources().getDisplayMetrics());
答案 0 :(得分:1)
第1步:首先,您需要创建layout-ar和values-ar(这将包含您将放置阿拉伯字符串的字符串文件)
第2步:您需要将您的语言存储在SharedPreference中,我使用SharedPreferenceHelper类进行存储。
步骤a:获取语言
SharedPreferencesHelper sharedPreferencesHelper = SharedPreferencesHelper.getSharedInstance(this);
SharedPreferences sharedPreferences = sharedPreferencesHelper.getLanguage();
String lng = sharedPreferences.getString("language", "en");
b:存储语言
SharedPreferencesHelper sharedPreferencesHelperlang = SharedPreferencesHelper.getSharedInstance(this);
sharedPreferencesHelperlang.saveLanguage("en");
<强> SharedPreferencesHelper.java 强>
公共类SharedPreferencesHelper {
Context context;
public static String LANGUAGE = "language";
public static SharedPreferencesHelper sharedInstance;
public static SharedPreferencesHelper getSharedInstance(Context context){
if(sharedInstance == null){
sharedInstance = new SharedPreferencesHelper(context);
}
return sharedInstance;
}
public SharedPreferencesHelper(Context context){
this.context = context;
}
public void saveLanguage(String lng){
SharedPreferences.Editor editor = context.getSharedPreferences(LANGUAGE,context.MODE_PRIVATE).edit();
editor.putString("language",lng);
editor.commit();
}
public SharedPreferences getLanguage(){
SharedPreferences sharedPreferences = context.getSharedPreferences("language",context.MODE_PRIVATE);
return sharedPreferences;
}
}
步骤3:添加此类语言环境助手,它将通过共享首选项检查语言更改。
public class LocaleHelper {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static void onAttach(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
setLocale(context);
}
public static void onAttach(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
setLocale(context);
}
public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}
public static Context setLocale(Context context) {
/*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
updateResources(context, language);
}else {
updateResourcesLegacy(context, language);
}*/
// GetPreferencesDetail getPreferencesDetail = new GetPreferencesDetail(context);
// getPreferencesDetail.getAllDetails();
// String language = GetPreferencesDetail.LANGAUGE;
// persist(context, language);
SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(context);
SharedPreferences sharedPreferences = sharedPreferencesHelper.getLanguage();
String language = sharedPreferences.getString("language", "en");
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
if (Build.VERSION.SDK_INT >= 17) {
config.setLocale(locale);
context = context.createConfigurationContext(config);
} else {
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
}
return context;
}
private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
}
我认为应该这样做。