2017-06-20 139 views
0

我想以編程方式查找我的所有應用程序。對於這一點,我已經創建了所需的文件夾:以編程方式本地化我的所有應用程序

required string folders

如果我改變我的設備的語言,該語言在我的應用程序更改。但我想選擇語言。

爲此,我創建了首選項活動。在本次活動結束後,我寫了這個代碼:

...  
mPreferenceLeguage.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 
      @Override 
      public boolean onPreferenceChange(Preference preference, Object value) { 

       leng = value.toString(); 
       int lengIndex = Integer.parseInt(leng) - 1; 

       // Update leguage summary 
       mPreferenceLeguage.setDefaultValue(lengIndex); 
       mPreferenceLeguage.setSummary(mPreferenceLeguage.getEntries()[lengIndex]); 

       if(leng.equals("1")){ 
        saveLocale("en"); 
       } else if(leng.equals("2")){ 
        saveLocale("eu"); 
       } else{ 
        saveLocale("es"); 
       } 
       return true; 
      } 
     }); 


    } 


    public void saveLocale(String lang) { 
     String langPref = "Language"; 
     SharedPreferences prefs = getSharedPreferences("CommonPrefs", 
       Activity.MODE_PRIVATE); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString(langPref, lang); 
     editor.commit(); 
    } 

而在MainActivity,下面的代碼:

... 

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main1); 
     loadLocale(); 

... 


    public void loadLocale() { 
     String langPref = "Language"; 
     SharedPreferences prefs = getSharedPreferences("CommonPrefs", 
       Activity.MODE_PRIVATE); 
     String language = prefs.getString(langPref, ""); 
     changeLang(language); 
    } 

    public void changeLang(String lang) { 
     if (lang.equalsIgnoreCase("")) 
      return; 
     myLocale = new Locale(lang); 
     Locale.setDefault(myLocale); 
     android.content.res.Configuration config = new android.content.res.Configuration(); 
     config.locale = myLocale; 
     getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics()); 
    } 

的語言改變了,但inmediately。重新啓動應用程序時,語言會發生變化。

我想這個chage將是中立的。對此,我試圖在SaveLocale()方法的末尾重新調用Preference活動中的MainActivity。

我能做些什麼來解決它?

回答

0

要皮卡配置更改,如本地化修改,請確保您已註冊爲您的活動適當的配置變化在Android清單:

https://developer.android.com/reference/android/R.attr.html#configChanges

您可以處理在配置更改您的活動(在正確註冊後)通過獲取onConfigurationChanged中的更改。

Activity#onConfigurationChanged文檔的更多信息: https://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration)

1
/**Use below code Spinet it will work fine you need to recreate your 
activity to change the language instantly without re-starting the app**/ 

Note: language ---> "en" or "eu" or "es". Based on your preference 

private void updateLanguage(String language) 
{ 
    Locale locale = new Locale(language); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); 

    SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE); 
    SharedPreferences.Editor editor = languagepref.edit(); 
    editor.putString("languageToLoad",language); 
    editor.apply(); 

    recreate(); 
} 

Override 
public void recreate() 
{ 
    if (android.os.Build.VERSION.SDK_INT >= 14) 
    { 
     super.recreate(); 
    } 
    else 
    { 
     startActivity(getIntent()); 
     finish(); 
    } 
} 
相關問題