首页 > 解决方案 > 如何在 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是“超出”字符串资源。
现在,假设hdarian_suffix是我要附加的匈牙利语后缀(如果系统语言是匈牙利语),我怎样才能以简单的方式实现这一点?

标签: androidstringlocale

解决方案


要获取当前语言环境,请先在下面写下一行:

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
}

推荐阅读