getString() 仅 returns 个英文值
getString() only returns english values
这是我的 onActivityResult
方法的代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
if (contents.length() == 26) {
BillBarcode barcode = new BillBarcode(contents);
edtBillId.setText(barcode.extract(BillBarcode.BarcodePart.BillId));
edtPaymentId.setText(barcode.extract(BillBarcode.BarcodePart.PaymentId));
launchService();
} else {
Dialog dialog = new DialogBuilder()
.setTitle(getString(R.string.dialog_title_global_error))
.setMessage(getString(R.string.unknown_barcode))
.build(getActivity());
dialog.show();
}
}
}
问题是 getString(R.string.dialog_title_global_error)
和 getString(R.string.unknown_barcode)
总是 returns 英语值,而我也有波斯语值并且语言环境也是波斯语。
只有这个方法存在问题
波斯语值:
<string name="unknown_barcode">بارکد قابل استفاده نیست.</string>
英文值:
<string name="unknown_barcode">Unknown barcode</string>
编辑
我有一个设置页面,当用户通过以下代码从语言页面选择波斯语时设置我的语言环境:
String languageToLoad = "fa";
Resources res = context.getResources();
// Change locale settings in the app.
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(languageToLoad);
让我尝试将所有评论汇总到一个答案中。您的问题是两个实施错误的组合:
您正在以编程方式设置当前 Activity
上下文的区域设置。但是,您这样做的方式不受支持,可能会产生不正确的结果。
当您的 activity 从 OnActivityResult()
中的另一个 activity 获得结果时,您的 Activity
要么完全重新启动,要么重置上下文配置到系统的默认语言环境。无论哪种方式,您在设置对话框中设置的语言环境都会丢失。
解决方案
- 此处概述了在本地更改应用程序语言环境的正确方法:Changing Locale within the app itself。
特别是,虽然仅更改配置中的区域设置 class 可能对您有用,但这显然不是更改应用程序区域设置的正确方法。正确地做它需要更多的工作:
Locale locale; // set to locale of your choice, i.e. "fa"
Configuration config = getResources().getConfiguration();
config.setLocale(locale); // There's a setter, don't set it directly
getApplicationContext().getResources().updateConfiguration(
config,
getApplicationContext().getResources().getDisplayMetrics()
);
// you might even need to use getApplicationContext().getBaseContext(), here.
Locale.setLocale(locale);
在应用程序上下文中设置语言环境应该在 activity 重启后仍然有效,但根据 Android 的生命周期保证,您不应假定语言环境会保留下来。
- 如果您确实需要能够在本地更改您的应用程序的区域设置,您应该保存用户指定的区域设置(例如在
SharedPreference
中)并在应用程序或您的 activity 重新启动(即至少在 OnCreate()
中)。请记住,Android 可以在您无法控制的任何时间自由保存和重新启动您的活动,您有责任优雅地处理重新启动。
这是我的 onActivityResult
方法的代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
if (contents.length() == 26) {
BillBarcode barcode = new BillBarcode(contents);
edtBillId.setText(barcode.extract(BillBarcode.BarcodePart.BillId));
edtPaymentId.setText(barcode.extract(BillBarcode.BarcodePart.PaymentId));
launchService();
} else {
Dialog dialog = new DialogBuilder()
.setTitle(getString(R.string.dialog_title_global_error))
.setMessage(getString(R.string.unknown_barcode))
.build(getActivity());
dialog.show();
}
}
}
问题是 getString(R.string.dialog_title_global_error)
和 getString(R.string.unknown_barcode)
总是 returns 英语值,而我也有波斯语值并且语言环境也是波斯语。
只有这个方法存在问题
波斯语值:
<string name="unknown_barcode">بارکد قابل استفاده نیست.</string>
英文值:
<string name="unknown_barcode">Unknown barcode</string>
编辑
我有一个设置页面,当用户通过以下代码从语言页面选择波斯语时设置我的语言环境:
String languageToLoad = "fa";
Resources res = context.getResources();
// Change locale settings in the app.
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(languageToLoad);
让我尝试将所有评论汇总到一个答案中。您的问题是两个实施错误的组合:
您正在以编程方式设置当前
Activity
上下文的区域设置。但是,您这样做的方式不受支持,可能会产生不正确的结果。当您的 activity 从
OnActivityResult()
中的另一个 activity 获得结果时,您的Activity
要么完全重新启动,要么重置上下文配置到系统的默认语言环境。无论哪种方式,您在设置对话框中设置的语言环境都会丢失。
解决方案
- 此处概述了在本地更改应用程序语言环境的正确方法:Changing Locale within the app itself。
特别是,虽然仅更改配置中的区域设置 class 可能对您有用,但这显然不是更改应用程序区域设置的正确方法。正确地做它需要更多的工作:
Locale locale; // set to locale of your choice, i.e. "fa"
Configuration config = getResources().getConfiguration();
config.setLocale(locale); // There's a setter, don't set it directly
getApplicationContext().getResources().updateConfiguration(
config,
getApplicationContext().getResources().getDisplayMetrics()
);
// you might even need to use getApplicationContext().getBaseContext(), here.
Locale.setLocale(locale);
在应用程序上下文中设置语言环境应该在 activity 重启后仍然有效,但根据 Android 的生命周期保证,您不应假定语言环境会保留下来。
- 如果您确实需要能够在本地更改您的应用程序的区域设置,您应该保存用户指定的区域设置(例如在
SharedPreference
中)并在应用程序或您的 activity 重新启动(即至少在OnCreate()
中)。请记住,Android 可以在您无法控制的任何时间自由保存和重新启动您的活动,您有责任优雅地处理重新启动。