Android: 如何让 App Locale 独立于 System Locale 工作?
Android: How to keep App Locale work independently from System Locale?
我有一个与显示语言相关的问题。我可以独立于 OS 系统更改应用程序内的语言("en" 英语和 "ja" 日语)。
但是,问题是当应用程序处于 "ja" 时,如果用户手动更改系统语言(不是 "en" 或 "ja"),我的应用程序会自动更改语言默认 ("en")。我想让我的应用程序的语言环境独立,无论用户手动更改语言,应用程序的语言仍然与他们注销时相同。
编辑
有一些有用的链接,但仍然不能解决我的问题。例如:
Change language programatically in Android
你能给我一些建议吗?
提前致谢!
Try this one:
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, AndroidLocalize.class);
startActivity(refresh);
finish();
}
已添加:
@Override
public void onConfigurationChanged(Configuration newConfig) {
newConfig.setLocale(yourLocale);
super.onConfigurationChanged(newConfig);
}
添加(2):
您必须设置 android:configChanges="layoutDirection|locale"
才能在更改 Locale
时触发 onConfigurationChanged()
。
我不能完全理解为什么会这样,也许有一些 RTL
语言...
我有一个与显示语言相关的问题。我可以独立于 OS 系统更改应用程序内的语言("en" 英语和 "ja" 日语)。
但是,问题是当应用程序处于 "ja" 时,如果用户手动更改系统语言(不是 "en" 或 "ja"),我的应用程序会自动更改语言默认 ("en")。我想让我的应用程序的语言环境独立,无论用户手动更改语言,应用程序的语言仍然与他们注销时相同。
编辑
有一些有用的链接,但仍然不能解决我的问题。例如: Change language programatically in Android
你能给我一些建议吗?
提前致谢!
Try this one:
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, AndroidLocalize.class);
startActivity(refresh);
finish();
}
已添加:
@Override
public void onConfigurationChanged(Configuration newConfig) {
newConfig.setLocale(yourLocale);
super.onConfigurationChanged(newConfig);
}
添加(2):
您必须设置 android:configChanges="layoutDirection|locale"
才能在更改 Locale
时触发 onConfigurationChanged()
。
我不能完全理解为什么会这样,也许有一些 RTL
语言...