3

i can't find Indonesia language in Locale.class, how can i add that language? so my code bellow showing red text because can't find that language.

public static Locale getUserLocale() {
    int currentLanguage = SpUtil.getInstance().getInt("currentLanguage", 0);

    Locale myLocale = Locale.INDONESIA;// error
    switch (currentLanguage) {
        case 0:
            myLocale = Locale.INDONESIA; // error
            break;
        case 1:
            myLocale = Locale.ENGLISH;
            break;

    }
    return myLocale;
}

here is excerpt from Locale.class :

public final class Locale implements Cloneable, Serializable {
public static final Locale CANADA = null;
public static final Locale CANADA_FRENCH = null;
public static final Locale CHINA = null;
public static final Locale CHINESE = null;
public static final Locale ENGLISH = null;
public static final Locale FRANCE = null;
public static final Locale FRENCH = null;
public static final Locale GERMAN = null;
public static final Locale GERMANY = null;
public static final Locale ITALIAN = null;
public static final Locale ITALY = null;
public static final Locale JAPAN = null;
public static final Locale JAPANESE = null;
public static final Locale KOREA = null;
public static final Locale KOREAN = null;
public static final Locale PRC = null;
public static final char PRIVATE_USE_EXTENSION = 'x';
public static final Locale ROOT = null;
public static final Locale SIMPLIFIED_CHINESE = null;
public static final Locale TAIWAN = null;
public static final Locale TRADITIONAL_CHINESE = null;
public static final Locale UK = null;
public static final char UNICODE_LOCALE_EXTENSION = 'u';
public static final Locale US = null;

public Locale(String language, String country, String variant) {
    throw new RuntimeException("Stub!");
} 
asqa
  • 199
  • 3
  • 17

2 Answers2

3

You can try this using Indonesian, Indonesia (in_ID) from

https://web.archive.org/web/20120814113314/http://colincooper.net/blog/2011/02/17/android-supported-language-and-locales/

so something like

Locale INDONESIA = new Locale("in", "ID");

then assign using

myLocale = INDONESIA;
Angel Koh
  • 12,479
  • 7
  • 64
  • 91
  • case 0: Locale INDONESIA = new Locale("in","ID"); myLocale = INDONESIA; break; like that right? – asqa Jan 29 '18 at 06:38
  • yup. you can move INDONESIA to a static field if you need to use it a lot/at other places. or straight away set myLocale = new Locale("in","ID"); if it is only used in one place. – Angel Koh Jan 29 '18 at 07:11
3

You cannot add the language to the class since it is final.

The Java class java.util.Locale does not have all the locales and countries as static members. You will have to create the one you need and then create another method for checking the locale. You will need to replace the logic in your getUserLocale method.

Locale locale = new Locale("in", "ID");

That will give you the correct locale object.

You can use

defaultLocale = Resources.getSystem().getConfiguration().getLocales().get(0);

to get the user's device locale.

Ray Hunter
  • 15,137
  • 5
  • 53
  • 51