我有一个具有核心和库的项目
我在核心项目的运行时更改了我的语言环境。
问题是,在图书馆项目中有一个
Locale.getDefault();
它只返回设备的语言环境,而不是应用程序的语言环境。这意味着,当我在我的应用程序中将语言环境更改为法语时,如果我的设备使用英语,则库项目获取的语言环境仍然是英语。如何解决?谢谢
我有一个具有核心和库的项目
我在核心项目的运行时更改了我的语言环境。
问题是,在图书馆项目中有一个
Locale.getDefault();
它只返回设备的语言环境,而不是应用程序的语言环境。这意味着,当我在我的应用程序中将语言环境更改为法语时,如果我的设备使用英语,则库项目获取的语言环境仍然是英语。如何解决?谢谢
您可以更改默认语言环境。
您可以尝试使用以下代码:
Locale locale = new Locale("fr"); //if you want to change to French
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
或者制作一个区域设置方法:
public void setLocale(String newLocale) {
Locale locale = new Locale(newLocale);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics() );
}