572 Stimmen

App-Sprache in Android programmatisch ändern

Ist es möglich, die Sprache einer App programmatisch zu ändern und dabei weiterhin Android-Ressourcen zu verwenden?

Wenn nicht, ist es möglich, eine Ressource in einer bestimmten Sprache anzufordern?

Ich möchte, dass der Benutzer die Sprache der App von der App aus ändern kann.

1voto

ashishdhiman2007 Punkte 735

Dies funktioniert, wenn ich die Taste drücken, um Text Sprache meiner TextView ändern.(strings.xml in Werte-de Ordner)

String languageToLoad = "de"; // your language
Configuration config = getBaseContext().getResources().getConfiguration();
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
recreate();

1voto

Baljinder Maan Punkte 31

Hinzufügen LocaleHelper Klasse

public 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);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return updateResources(context, language);
    }

    return updateResourcesLegacy(context, language);
}

private 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);
    configuration.setLayoutDirection(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;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLayoutDirection(locale);
    }

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

    return context;
}
}

In Aktivität oder Fragment

Context context = LocaleHelper.setLocale(this, App.getSharedPre().getLanguage());
Resource resources = context.getResources();

Jetzt SetText für jeden Text

TextView tv = findViewById(R.id.tv);
tv.setText(resources.getString(R.string.tv));

1voto

Kamran Gasimov Punkte 867

Für mich ist das die beste Lösung: https://www.bitcaal.com/how-to-change-the-app-language-programmatically-in-Android/

package me.mehadih.multiplelanguage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setApplicationLocale("az"); // short name of language. "en" for English
        setContentView(R.layout.activity_main);

    }

    private void setApplicationLocale(String locale) {
        Resources resources = getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = resources.getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(new Locale(locale.toLowerCase()));
        } else {
            config.locale = new Locale(locale.toLowerCase());
        }
        resources.updateConfiguration(config, dm);
    }
}

1voto

Gunhan Punkte 6241

Es gibt einige Schritte, die Sie durchführen sollten

Zunächst müssen Sie das Gebietsschema Ihrer Konfiguration ändern

Resources resources = context.getResources();

Configuration configuration = resources.getConfiguration();
configuration.locale = new Locale(language);

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

Zweitens, wenn Sie möchten, dass Ihre Änderungen direkt auf das sichtbare Layout angewendet werden, können Sie entweder die Ansichten direkt aktualisieren oder Sie können einfach activity.recreate() aufrufen, um die aktuelle Aktivität neu zu starten.

Außerdem müssen Sie Ihre Änderungen beibehalten, denn wenn der Benutzer Ihre Anwendung schließt, geht die Sprachänderung verloren.

Eine ausführlichere Lösung habe ich in meinem Blogbeitrag Sprache in Android programmatisch ändern

Im Grunde rufen Sie einfach LocaleHelper.onCreate() in Ihrer Anwendungsklasse auf, und wenn Sie das Gebietsschema im laufenden Betrieb ändern möchten, können Sie LocaleHelper.setLocale() aufrufen

0voto

zgr Punkte 147

Für androidx.appcompat:appcompat Benutzer, obige Lösungen funktionieren nach Version 1.3.0 . Wie erwähnt in aquí .

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X