7

For e.g there is app which provides multi-language support, in my activity/UI, I call getResources().getString(R.string.hello) which exist in strings.xml,such that

values\strings.xml
values-ru\strings.xml

Now when calling getResources().getString(R.string.hello) and need to access string based on system locale, so will one get strings from values\strings.xml OR values-ru\strings.xml?

OR

does one need to change my app locale based on system locale (keep app locale same as system locale) and then retrieve the value from getString(), something suggested in below links

  1. get-string-from-default-locale-using-string-in-specific-locale

  2. how-to-get-string-from-different-locales-in-android

I have searched various other links, but not able to find the solution

Community
  • 1
  • 1
Akhil Jain
  • 13,872
  • 15
  • 57
  • 93

2 Answers2

8
MyProject/
    res/
       values/
           strings.xml
       values-es/
           strings.xml
       values-fr/
           strings.xml

Add the string values for each locale into the appropriate file.

At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.

For info on Localizing with Resources

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

More info @

http://developer.android.com/training/basics/supporting-devices/languages.html

Also check the below link

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/android/content/ContextWrapper.java/

86     @Override
87     public Resources getResources()
88     {
89         return mBase.getResources();
90     }

Return a Resources instance for your application's package.

332 
333     public final String getString(int resId) {
334         return getResources().getString(resId);
335     }

Return a localized string from the application's package's default string table. Parameters: resId Resource id for the string

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • thanks, for long description, actually i did look into but was not able to understand. – Akhil Jain Sep 05 '13 at 17:18
  • @AkhilJain glad if i could help. I think doc has all the info you need. http://developer.android.com/guide/topics/resources/localization.html. What part did you not understand? – Raghunandan Sep 05 '13 at 17:19
  • i opened the grep code for the Resources class, but was not able to understand as the code seemed scattered at first glance, spending more time seemed digging hole more deeper – Akhil Jain Sep 06 '13 at 06:43
  • Does getString get the string from memory or from disk? Trying to understand what the latency in getString is. – the_prole Feb 01 '22 at 18:00
2

It is done automatically. By standard the language that is on is in your values\strings.xml but if the user device has his language set to ru the string automatically is the one on the values-ru\strings.xml and so on for all the languages that you put on your resources.

You can read more about this subject in here.

At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.

Dyna
  • 2,304
  • 1
  • 14
  • 25