2

I am trying to change the language of the app but it's not happening.I have tried almost all solutions available in SO but none worked. Interesting fact is most of the codes are able to change the language of the Facebook Account Kit activity but other activity's language is not being changed.Below is my application class and manifest file.

public class MyApplication extends Application {


    private Locale locale = null;

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        if (locale != null)
        {
            newConfig.locale = locale;
            Locale.setDefault(locale);
            getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
        }
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        Configuration config = getBaseContext().getResources().getConfiguration();

        new SharedPrefManager(this).setLanguage("fr");
        String lang = new SharedPrefManager(this).getLanguage();

        locale = new Locale(lang);
        Locale.setDefault(locale);
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        AccountKit.initialize(getApplicationContext());
    }


}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.pullerr.pullerrdriver">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>
    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data android:name="com.facebook.accountkit.ApplicationName"
            android:value="@string/app_name" />
        <meta-data android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/FACEBOOK_APP_ID" />
        <meta-data android:name="com.facebook.accountkit.ClientToken"
            android:value="@string/ACCOUNT_KIT_CLIENT_TOKEN" />
        <meta-data android:name="com.facebook.accountkit.FacebookAppEventsEnabled"
            android:value="false"/>

        <activity
            android:name="com.facebook.accountkit.ui.AccountKitActivity"
            android:theme="@style/AppLoginTheme"
            tools:replace="android:theme"/>
        <activity
            android:configChanges="layoutDirection|locale"
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAINACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:configChanges="layoutDirection|locale"
            android:name=".LoadDetails"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.LOADDETAILS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:configChanges="layoutDirection|locale"
            android:name=".Login"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:configChanges="layoutDirection|locale"
            android:name=".Register"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.REGISTER" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <service android:name="services.LocationService"/>
    </application>

</manifest>

Facebook AccountKit Language is Changed

Anuran Barman
  • 1,556
  • 2
  • 16
  • 31
  • it is not going to change automatically with locale you need to add your words in string.xml –  Sep 13 '17 at 06:01
  • Can you show me a demo please? – Anuran Barman Sep 13 '17 at 06:02
  • create a new values folder for the language you are going to add translated string files, for example, if you need hindi create a new folder inside res like "values-hi" and add a string file inside this with default translations in hindi – Navneet Krishna Sep 13 '17 at 06:08
  • yes I have seen a project in Github where this was done. what should be the name of the string. I mean My App That means every single text has to be mentioned in the strings.xml file? can hard code it anywhere? – Anuran Barman Sep 13 '17 at 06:12
  • check the links you'll understand –  Sep 13 '17 at 06:13
  • possible duplicate of https://stackoverflow.com/questions/13181847/change-the-locale-at-runtime/ – k3b Sep 13 '17 at 08:40

2 Answers2

1

You can change your language (i18N) as below :

public static void changeLanguage(String language, Context context) {
    try {
        if ( null != language) {
            Locale locale = new Locale(language); 
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
        }

        Log.i(TAG, "Language changed to " + language);
    } catch (Exception e) {
        Log.e(TAG, "[changeLanguage]" + e.getMessage());
    }

}

I have one open-source project on GitHub. Drive-in yourself at https://github.com/configurer/easy-screen-lock/blob/master/EasyScreenLock/src/com/configurer/easyscreenlock/utils/LanguageUtils.java

james
  • 1,967
  • 3
  • 21
  • 27
  • 1
    I use similar code. On my andrid-4.4 it only worked if the call to `changeLanguage(..)` was made **before** calling `super.onCreate()` – k3b Sep 13 '17 at 08:38
  • Yes app restart is required to effect the locale changes. – james Sep 13 '17 at 08:55