1

I am already well aware of how easy it is to make a multilanguage app with Android by using the string.xml code. That part was nice and easy.

The issue is I have a lot of dynamic texts in my app that are made with my Java code. This is the part where I am can't find how to also make multilingual. The way I am picturing a solution is having some kind of condition in which I verify what language it is in, and change the string. For example:

 if(language.equals(English))
      textView1.setText("Hi! This is in English!");
 if(language.equals(Portuguese))
      textView1.setText("Oi! Isso é em Português!");

Is it possible for me to do something like this? If so, how can I do it?

Thanks!

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
theJuls
  • 6,788
  • 14
  • 73
  • 160
  • I guess you could, but that could get rough depending on how many views you have. It might be easier to just create different versions of the app entirely depending on download locale or something. – zgc7009 May 22 '14 at 19:55
  • This might be of some use: http://developer.android.com/guide/topics/resources/localization.html – Perneel May 22 '14 at 19:55
  • Could also take a look at http://www.icanlocalize.com/site/tutorials/android-application-localization-tutorial/ – zgc7009 May 22 '14 at 19:56
  • What do you mean by "dynamic texts"? There is nothing dynamic about your example. – Brodo Fraggins May 22 '14 at 20:00
  • I think I might've found a solution, although I do not know if it's the best: String locale = context.getResources().getConfiguration().locale.getDisplayName(); it gives me back whatever language I am using @BrodoFraggins by dynamic I mean they change as I am running my app depending on what I am running. That was just an example because I change a lot of TextViews through Java as well as send some messages to the user depending on what he does. – theJuls May 22 '14 at 20:02
  • but where do these strings come from and why can't they be in strings.xml? – njzk2 May 22 '14 at 20:09
  • @njzk2 they come from recommendations that are given by the application. Basically what it does is tell the user how much exercise he/she should do, based on previous values. I also have some Toasts that recommend changes for the user to do. The way I see it, it just seems very hard to do all these different recommendations and messages through the strings.xml file. – theJuls May 22 '14 at 20:13
  • @theJuls: i still don't understand why it is not simpler to just put the strings in strings.xml (note that you can have several xml files with strings in them). That's explicitly made for handling localisation and numerous texts and translations. – njzk2 May 22 '14 at 20:16
  • `it just seems very hard to do all these different recommendations and messages through the strings.xml file` don't understand why – njzk2 May 22 '14 at 20:17
  • @njzk2 it just made more sense for me at the time (before deciding to make the app multilingual). But with all these recommendations to just use strings.xml, I am just going to go that route after all. It is an all around good practice anyway. – theJuls May 22 '14 at 20:20

3 Answers3

5

Extract those strings into the strings.xml file too and it will choose the value from the correct file depending on the language. There is no reason to have those strings hardcoded in your java.

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

Will have the right language if its available, since at runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device. For instance if you have res/values-ru/strings.xml that contains res/values/strings.xml and the device is in russian, it will take the value (if it's available) from the first one.

Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
  • For some reason I just thought it seemed easier to hardcode the messages and all into Java. But if it's really recommended to just do it all in xml code, I will change it to do so. Thanks for your help. – theJuls May 22 '14 at 20:19
  • @theJuls its the recommended way because you keep content separated from logic. This way someone without any programming language could modify a literal or translate it without needing to mess with your code and potentially breaking it, at least that's how I see it – Juan Cortés May 22 '14 at 21:04
2

Based on your example, it sounds like you don't actually understand how strings.xml works, because that seems to be the answer you're looking for. Your entire example can be replaced with one line:

textView1.setText(R.string.hello);

Then you create different versions of strings.xml, all of them containing exactly the same IDs but different string values. If English is your default, then the English version would be named /res/values/strings.xml. The Portuguese file would be named /res/values-pt/strings.xml. The system would automatically use the strings from the correct file for the device locale.

Brodo Fraggins
  • 628
  • 5
  • 14
  • I suppose that makes sense. It just seemed at first to me that it would probably be easier to change the language also within the Java code. But since it apparently really isn't recommended, I might as well just stick to this example. Thanks for your help. – theJuls May 22 '14 at 20:17
1

After a few hundreds texts to translate and include in the code, you'll see that it is much simpler to use strings in resources.

  1. Your java code never has to worry about adding a new language, just create a new values-.. folder
  2. You can have separate persons, including non-developers, working on translations at the same time as you work on the code
  3. You never have to worry about a missing translation, as lint will warn you, and the default language fallback will guaranty there will be no crash
  4. That's the only way to put localized texts in XML layout without setting them explicitly in java, which saves you tons of useless, error prone, spaghetti code.
  5. It is the standard and recommended way to do so, meaning a/ you'll find support should you need it and b/ any android developer joining your team will be in a familiar setting
njzk2
  • 38,969
  • 7
  • 69
  • 107