Search code examples
javaandroidlocalizationlocaleculture

Android/Java: How to code if-statements based on localisation-settings?


Right now I'm starting to think about localisation of my ANDROID app. I'm coding in JAVA and I'm using the ADT environment. The app is supposed to run in German and English once it is finished. I found quite a few nice tutorial about how to access resources like strings, labels images etc. based on the devices UI.

However, I need to make DECISIONS in my code based on the language the app is running in. Let me give you a brief example of what I'd like to achieve (just as an example) with some pseudo-code:

If ("language-is-german") {

  start an audiostream playing a .wav-file saying "Guten morgen lieber Benutzer!!!";
  Integer intErgebnis = 2 + 4;

}
Else If ("language-is-english") {

  start an audiostream playing a .wav-file saying "Good morning dear user!!!";
  Integer intResult = 2 + 9;

}

The basic idea is to play different sound files (I know that you could also achieve that by pointing to different resources) - but they need to be played in different orders etc. I added the calculation just to show you that I really want to do different things based on the language set up on the user's computer.

How can I check for that in my code behind (i.e. what do I have to type instead of language-is-german in my pseudo-code above?


Solution

  • Locale.getDefault() returns the user's preferred locale.

    Test if its language is equal to Locale.FRENCH.getLanguage() or Locale.GERMAN.getLanguage().