2017-09-29 4 views
0

です。多言語アプリケーションを作成しています。私は言語の変更のための2つのオプションを与えました。言語の変更は機能していますが、ユーザーが終了するとアプリが終了します。デフォルト言語に戻ります。アプリの終了後にデフォルトに設定された言語は、アンドロイド

public boolean onOptionsItemSelected(MenuItem item) { 

    switch (item.getItemId()) { 
     case R.id.hn: 
      String languageToLoad = "hi"; 
      Locale locale = new Locale(languageToLoad); 
      Locale.setDefault(locale); 
      Configuration config = new Configuration(); 
      config.locale = locale; 
      getBaseContext().getResources().updateConfiguration(config, 
        getBaseContext().getResources().getDisplayMetrics()); 
      this.setContentView(R.layout.activity_main); 
      Intent i = getBaseContext().getPackageManager() 
        .getLaunchIntentForPackage(getBaseContext().getPackageName()); 
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(i); 
      break; 
     case R.id.eng: 
      languageToLoad = "en"; // your language 
      locale = new Locale(languageToLoad); 
      Locale.setDefault(locale); 
      config = new Configuration(); 
      config.locale = locale; 
      getBaseContext().getResources().updateConfiguration(config, 
        getBaseContext().getResources().getDisplayMetrics()); 
      this.setContentView(R.layout.activity_main); 
      Intent in = getBaseContext().getPackageManager() 
        .getLaunchIntentForPackage(getBaseContext().getPackageName()); 
      in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(in); 
      break; 

     default: 

      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+0

。 – Eselfar

答えて

0

この回答を適用します部分的にはthis postに基づいています。

基本的にカスタムアプリケーションクラスを作成し、onCreateonConfigurationChangedの両方で言語を変更するメソッドを呼び出す必要があります。そうしないと、画面が回転するなど、言語がデフォルトにリセットされます。

カスタムアプリケーション

public class CustomApplication extends Application { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     initLanguage(); 
    } 

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

    private void initLanguage() { 
     String ul = LanguageHelper.getUserLanguage(this); 
     // if null the language doesn't need to be changed as the user has not chosen one. 
     if (ul != null) { 
      LanguageHelper.updateLanguage(this, ul); 
     } 
    } 
} 

次に、このカスタムクラスを使用するようにアプリを伝える必要があります。 <applicationアドオン下AndroidManifestで :

android:name=".CustomApplication"

言語ヘルパークラスユーザーが言語を選択するあなたの活動に

public class LanguageHelper { 

    private static final String GENERAL_STORAGE = "GENERAL_STORAGE"; 
    private static final String KEY_USER_LANGUAGE = "KEY_USER_LANGUAGE"; 

    /** 
    * Update the app language 
    * 
    * @param language Language to switch to. 
    */ 
    public static void updateLanguage(Context context, String language) { 
     final Locale locale = new Locale(language); 
     Locale.setDefault(locale); 

     Resources res = context.getResources(); 
     Configuration cfg = new Configuration(res.getConfiguration()); 
     cfg.locale = locale; 
     res.updateConfiguration(cfg, res.getDisplayMetrics()); 
    } 

    /** 
    * Store the language selected by the user. 
    * /!\ SHOULD BE CALLED WHEN THE USER CHOOSE THE LANGUAGE 
    */ 
    public static void storeUserLanguage(Context context, String language) { 
     context.getSharedPreferences(GENERAL_STORAGE, MODE_PRIVATE) 
       .edit() 
       .putString(KEY_USER_LANGUAGE, language) 
       .apply(); 
    } 

    /** 
    * @return The stored user language or null if not found. 
    */ 
    public static String getUserLanguage(Context context) { 
     return context.getSharedPreferences(GENERAL_STORAGE, MODE_PRIVATE) 
       .getString(KEY_USER_LANGUAGE, null); 
    } 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    String language; 
    switch (item.getItemId()) { 
     case R.id.hn: 
      language = "hi"; 
      LanguageHelper.storeUserLanguage(this, language); 
      LanguageHelper.updateLanguage(this, language); 

      // ... your code 
      break; 
     case R.id.eng: 
      language = "en"; 
      LanguageHelper.storeUserLanguage(this, language); 
      LanguageHelper.updateLanguage(this, language); 

      // ... your code 
      break; 

     default: 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+0

でこのメソッドを呼び出す方法で使用されます。 –

+0

どの方法ですか?あなたは上に必要なものすべてを持っています。あなたのアクティビティでユーザが選択したものに基づいてアプリケーション言語を変更する '' CustomApplication''の 'onLoadLanguage'を' onCreate'と 'onConfigurationChanged'に置き換えます。それはアプリ全体の言語を変更します。 – Eselfar

+0

アプリケーションは、アプリケーション全体を管理するクラスです。それは自動的に作成され、システムによって呼び出されます。このクラスは、作成しなくてもデフォルトでそこにあります。私たちの場合、新しい機能を追加するためにデフォルトのものを拡張しています。 – Eselfar

0

あなたはこの

public void saveLanguage(String lang) 
{ 
    SharedPreferences sharedpreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE); 
    Editor editor = sharedpreferences.edit(); 
    editor.putString("lang", lang); 
    editor.commit(); 
} 

のようなあなたの選択した言語の何かを保存するこの ためSharedprefrencesを使用する必要があり、アプリが起動しますたびに、言語をフェッチし、それが

public String fetchLanguage() 
{ 
    SharedPreferences sharedpreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE); 
    return sharedpreferences.getString("lang", "NA"); 
//NA is the default lang will be returned first time app started after choosing a language it wont . . 
} 
+0

ここでは、 –

+0

保存機能が上記のコードで使用され、ユーザが言語を選択し、フェッチ機能がアクティビティロードメソッド –

0

これを試してください。

LanguageUtils

public class LanguageUtils { 

/** 
* change language 
* 
* @param context 
* @param language 
*/ 
public static void shiftLanguage(Activity context, String language) { 
    if (language.equals("en")) { 
     Locale.setDefault(Locale.ENGLISH); 
     Configuration config = context.getResources().getConfiguration(); 
     config.locale = Locale.ENGLISH; 
     context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); 
     SharedPreferenceUtils.createSP(context, "en"); 
    } else { 
     Locale.setDefault(Locale.CHINESE); 
     Configuration config = context.getResources().getConfiguration(); 
     config.locale = Locale.CHINESE; 
     context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); 
     SharedPreferenceUtils.createSP(context, "zh"); 
    } 
    context.recreate(); 
} 

