Android tip #014 – Multi-language application
Platform/Language: Java/Android
Description: if we need to create an application that supports different languages, we need to create different folders for values for different languages. Here an example of English (default), Spanish (es) and Swedish (sv). The language is selected automatically using the system language.
Code:
Different folders inside /res/
1 2 3 | /res/values/strings.xml /res/values-es/strings.xml /res/values-sv/strings.xml |
English /res/values/strings.xml
1 2 3 4 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello</string> </resources> |
Spanish /res/values-es/strings.xml
1 2 3 4 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hola</string> </resources> |
Swedish /res/values-sv/strings.xml
1 2 3 4 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hej</string> </resources> |
YourActivity.java
1 2 3 4 5 6 7 8 | /* ... */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String helloString = this.getString(R.id.hello); } /* ... */ |

