5

I have an issues related to display language. I am able to change the language inside the application ("en" for English and "ja" for Japanese) independently from OS system.

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 auto-change the language to default ("en"). I want to make locale of my application stand alone, whatever language users change manually, the language of application still remain the same as when they log out.

EDIT

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

Could you give my any suggestion to do it?

Thank you in advance!

Community
  • 1
  • 1
Luong Truong
  • 1,953
  • 2
  • 27
  • 46
  • Possible duplicate of [Change language programatically in Android](http://stackoverflow.com/questions/2900023/change-language-programatically-in-android) – Doug Stevenson Mar 24 '16 at 06:05
  • I am using the same way which is mentioned in your link, but I meet the issue. Thank you for your link, I will update my question – Luong Truong Mar 24 '16 at 06:25

1 Answers1

0

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

ADDED:

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

ADDED(2):

You have to set android:configChanges="layoutDirection|locale" in order to trigger onConfigurationChanged() when you change Locale.
I cannot fully understand why this happens, maybe there are some RTL languages...

Community
  • 1
  • 1
Suhyeon Lee
  • 448
  • 1
  • 4
  • 11
  • It would be better if you could explain your answer. Consider explaining how your code works. – Archit Saxena Mar 24 '16 at 06:44
  • @Suhyeon Lee: this is what I am using in my app. Is there anyway to make sure that users can only change locale inside the app? Locale of the app will not be affected by system locale. Even users change the system locale, app still remain the same locale – Luong Truong Mar 24 '16 at 06:48
  • @LuongTruong If you set android:configChanges="locale" to your app, then I think you can try overriding onConfigurationChanged() in your Activity and immediately return or change locale. – Suhyeon Lee Mar 24 '16 at 06:53
  • @Suhyeon Lee: I also put the android:configChanges="locale" in activity AndroidManifest. Inside the activity I override onConfigurationChanged(), but when I change the locale, onConfigurationChanged() is not called :( – Luong Truong Mar 24 '16 at 06:58
  • @LuongTruong That's weird.. I haven't used onConifgurationChanged() for a long time, maybe something changed in recent API. I gotta dig a bit.. – Suhyeon Lee Mar 24 '16 at 07:07
  • 1
    @LuongTruong Added code to force trigger onConfigurationChanged(). Please have a try. – Suhyeon Lee Mar 24 '16 at 07:19
  • @Suhyeon Lee: thank you a lot Suhyeon, I finally make it work http://stackoverflow.com/questions/17960525/onconfigurationchanged-is-not-getting-called-any-time-in-android. The problem is that we have to add "layoutDirection" too. I do not know why, but onConfigChange() is now called. – Luong Truong Mar 24 '16 at 07:23