我的应用程序具有将应用程序语言更改为英语,阿拉伯语或俄语的功能,此应用程序具有不同的产品口味,即Flavor1,Flavour2。每个风味应用程序都具有此选项,可以将应用程序语言转换为英语,阿拉伯语,俄语。但是,当我从选项中更改语言时,它只是更改了从右到左的阿拉伯语方向,但字符串没有得到翻译。
下面是字符串文件的层次结构。
Flavor1
values
strings.xml [Contains all english strings]
values-ar
strings.xml [Contains all arabic strings]
values-ru
strings.xml [Contains all russian strings]
main
values
Contains colors,dimens but no strings file
Flavor2
values
strings.xml [Contains all english strings]
values-ar
strings.xml [Contains all arabic strings]
values-ru
strings.xml [Contains all russian strings]
下面是SettingsFragment的代码,其中更改了应用程序语言。
public class SettingsFragment extends PreferenceFragmentCompat implements
SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("OnCreate:", "Preference Fragment");
LanguageLocale.getInstance().getResources().updateConfiguration(LanguageLocale.getInstance().getConfiguration(), LanguageLocale.getInstance().getResources().getDisplayMetrics());
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onResume() {
LanguageLocale.getInstance().getResources().updateConfiguration(LanguageLocale.getInstance().getConfiguration(), LanguageLocale.getInstance().getResources().getDisplayMetrics());
Log.e("OnResume:", "Preference Fragment");
super.onResume();
}
@Override
public void onDestroy() {
Log.e("OnDestroy:", "Preference Fragment");
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
Log.e("OnCreatePreference:", "Preference Fragment");
addPreferencesFromResource(R.xml.pref_include);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
Preference preference = findPreference(s);
Log.e("PF", "In OnSharedPreferenceChanged");
if (null != preference) {
if (preference instanceof ListPreference) {
String selectedlang = null;
Log.e("Key: ", preference.getKey());
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(getContext()));
LanguageLocale.getInstance().setContext(getContext());
LanguageLocale.getInstance().setResources(Objects.requireNonNull(getActivity()).getResources());
LanguageLocale.getInstance().setConfiguration(LanguageLocale.getInstance().getResources().getConfiguration());
String value = sharedPreferences.getString(preference.getKey(), null);
assert value != null;
if (value.equals(getString(R.string.TEXT_LABEL_ENGLISH))) {
((ListPreference) preference).setDefaultValue(getContext().getResources().getString(R.string.TEXT_LABEL_ENGLISH));
LanguageLocale.getInstance().setLocale(new Locale("en"));
Locale.setDefault(LanguageLocale.getInstance().getLocale());
LanguageLocale.getInstance().setConfiguration(new Configuration());
LanguageLocale.getInstance().getConfiguration().setLocale(LanguageLocale.getInstance().getLocale());
LanguageLocale.getInstance().getContext().getResources().updateConfiguration(LanguageLocale.getInstance().getConfiguration(), LanguageLocale.getInstance().getContext().getResources().getDisplayMetrics());
} else if (value.equals(getString(R.string.TEXT_LABEL_RUSSIAN))) {
((ListPreference) preference).setDefaultValue(getContext().getResources().getString(R.string.TEXT_LABEL_RUSSIAN));
LanguageLocale.getInstance().setLocale(new Locale("ru"));
Locale.setDefault(LanguageLocale.getInstance().getLocale());
LanguageLocale.getInstance().setConfiguration(new Configuration());
LanguageLocale.getInstance().getConfiguration().setLocale(LanguageLocale.getInstance().getLocale());
LanguageLocale.getInstance().getContext().getResources().updateConfiguration(LanguageLocale.getInstance().getConfiguration(), LanguageLocale.getInstance().getContext().getResources().getDisplayMetrics());
}
// Arabic Language setting added internalization
else if (value.equals(getString(R.string.TEXT_LABEL_ARABIC))) {
((ListPreference) preference).setDefaultValue(getContext().getResources().getString(R.string.TEXT_LABEL_ARABIC));
LanguageLocale.getInstance().setLocale(new Locale("ar"));
Locale.setDefault(LanguageLocale.getInstance().getLocale());
LanguageLocale.getInstance().setConfiguration(new Configuration());
LanguageLocale.getInstance().getConfiguration().setLocale(LanguageLocale.getInstance().getLocale());
LanguageLocale.getInstance().getContext().getResources().updateConfiguration(LanguageLocale.getInstance().getConfiguration(), LanguageLocale.getInstance().getContext().getResources().getDisplayMetrics());
}
preference.setSummary(value);
getActivity().finish();
setPreferenceSummary(preference, value);
}
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
return true;
}
private void setPreferenceSummary(Preference preference, String value) {
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
listPreference.setSummary(value);
}
}
@Override
public void onDisplayPreferenceDialog(Preference preference) {
try {
if (preference.getKey().equalsIgnoreCase("password_dialog")) {
startActivity(new Intent(getActivity(), ChangePassword.class));
} else {
super.onDisplayPreferenceDialog(preference);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void onPause() {
LanguageLocale.getInstance().getResources().updateConfiguration(LanguageLocale.getInstance().getConfiguration(), LanguageLocale.getInstance().getResources().getDisplayMetrics());
Log.e("OnPause:", "Preference Fragment");
super.onPause();
}
}
下面是构建文件中的产品口味和来源集定义
productFlavors {
Flavor1 {
}
Flavor2 {}
}
sourceSets {
main{
java {
srcDirs('src/Flavor1/java/src', 'src/Flavor2/java/src','src/main/java/src')
}
res {
srcDirs('src/Flavor1/res/src', 'src/Flavor2/res/src','src/main/res/src')
}
}
Syriatel {
java {
srcDirs('src/Flavor1/java/src', 'src/main/java/src')
}
res {
srcDirs('src/Flavor1/res/src', 'src/main/res/src')
}
}
FMFI {
java {
srcDirs('src/Flavor2/java/src', 'src/main/java/src')
}
res {
srcDirs('src/Flavor2/res/src', 'src/main/res/src')
}
}
}
有人可以帮帮我吗,我无法理解我在这里做错了什么。任何帮助将不胜感激。预先感谢。
答案 0 :(得分:0)
我使用此类来使其正常工作:
object LocaleManager {
val LANGUAGE_RUSSIAN = "ru"
val LANGUAGE_UZBEK = "uz"
val INITIALIZATED = "inited"
private val LANGUAGE_KEY = "language_key"
fun setLocale(c: Context): Context {
return updateResources(c, getLanguage(c))
}
fun setNewLocale(c: Context, language: String): Context {
initial(c)
persistLanguage(c, language)
return updateResources(c, language)
}
fun getLanguage(c: Context): String {
val prefs = PreferenceManager.getDefaultSharedPreferences(c)
return prefs.getString(LANGUAGE_KEY, LANGUAGE_RUSSIAN) ?: LANGUAGE_RUSSIAN
}
fun initial(c: Context){
val prefs = PreferenceManager.getDefaultSharedPreferences(c)
prefs.edit().putBoolean(INITIALIZATED,true).apply()
}
fun isInitialed(c: Context):Boolean{
val prefs = PreferenceManager.getDefaultSharedPreferences(c)
return prefs.getBoolean(INITIALIZATED,false)
}
fun restoreLang(c: Context){
val prefs = PreferenceManager.getDefaultSharedPreferences(c)
prefs.edit().putBoolean(INITIALIZATED,false).apply()
}
@SuppressLint("ApplySharedPref")
private fun persistLanguage(c: Context, language: String) {
val prefs = PreferenceManager.getDefaultSharedPreferences(c)
prefs.edit().putString(LANGUAGE_KEY, language).commit()
}
private fun updateResources(context: Context, language: String): Context {
var context = context
val locale = Locale(language)
Locale.setDefault(locale)
val res = context.resources
val config = Configuration(res.configuration)
if (Build.VERSION.SDK_INT >= 17) {
config.setLocale(locale)
context = context.createConfigurationContext(config)
} else {
config.locale = locale
res.updateConfiguration(config, res.displayMetrics)
}
return context
}
fun getLocale(res: Resources): Locale {
val config = res.configuration
return if (Build.VERSION.SDK_INT >= 24) config.locales.get(0) else config.locale
}
}
然后在BaseActivity类中添加下一个方法:
override fun attachBaseContext(base: Context) {
super.attachBaseContext(LocaleManager.setLocale(base))
}