简体   繁体   中英

How can I make a multi-language app with the Java code?

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!

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM