如何在 Locale 上添加测试条件
How to add a test condition on Locale
我正在尝试将我的应用程序翻译成英语、法语、西班牙语和匈牙利语。
我想动态翻译字符串“1 out of 2”。因此,字符串资源中的 "out of" 部分并使用代码附加和前置数字。
它适用于法语 (1 sur 2) 和西班牙语 (1 de 2)。但是,对于匈牙利语,它应该是“1 a 2-ből”。
注意后缀“-ből”。
因此,我只想为匈牙利语言环境添加此后缀(使用测试条件)。
我的代码如下:
// Example : "1 out of 2"
textToRead = item.itemIndex() + getContext().getResources().getString(R.string.index_over_size_separator) + item.numberOfSiblings();
index_over_size_separator 是 "out of" 字符串资源。
现在,假设 hungarian_suffix 是我要附加的匈牙利语后缀(如果系统语言是匈牙利语),我怎样才能以简单的方式实现这一点?
要获取当前语言环境,请先在下面的行中写入:
Locale current = getResources().getConfiguration().locale;
然后将其与 "hu_ [Hungarian]" 或 "hu_HU [Hungarian (Hungary)]" 进行比较,如下所示:
if(current.toString().equalsIgnoreCase("hu_") || current.toString().equalsIgnoreCase("hu_HU")){
// do your task for only Hungarian language
}
您可以像这样使用资源字符串来显示动态值:
<!-- french -->
<string name="index">%d sur %d</string>
<!-- english-->
<string name="index">%d out of %d</string>
<!-- spanish -->
<string name="index">%d de %d</string>
<!-- hungarian -->
<string name="index">%d a %d-ből</string>
然后您可以在 getString()
中填写 %d 个值
textToRead = getContext().getResources().getString(R.string.index_over_size_separator, item.itemIndex(), item.numberOfSiblings());
有关详细信息,请参阅 this
我正在尝试将我的应用程序翻译成英语、法语、西班牙语和匈牙利语。
我想动态翻译字符串“1 out of 2”。因此,字符串资源中的 "out of" 部分并使用代码附加和前置数字。
它适用于法语 (1 sur 2) 和西班牙语 (1 de 2)。但是,对于匈牙利语,它应该是“1 a 2-ből”。
注意后缀“-ből”。
因此,我只想为匈牙利语言环境添加此后缀(使用测试条件)。
我的代码如下:
// Example : "1 out of 2"
textToRead = item.itemIndex() + getContext().getResources().getString(R.string.index_over_size_separator) + item.numberOfSiblings();
index_over_size_separator 是 "out of" 字符串资源。
现在,假设 hungarian_suffix 是我要附加的匈牙利语后缀(如果系统语言是匈牙利语),我怎样才能以简单的方式实现这一点?
要获取当前语言环境,请先在下面的行中写入:
Locale current = getResources().getConfiguration().locale;
然后将其与 "hu_ [Hungarian]" 或 "hu_HU [Hungarian (Hungary)]" 进行比较,如下所示:
if(current.toString().equalsIgnoreCase("hu_") || current.toString().equalsIgnoreCase("hu_HU")){
// do your task for only Hungarian language
}
您可以像这样使用资源字符串来显示动态值:
<!-- french -->
<string name="index">%d sur %d</string>
<!-- english-->
<string name="index">%d out of %d</string>
<!-- spanish -->
<string name="index">%d de %d</string>
<!-- hungarian -->
<string name="index">%d a %d-ből</string>
然后您可以在 getString()
textToRead = getContext().getResources().getString(R.string.index_over_size_separator, item.itemIndex(), item.numberOfSiblings());
有关详细信息,请参阅 this