是否可以在使用Android资源的同时以编程方式更改应用程序的语言?
如果没有,是否可以请求特定语言的资源?
我想让用户从应用程序更改应用程序的语言.
我想在 Jetpack Compose 中以编程方式更改语言。我已经阅读了很多帖子并观看了视频,但仍然找不到方法。(帖子和视频均在Android视图系统中。)
\n如何更改 kotlin 中的语言(区域设置)
\n https://www.youtube.com/watch?v=xxPzi2h0Vvc
我希望我的应用程序像下图一样工作。点击语言后,整个app都会改变语言。下面的代码是可点击的部分。我应该在这个可点击部分和MainActivity.kt中做什么?
\n@Composable\nfun LanguageScreen(\n navController: NavController,\n) {\n val context = LocalContext.current\n val langList = arrayOf("English", "\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87", "\xe7\xae\x80\xe4\xbd\x93\xe4\xb8\xad\xe6\x96\x87", "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e")\n var items by remember {\n mutableStateOf(\n langList.map {\n LanguageItem(\n title = it,\n isSelected = false\n )\n }\n )\n }\n LazyColumn(\n modifier = Modifier\n .fillMaxSize()\n ) {\n items(items.size) { i ->\n Row(\n modifier = Modifier\n .fillMaxWidth()\n .clickable {\n\n items = items.mapIndexed { j, item ->\n if (i == j) {\n item.copy(isSelected = …Run Code Online (Sandbox Code Playgroud) 我有一些代码可以在 Java 中以编程方式更改语言环境。但是当我的应用程序迁移到 Kotlin 时,我无法再更改语言环境。
例如,Java 中的这段代码效果很好:
public static final void setAppLocale(String language, Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Resources resources = activity.getResources();
Configuration configuration = resources.getConfiguration();
configuration.setLocale(new Locale(language));
activity.getApplicationContext().createConfigurationContext(configuration);
} else {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = activity.getResources().getConfiguration();
config.locale = locale;
activity.getResources().updateConfiguration(config,
activity.getResources().getDisplayMetrics());
}
}
Run Code Online (Sandbox Code Playgroud)
我在 Kotlin 中尝试了很多代码,但没有一个对我有用。这是我最后一次尝试:
fun changeLanguage(context: Context, language : String) {
val locale = Locale(language)
Locale.setDefault(locale)
val config = context.resources.configuration
config.setLocale(locale)
context.createConfigurationContext(config)
context.resources.updateConfiguration(config, context.resources.displayMetrics)
}
Run Code Online (Sandbox Code Playgroud)
如何在 Kotlin 中更改应用程序的本地?用 Java …
所以基本上我有一个可以在三种语言之间进行选择的微调器,这是代码,我的语言确实发生了变化,但它似乎正在加载语言,而不是我为其设置的资源。
我的代码
private void setLocale(String localeName){
if (localeName.equalsIgnoreCase(""))
return;
Resources resources = getResources();
Locale locale = new Locale(localeName);
Locale.setDefault(locale);
android.content.res.Configuration config = new
android.content.res.Configuration();
config.locale = locale;
resources.updateConfiguration(config, resources.getDisplayMetrics());
//restart base activity
this.finish();
this.startActivity(this.getIntent());
}
Run Code Online (Sandbox Code Playgroud)
语言之一的 strings.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Tabibi</string>
<string name="already_have_an_account">????? ?????</string>
<string name="join">????? ????</string>
<string name="login">????? ??????</string>
<string name="language">?????</string>
</resources>
Run Code Online (Sandbox Code Playgroud)