android - 如何在 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是我要附加的匈牙利语后缀(如果系统语言是匈牙利语),我怎样才能以简单的方式实现这一点?
解决方案
要获取当前语言环境,请先在下面写下一行:
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
}
推荐阅读
- javascript - 如何在不使调用者异步的情况下等待承诺?
- azure-ad-b2c - Azure B2C API 包装库 - 它是否存在?
- sql - 需要通过 informatica 获取列中的特定数据
- security - FirebaseFirestore - 是否有规则或方法仅授权我的网络应用程序从数据库中读取?
- symfony5 - 如何在控制器中获取控制器名称 | symfony5
- npm - 在 devDependencies 中使用相同的名称使用分叉的 NPM 包作为替换
- flutter - 加载网络图片失败
- android - Android 'BadTokenException 窗口令牌 android.os.BinderProxy@4250d6d8 无效',前台服务正在运行
- r - 绘制预测值的数据
- python - Pika 心跳终止连接