Get string from default locale using string in specific locale

后端 未结 8 983
温柔的废话
温柔的废话 2020-12-06 09:58

Ok, I know title sound crazy :)

Here is what I want. My app is localized for device user but information I send back to server need to be all English. My default app

8条回答
  •  暖寄归人
    2020-12-06 10:38

    The code below will retrieve localized string for polish language even if the default locale on the device is different:

    Configuration conf = getResources().getConfiguration();
    conf.locale = new Locale("pl");
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    Resources resources = new Resources(getAssets(), metrics, conf);
    /* get localized string */
    String str = resources.getString(R.string.hello);
    

    I hope this also apply to other resource types like array, so it should suffice to replace "pl" with "en"...

提交回复
热议问题