为什么我的应用从本地化资源中获取错误的字符串?

And*_*hko 4 android

我创建了三个用于本地化的文件:"en", "ru""uk"我看到问题应用程序可以得到一些正确的字符串和一些错误的字符串。这是什么意思?例如,一个布局在其工作期间可以包含多个语言环境而不是一个,这是错误的。我看到一些用户通过添加一些代码行解决了这个问题,buil.gradle但也许我们有另一个解决方案,为什么几周前当我的项目使用 Java 时它运行良好。我确信它不会由从一种语言转移到另一种语言引起。在 recyclerView 我可以看到类似的问题。在活动中,我可以像这样设置我当前的语言:

sp = this.getSharedPreferences("app_data", 0)
        val lang = sp!!.getString("language", Locale.getDefault().language)
        val locale = Locale(lang)
        Locale.setDefault(locale)
        val config = Configuration()
        config.setLocale(locale)

        resources.updateConfiguration(
                config, resources.displayMetrics
        )
Run Code Online (Sandbox Code Playgroud)

但是 updateConfiguration 已被弃用,我寻找了一些可以工作的新变体。然后我发现了这个问题,但它没有帮助我。我需要在 RV 项目、活动和片段中设置语言,但我做不到。为什么会发生,我该如何解决这个问题?

Zee*_*Zee 5

我实际上只需要自己做这件事。这是一种痛苦,尤其是对于较新的设备。

首先,您需要一个语言环境助手:

package za.co.overtake.onlinetrucks.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;

import java.util.Locale;

/**
 * This class is used to change your application locale and persist this change for the next time
 * that your app is going to be used.
 * <p/>
 * You can also change the locale of your application on the fly by using the setLocale method.
 * <p/>
 * Created by gunhansancar on 07/10/15.
 */
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;
}
}
Run Code Online (Sandbox Code Playgroud)

这负责切换语言以及使用已弃用或未弃用的方法。

然后在每个活动中,您需要覆盖此方法(我建议您只制作每个活动扩展的基本活动):

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleHelper.onAttach(base));
}
Run Code Online (Sandbox Code Playgroud)

这应该处理大多数情况。但是,我使用 ViewModels,出于某种原因,当我执行 getApplication.getResources().getString() 时,它有时会返回错误的字符串。在这种情况下,我创建了一个 Utility 方法来解决这个问题:

public static Resources getResources(Context context) {
    return LocaleHelper.onAttach(context).getResources();
}
Run Code Online (Sandbox Code Playgroud)

所以现在我可以使用这个方法来代替通常的 getResources()。

这是一个真正的痛苦,但这是我可以在高于 android 8 和低于 android8 的两种设备上进行翻译的唯一方法。

稍后我将发布其中一些的链接...

编辑:另外,就像之前的答案一样,您需要重新创建您更改语言的活动。

链接:https : //proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758

https://gunhansancar.com/change-language-programmatically-in-android/