Java – Android: how to make app locale work independently of system locale?

I have a problem with display language I can change the language in the application independently of the OS System (English is "en" and Japanese is "JA")

However, the problem is that when the application is in the "JA" state, if the user manually changes the system language (instead of "en" or "JA"), the application will automatically change the language to the default language ("en") I want the locale of my application to be independent. No matter the user manually changes the language, the language of the application will remain the same as when logging out

edit

There are some useful links, but they still can't solve my problem For example: change language programmatically in Android

Can you give me any advice to do it?

Thank you first!

Solution

Try this one:

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();
}

add to:

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

Add (2):

You must set Android: configchanges = "layoutdirection | locale" to trigger onconfigurationchanged() I can't fully understand why this happens. Maybe there are some RTL languages

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>