使用新的支持库根据区域设置更改应用程序语言
Change App languages based on Locale using the new support libraries
我曾经更改过应用程序中的语言
通过执行以下操作:
Configuration cfg = new Configuration();
if (!TextUtils.isEmpty(lang))
cfg.locale = new Locale(lang);
else
cfg.locale = Locale.getDefault();
Resources resources = ctx.getResources();
resources.updateConfiguration(cfg, resources.getDisplayMetrics());
然后重启activity:
Intent intent = new Intent(context, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
这种方式工作正常,但是在我将支持库版本从 'com.android.support:appcompat-v7:23.1.1'
更改后
到'com.android.support:appcompat-v7:23.2.1'
除非我终止应用程序,否则资源不会改变
我写了一个示例here that demonstrate the problem with a dummy solution that restart the app through the alarm manager, yet i believe that i might be doing something wrong or i missed something wrote in the release notes here
我已经搜索了很多,但我没有找到任何关于这个问题的信息,
commonsware's blog
中提到了我发现的与资源相关的唯一新问题
这是关于新 android N 的,如其所说:
if you are one of those developers who has been overriding the user’s device locale within your app… test thoroughly on N, please.
然而我的问题是棒棒糖、奇巧和糖豆
下面的代码对我来说工作正常。
我优先存储语言并使用以下代码我能够制作多语言应用程序。
Locale locale;
switch (userPreference.language)
{
default:
case 0:
locale = new Locale("de");
languageCode = "de-de";
break;
case 1:
locale = new Locale("en");
languageCode = "en-en";
break;
case 2:
locale = new Locale("fr");
languageCode = "fr-fr";
break;
case 3:
locale = new Locale("pl");
languageCode = "pl-pl";
break;
}
Configuration config = getResources().getConfiguration();
Locale.setDefault(locale);
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
请看下面我的代码activity
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
UserPreference userPreference = PreferenceHelper.getUserPreference(context);
switch (userPreference.language)
{
case 0:
GenericHelper.changeLanguage(context, "de");
break;
case 1:
GenericHelper.changeLanguage(context, "en");
break;
case 2:
GenericHelper.changeLanguage(context, "fr");
break;
case 3:
GenericHelper.changeLanguage(context, "pl");
break;
}
setContentView(R.layout.activity_get_signature);
我曾经更改过应用程序中的语言 通过执行以下操作:
Configuration cfg = new Configuration();
if (!TextUtils.isEmpty(lang))
cfg.locale = new Locale(lang);
else
cfg.locale = Locale.getDefault();
Resources resources = ctx.getResources();
resources.updateConfiguration(cfg, resources.getDisplayMetrics());
然后重启activity:
Intent intent = new Intent(context, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
这种方式工作正常,但是在我将支持库版本从 'com.android.support:appcompat-v7:23.1.1'
到'com.android.support:appcompat-v7:23.2.1'
除非我终止应用程序,否则资源不会改变
我写了一个示例here that demonstrate the problem with a dummy solution that restart the app through the alarm manager, yet i believe that i might be doing something wrong or i missed something wrote in the release notes here
我已经搜索了很多,但我没有找到任何关于这个问题的信息, commonsware's blog
中提到了我发现的与资源相关的唯一新问题这是关于新 android N 的,如其所说:
if you are one of those developers who has been overriding the user’s device locale within your app… test thoroughly on N, please.
然而我的问题是棒棒糖、奇巧和糖豆
下面的代码对我来说工作正常。 我优先存储语言并使用以下代码我能够制作多语言应用程序。
Locale locale;
switch (userPreference.language)
{
default:
case 0:
locale = new Locale("de");
languageCode = "de-de";
break;
case 1:
locale = new Locale("en");
languageCode = "en-en";
break;
case 2:
locale = new Locale("fr");
languageCode = "fr-fr";
break;
case 3:
locale = new Locale("pl");
languageCode = "pl-pl";
break;
}
Configuration config = getResources().getConfiguration();
Locale.setDefault(locale);
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
请看下面我的代码activity
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
UserPreference userPreference = PreferenceHelper.getUserPreference(context);
switch (userPreference.language)
{
case 0:
GenericHelper.changeLanguage(context, "de");
break;
case 1:
GenericHelper.changeLanguage(context, "en");
break;
case 2:
GenericHelper.changeLanguage(context, "fr");
break;
case 3:
GenericHelper.changeLanguage(context, "pl");
break;
}
setContentView(R.layout.activity_get_signature);