2017-09-13 5 views
1

私はアプリの言語を変更しようとしていますが、それは起こっていません。興味深いことに、ほとんどのコードはFacebook Account Kitの活動の言語を変更できますが、他の活動の言語は変更されていません。私のアプリケーションクラスとマニフェストファイルは変更されていません。ロケールアプリ内での変更がうまくいかない

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

+0

ロケールで自動的に変更されることはありません。string.xmlに単語を追加する必要があります – Eazyz

+0

私にデモをお願いできますか? –

+0

変換された文字列ファイルを追加する言語の新しい値フォルダを作成します。例えば、ヒンディー語でresの中に "values-hi"のような新しいフォルダを作成し、hindiのデフォルトの翻訳を含む文字列ファイルを追加します。 –

答えて

1

あなたは以下のようにあなたの言語化(I18N)を変更することができます10:

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

} 

私はGitHub上の1、オープンソースのプロジェクトを持っています。 https://github.com/configurer/easy-screen-lock/blob/master/EasyScreenLock/src/com/configurer/easyscreenlock/utils/LanguageUtils.java

+0

私は同様のコードを使用しています。私のandrid-4.4では、 'changeLanguage(..) 'の呼び出しが**行われた後に** ** super.onCreate()が呼び出されました。 – k3b

+0

はい、ロケールの変更を行うにはアプリの再起動が必要です。 – james

関連する問題