/** 
* app start setting language 
* 
* @param context 
* @param language 
*/ 
public static void startLanguage(Context context, String language) { 
    if (language.equals("zh")) { 
     Locale.setDefault(Locale.CHINESE); 
     Configuration config = context.getResources().getConfiguration(); 
     config.locale = Locale.CHINESE; 
     context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); 
    } else { 
     Locale.setDefault(Locale.ENGLISH); 
     Configuration config = context.getResources().getConfiguration(); 
     config.locale = Locale.ENGLISH; 
     context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); 
    } 
} 
} 

switch (item.getItemId()) { 
     case R.id.hn: 
      String languageToLoad = "hi"; 
      LanguageUtils.shiftLanguage(this, languageToLoad); 
      break; 
     case R.id.eng: 
      languageToLoad = "en"; // your language 
      LanguageUtils.shiftLanguage(this, languageToLoad); 
      break; 

     default: 
} 

そして、あなたはアプリを再起動すると、それは言語を設定しますアプリケーション

でそれを使用して使用してください。あなたが(例えば、共有環境設定で)ユーザーの選択を保存し、それに応じてアプリが起動されるたびに言語を変更する必要が

public class MyApplication extends Application { 

@Override 
public void onCreate() { 
    super.onCreate(); 
    if (TextUtils.equals(SharedPreferenceUtils.getLanguage(this), "en")) { 
     LanguageUtils.startLanguage(getApplicationContext(), "en"); 
    } else { 
     LanguageUtils.startLanguage(getApplicationContext(), "zh"); 
    } 
} 

} 

SharedPreferenceUtils

public class SharedPreferenceUtils { 

private static final String SP_NAME = "sp"; 
public static final String LANGUAGE = "language"; 


// create 
public static boolean createSP(Context context, String language) { 
    SharedPreferences.Editor editor = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE).edit(); 
    editor.putString(LANGUAGE, language); 
    return editor.commit(); 
} 


// get language 
public static String getLanguage(Context context) { 
    SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE); 
    return sp.getString(LANGUAGE, ""); 
} 

} 
関連する問題