[英]How to lookup for a string translation on android?
I need to translate error codes that i receive from one of owner servers. 我需要翻译从一台所有者服务器收到的错误代码。
So, instead of having a string id i have a string, the error code, and i need to check the resources for possible translation. 因此,我没有字符串ID,而是拥有字符串,错误代码,并且需要检查资源以进行可能的翻译。
How can i perform a lookup? 如何执行查找?
Turns out that there is a function for that, getIdentifier. 事实证明,有一个函数getIdentifier。
private String getStringResourceByName(Resources res, String aString) {
String packageName = App.getInstance().getPackageName();
int resId = App.getInstance().getResources().getIdentifier(aString, "string", packageName);
return res.getString(resId);
}
Trying on a simple test: 尝试一个简单的测试:
@Test
public void test(){
Resources res = serviceFactory.getApplicationContext().getResources();
Log.d(TAG, "default language: "+ getStringResourceByName(res,"LG")); //will print the default definition.
Configuration conf = res.getConfiguration();
conf.locale = Locale.FRENCH; //for instance
res.updateConfiguration(conf, null); //
Log.d(TAG, "French: "+ getStringResourceByName( res,"LG"));//will print definition of LG in french resource file
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.