0

I wish to set my app to use value-th\string.xml (which i have all the string value in thai). This is what i will do. -app start. check sharepreference value language (in this case english/thai) if thai, i will run thai. I have this function, however, i notice in Locale, i do not have an options for thai.

public static void updateLanguageResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        android.content.res.Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());



    }

I also tried use below code to change via settings. However, that will change the phone setting to thai, which my intention the change should only for my app. not the device's setting.

Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
            startActivity(intent);

Any suggestions are welcomed. Thanks in advance.

kggoh
  • 742
  • 1
  • 10
  • 24
  • try this demo: http://www.androidhive.info/2014/07/android-building-multi-language-supported-app/ – Damini Mehra Aug 11 '16 at 08:52
  • @Damini that's what Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS); startActivity(intent); does. I am able to change the device language to thai, however that will affect all app in the phone. I only want thai in my own app. – kggoh Aug 11 '16 at 09:21
  • found this [link] http://stackoverflow.com/questions/2264874/changing-locale-within-the-app-itself?rq=1 let me try first – kggoh Aug 11 '16 at 09:28
  • hm.. i think i hit the same problem Locale does not have thai options. – kggoh Aug 11 '16 at 09:30
  • found the answer. Thanks. – kggoh Aug 12 '16 at 04:09

1 Answers1

0

I have found the answer for my own question. Hope this may help others in the future.

first need to prepare a values\string.xml (default english), second language values-th\string.xml

public static void changeLocale(Context context, Locale locale) {
        Configuration conf = context.getResources().getConfiguration();
        conf.locale = locale;
        Locale.setDefault(locale);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            conf.setLayoutDirection(conf.locale);
        }

        System.out.println("changeLocale");
        context.getResources().updateConfiguration(conf, context.getResources().getDisplayMetrics());
    }

then call the function...make sure the file for thai language is -th

Utils.changeLocale(MyApplication.getInstance(), new Locale("th"));
kggoh
  • 742
  • 1
  • 10
  • 24