如何在 android 上查找字符串翻译?

How to lookup for a string translation on android?

我需要翻译从其中一台所有者服务器收到的错误代码。

因此,我没有字符串 ID,而是字符串、错误代码,我需要检查资源以进行可能的翻译。

我如何执行查找?

原来有一个函数 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);
}

正在做一个简单的测试:

@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

}