Home > Blockchain >  How to get language value when using setLocale() function?
How to get language value when using setLocale() function?

Time:11-05

I have setLocale function like below that I wrote in main.java file.

How can I get the current Locale in the setLocale function?

I use the Locale value to be able to include an "if-else" condition in another java file. This mean, I have 2 tables of data, when the language in setLocale function is English, I will select the table for English, and when I choose another language, I will select the table for that language.

// main.java
public void setLocale(String lang) {
    Locale myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(this, MainActivity2.class);
    finish();
    startActivity(refresh);
}

CodePudding user response:

You can declare a new method to return the selected locale like the one below. and use this method in another java file.

public Locale getLocale() {
    return getResources().getConfiguration().locale;
}

CodePudding user response:

According to the documentation, using language as a parameter to determine what to do is unstable, but you can still do it. Using Annamalai Palanikumar's method above, you can do the following:

if (getLocale().getLanguage().equals(new Locale("en").getLanguage())) {
   // do something here
}

I hope this points you in the right direction. Good luck!

  • Related