8

how could i detect if my phone language has been changed, like facebook application that will give us announce : please wait, we preparing your language

i used myString = Locale.getDefault().getDisplayLanguage();in my onCreate()

on my onCreate()

@Override
    public void onCreate(Bundle savedInstanceState) {
        String myString = Locale.getDefault().getDisplayLanguage();
        if(myString.equals("en"){
            ProgressDialog progressDialog = new ProgressDialog(getApplicationContext());
            progressDialog.setMessage("Please wait, we preparing your language");
            progressDialog.show();
            /*
            it will dismiss until the language has been prepared
             */
        }else{
            //do nothing
        }
    }
}

please give me suggestion, i still learning, will try harder. thank you.

Sarudu Matra
  • 141
  • 1
  • 1
  • 6

4 Answers4

13

You can use: ACTION_LOCALE_CHANGED

Here an example:

    private BroadcastReceiver mLangReceiver = null;

    protected BroadcastReceiver setupLangReceiver(){

        if(mLangReceiver == null) {

            mLangReceiver = new BroadcastReceiver() {

                @Override
                public void onReceive(Context context, Intent intent) {
                    // do what you want
                }

            };

            IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
            registerReceiver(mLangReceiver, filter);
        }

        return mLangReceiver;
    }
Alessandro Verona
  • 1,157
  • 9
  • 23
4

What about to make a broadcast listener to: ACTION_LOCALE_CHANGED

Since "Locale represents a language/country/variant combination. Locales are used to alter the presentation of information such as numbers or dates to suit the conventions in the region they describe."

http://developer.android.com/reference/java/util/Locale.html

narancs
  • 5,234
  • 4
  • 41
  • 60
2
Locale.getDefault().getLanguage(); 

You can use this to get languages in "en"-like format.

Locale.getDefault().getDisplayLanguage(); returns language name e.g. "English" not "en".

This is also useful : https://stackoverflow.com/a/34265899/5515972

Community
  • 1
  • 1
Mann
  • 560
  • 3
  • 13
  • if i use progressDialog as notification if the language has been changes, so when the progressBar dismiss, (case : doesn't use timer ) – Sarudu Matra Dec 15 '15 at 10:17
  • This doesn't answer the question. Your answer gets the current locale. It doesn't detect when the locale has changed. – James May 18 '22 at 06:22
  • When getting the locale you should use `Resources.getSystem().configuration.locales[0]` instead of `Locale.getDefault()`. Reference: https://medium.com/@hectorricardomendez/how-to-get-the-current-locale-in-android-fc12d8be6242 – James May 18 '22 at 17:06
2

Here's a more modern way to get locale changes using Flow. Like others mentioned, we'll need a BroadcastReceiver to receive the Intent.ACTION_LOCALE_CHANGED action. To start, add this function to create the Flow.

private fun getLocaleChangedFlow(context: Context) = callbackFlow {
    val receiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent) {
            if (intent.action == Intent.ACTION_LOCALE_CHANGED) {
                val locale = context?.resources?.configuration?.locales?.get(0)
                if (locale != null) {
                    trySend(locale)
                }
            }
        }
    }
    context.registerReceiver(receiver, IntentFilter(Intent.ACTION_LOCALE_CHANGED))

    awaitClose {
        context.unregisterReceiver(receiver)
    }
}

Then use the Flow. Replace the TODO() with your own logic:

getLocaleChangedFlow(context)
    .onEach {
        TODO() // Locale changed, add code here
    }
    .launchIn(coroutineScope)

To learn more, see these links:

Kotlin docs about callbackFlow

Android docs for Intent.ACTION_LOCALE_CHANGED

StackOverflow answer to Wrap a BroadcastReceiver in a Flow

James
  • 4,573
  • 29
  • 32