1

I have several language files in Android application: value/strings.xml, value-en/strings.xml, ...etc It is possible to load the content of this files in some arrays or something. For example, I would like to load default text strings and english strings in 2 different arrays at run time.

Thanks

Alin

Iftemi Alin
  • 485
  • 4
  • 14
  • did you tried to change the locale in the configuration of your resource ? – njzk2 Sep 25 '12 at 14:53
  • I can change the language and then I have access to the configuration. But I intend to load the strings in an array without changing the locale at run-time. I was wondering if there is something like: array = loadResources("en"), array2=loadResources("es"). Changing the locale at run-time just to get these arrays is not an option for me, maybe to access the files direclty ? – Iftemi Alin Sep 25 '12 at 14:57
  • i don't necesarly mean changing the system locale, but may be you can change it in a configuration object and use resource.setConfiguration – njzk2 Sep 25 '12 at 15:15
  • you cannot really access the files, as the xml are compiled by the sdk – njzk2 Sep 25 '12 at 15:16
  • My intention is to use some proprietary language system (which is not wised) because the app require so. And in order not to hard code a huge set of language keys and values I was thinking maybe I can reuse the standard values/strings.xml I know that there are better recommended ways but the requirement for this one is to define language files , and user select language at run-time, based on his selection the app load the right array. To avoid hard-coding I was thinking maybe I can use those. I was just trying to stay as closed as possible to the default best practice. – Iftemi Alin Sep 25 '12 at 15:23
  • prevent your user from selecting the language at runtime. it makes no sense – njzk2 Sep 25 '12 at 15:25
  • this http://stackoverflow.com/questions/5244889/load-language-specific-string-from-resource may best illustrate what i try to explain – njzk2 Sep 25 '12 at 15:27
  • Actually the situation is like that. Imagine I have this scenario: – Iftemi Alin Sep 26 '12 at 08:02

2 Answers2

1

Create a method like this:

Resources getResourcesByLocale( Context context, String localeName ) {
    Resource res = context.getResources();
    Configuration conf = new Configuration(res.getConfiguration());
    conf.locale = new Locale(localeName);
    return new Resources(res.getAssets(), res.getDisplayMetrics(), conf);
}

Than you can get resources for any locale you've defined, for example:

Resources res_en = getResourcesByLocale(context, "en");
Resources res_de = getResourcesByLocale(context, "de");
Resources res_fr = getResourcesByLocale(context, "fr");

String some_name_en = res_en.getString(R.string.some_name);
String some_name_fr = res_fr.getString(R.string.some_name);
// etc...

Moreover, you do not need to take care about exceptions if you did not define string for some locale, because anyway default (from res/values/*) will be loaded instead.

Sergei Pikalev
  • 516
  • 4
  • 5
0

Actually the situation is like that. Imagine I have this scenario: some chinese open the application. He has the mobile phone set with ch locale. The application default is xx as language, meaning I have 2 language files values/strings.xml (spanish for eg as default) and another language values-en/strings.xml for english. The default will make no sense for him, so english will be the most appropiate for his understanding, even if he does not understand it very good. So at the app start I open language settings (android language settings), where any selection will set the app in spanish unless he select english. I am forcing him to change the phone locale in english basically, just to use my app. Overall the concept of android is wrong, because i need to be able to see an application in any language I want without changing device language.


What I have done: - I created in values folder one more string_xx.xml file. Now, for a translation string name = "txtTranslation" I have in string_xx file "en_txtTranslation" key. R.java loads them all and in my app, based on a global var selectedLanguage = xx, I attach the write string using this code:

public String translate(String text)
{
      String appLanguage = UtilsCentral.getUserLanguage(getApplicationContext());
      if (appLanguage != "")
      {
          return getString(getResources().getIdentifier(appLanguage + "_" + text, "string", this.getPackageName()));  
      }
      else 
      {
          return getString(getResources().getIdentifier(text, "string", this.getPackageName()));
      }
}

Indeed, at activity on create i need to set all views with .text = tarnslate("txtTranslation")

Note: UtilsCentral.getUserLanguage(getApplicationContext()) returns app language (user selection)

Conclusion, there is more unuseful work, but lets me do what i need, and what i believe is normal.

Iftemi Alin
  • 485
  • 4
  • 14
  • this is an interesting point indeed. "What happens if the app is not available in my language and I don't understand the app default language". This should be taken directly to android developement teams, I think – njzk2 Sep 26 '12 at 08:42
  • what you could do is change the system wide locale in your app, and change it back when the user exits – njzk2 Sep 26 '12 at 08:42
  • @njzk2 you can always ask them but I think that they will tell you that the solution is to provide a localization for the default language or do not let the user download the app in that market. – Teovald Sep 26 '12 at 08:59