Get String From Different Values Folder in Android

情到浓时终转凉″ 提交于 2020-02-23 05:57:10

问题


I wanna develop android application for different types of languages. So I have used localization for it. For that I have created different values folder like values-fr, values-ja , values-de. N also created strings.xml with static value according to that values folder. So All is good. But now my question is that I wanna change UI text according to user's selection of language. So How can I manually get particular string values from values->string.xml for particular language???

I think it may be easy, But I have no idea.

Thanks,


回答1:


Locale locale = new Locale("cn");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale; 
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

http://developer.android.com/guide/topics/resources/localization.html




回答2:


You just need to get the String from the file like this way

String string = getString(R.string.hello);

Android will choose the folder based on the mobile locale. See this link for further information about Android localization.

http://developer.android.com/guide/topics/resources/localization.html

And this one for String resources if you still have some doubts on how to acces the strings.

http://developer.android.com/guide/topics/resources/string-resource.html

Hope it helps, good luck! :)



来源:https://stackoverflow.com/questions/12072134/get-string-from-different-values-folder-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!