2017-11-23 3 views
-4

Ich habe eine Android-App, die zwei Sprachen enthält, möchte ich Switcher hinzufügen Wenn gedrückt, wird die Anwendung in Englisch, Wenn gedrückt, wird die Anwendung ArabischWie app Sprache ändern Switcher mit

+1

Haben Sie irgendwas versucht? Fügen Sie bitte Ihren Code hinzu –

+0

Ich habe keinen Code und würde gerne wissen, wie man ihn hinzufügt, in MainActivity ?? –

Antwort

0

Rufen Sie die unten Funktion Aktualisieren Sie die Sprache.

private void updateLanguage(String languageCode) //languageCode ex. 'en' for english 
{ 
    Context context = LocaleHelper.setLocale(MainActivity.this, languageCode); 
    Log.v("langcode", languageCode); 
    Resources resources = context.getResources(); 
} 

Erstellen Sie eine Klasse LocaleHelper genannt, die Dinge zu tun.

class LocaleHelper { 

private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language"; 

public static Context onAttach(Context context) { 
    String lang = getPersistedData(context, Locale.getDefault().getLanguage()); 
    return setLocale(context, lang); 
} 

public static Context onAttach(Context context, String defaultLanguage) { 
    String lang = getPersistedData(context, defaultLanguage); 
    return setLocale(context, lang); 
} 

public static String getLanguage(Context context) { 
    return getPersistedData(context, Locale.getDefault().getLanguage()); 
} 

public static Context setLocale(Context context, String language) { 
    persist(context, language); 

    Log.v("abcdef", Locale.getDefault().getLanguage()); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
     Log.v("Updatinglocale","Nougat"); 
     return updateResources(context, language); 
    } 

    return updateResourcesLegacy(context, language); 
} 

public static String getPersistedData(Context context, String defaultLanguage) { 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 
    return preferences.getString(SELECTED_LANGUAGE, defaultLanguage); 
} 

private static void persist(Context context, String language) { 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 
    SharedPreferences.Editor editor = preferences.edit(); 

    editor.putString(SELECTED_LANGUAGE, language); 
    editor.apply(); 
} 

@TargetApi(Build.VERSION_CODES.N) 
private static Context updateResources(Context context, String language) { 
    Locale locale = new Locale(language); 
    Locale.setDefault(locale); 

    Configuration configuration = context.getResources().getConfiguration(); 
    configuration.setLocale(locale); 

    return context.createConfigurationContext(configuration); 
} 

@SuppressWarnings("deprecation") 
private static Context updateResourcesLegacy(Context context, String language) { 
    Locale locale = new Locale(language); 
    Locale.setDefault(locale); 

    Resources resources = context.getResources(); 

    Configuration configuration = resources.getConfiguration(); 
    configuration.locale = locale; 

    resources.updateConfiguration(configuration, resources.getDisplayMetrics()); 

    return context; 
} 

} 
0

Try this ...

standardmäßig Android hält Englisch als Hauptsprache und lädt die String-Ressourcen aus res ⇒ Werte strings.xml ⇒. Wenn Sie Unterstützung für eine andere Sprache hinzufügen möchten, müssen Sie durch Anhängen eines Hyphen und die ISO-Sprachcode

In Ihrer Sprache Update-Methode

If(strlanguageToLoad.contentEquals("en"){ 
    Locale locale = new Locale(strlanguageToLoad); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    getBaseContext().getResources().updateConfiguration(config, 
     getBaseContext().getResources().getDisplayMetrics()); 

//save strlanguageToLoad in sharedPreference to check later 
    sharePref_Editor = sharePrefs.edit(); 
    sharePref_Editor.putString(“SAVED_LANGUAGE”, "en"); 
    sharePref_Editor.commit(); 

//refresh the activity 

}else{ 
//for arabic “ar” 
//Do the same here like “en” above 
} 

onCreate

prüfen einen Werte-Ordner erstellen die sharedPreference für die Sprache für jede Aktivität und die folgenden Schritte ausführen:

if (sharePrefs.getString(“SAVED_LANGUAGE”, "").equalsIgnoreCase("ar")) { 
    String languageToLoad = "ar"; // your language 
    Locale locale = new Locale(languageToLoad); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    getBaseContext().getResources().updateConfiguration(config, 
      getBaseContext().getResources().getDisplayMetrics()); 

} else { 
    String languageToLoad = "en"; 
    Locale locale = new Locale(languageToLoad); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    getBaseContext().getResources().updateConfiguration(config, 
      getBaseContext().getResources().getDisplayMetrics()); 
} 

Zur Beispielprüfung here