gpt4 book ai didi

android - 当我将语言更改为法语时,getResources.getString(R.string.hello_world) 不起作用

转载 作者:行者123 更新时间:2023-11-29 02:27:09 26 4
gpt4 key购买 nike

 我有 3 个字符串文件,用于 3 种不同的语言(English enFrench frSpanish sp)。当我将语言从 en 更改为 sp 时,一切正常。如果我将其更改为 fr,它会显示未找到资源 ID。该资源在 3 个字符串文件中可用。我怎样才能获得 fr 语言?

Process: com.bhaskar.project, PID: 19377 Error android.content.res.Resources$NotFoundException: String resource ID #0x7f0f0059

最佳答案

试试下面的代码。为了更改语言,我制作了一个函数,您必须传递语言代码和上下文。

语言=“zh”根据您的要求设置语言(英语 en、法语 fr、西类牙语 sp)上下文传递您的 Activity/fragment 上下文

public static boolean setLangRecreate(String language, Context context) {
try {
Configuration config = context.getResources().getConfiguration();
Locale locale = new Locale(language);
Locale.setDefault(locale);
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
// recreate();
((Activity) context).finish();
((Activity) context).overridePendingTransition(0, 0);
((Activity) context).startActivity(((Activity) context).getIntent());
((Activity) context).overridePendingTransition(0, 0);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

关于android - 当我将语言更改为法语时,getResources.getString(R.string.hello_world) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51551601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com