How to detect Russian for localizing app in Android

5.1k views Asked by At

I have an app that needs to support Engligh, Spanish, and Russian.

To detect this, I use this method:

UserLanguage = setLanguage(Locale.getDefault().getDisplayLanguage());

UserLanguage is "en" if English, "es" is Spanish, and it needs to be "ru" if Russian, otherwise it will be English. However, when I set the locale on my phone to Russian, it is not detected. The documentation on the Android website does not say anything about Russian. Any help?

private String setLanguage(String locale){
    //Toast.makeText(context, locale.toString(), Toast.LENGTH_SHORT).show();
    if(locale.equals("English")){
        UserLanguage="en";
    }else{
        if(locale.equals("español")){//espanol
            UserLanguage="es";
        }else{
            if(locale.equals("ru")){//cant compile with true russian
                UserLanguage="ru";
            }else{
                //I give up.... english??
                UserLanguage="en";
            }
        }
    }
    return UserLanguage;
}
2

There are 2 answers

5
morphium On BEST ANSWER

For Russian Locale.getDefault().getDisplayLanguage() will return "русский"

Try this instead:

        if(locale.equals("русский")){ 
            UserLanguage="ru";
        }else{
            //I give up.... english??
            UserLanguage="en";
        }

But much better solution is to use getLanguage(), instead of getDisplayLanguage():

private String setLanguage(String locale){ 
    if (locale.equals("es")){
        mUserLanguage = "es";
    } else if(locale.equals("ru")){
        mUserLanguage = "ru";
    } else { 
        mUserLanguage = "en";
    }
    return UserLanguage;
}

setLanguage(Locale.getDefault().getLanguage());
0
Jon Willis On

First, check out the Android Developers Localization Guide

You can achieve a lot of what you may want to do by taking advantage of Android's XML magic. For instance, say you want to have localized strings. All you'd have to do is create:

res/values/strings.xml
res/values-es/strings.xml
res/values-ru/strings.xml

with localized strings in each language's respective strings.xml. You can also do this with drawables, layouts, and other XML resources that might change between locale.

You also might want to check out Crowdin which provides collaborative localization services specifically for android.