Android: How to keep App Locale independent of System Locale?

I have problems displaying the language. I can change the language inside the application ("en" for English and "ja" for Japanese) regardless of the OS.

However, the problem is that when the application is in "ja", if users change the system language manually (not "en" or "ja"), than my application automatically changes the default language ("en"). I want to make the locales of my application autonomous, no matter what languages ​​are changed manually, the application language still remains the same as when you exit it.

EDIT

There are some useful links, but they still cannot solve my problem. For example: Change the language programmatically in Android

Could you give me any suggestion to do this?

Thank you in advance!

+4
source share
1 answer

Try this option :

import java.util.Locale; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.res.Configuration; 
import android.content.res.Resources; 
import android.util.DisplayMetrics; 

public void setLocale(String lang) { 
    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, AndroidLocalize.class); 
    startActivity(refresh); 
    finish();
} 

ADDED:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    newConfig.setLocale(yourLocale);
    super.onConfigurationChanged(newConfig);
}

ADD (2):

You must set android:configChanges="layoutDirection|locale"to invoke onConfigurationChanged()when changing Locale.
I can’t fully understand why this is happening, maybe there are some languages RTL...

0
source

All Articles