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

2024-04-02

我创建了三个本地化文件:"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
        )

但 updateConfiguration 已被弃用,我寻找了一些可以工作的新变体。然后我发现this https://stackoverflow.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated问题但这对我没有帮助。我需要在 RV 项目、活动和片段上设置语言,但我做不到。为什么会发生这种情况以及如何解决这个问题?


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

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

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;
}
}

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

然后在每个活动中,您需要重写此方法(我建议您只创建一个每个活动扩展的基本活动):

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleHelper.onAttach(base));
}

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

public static Resources getResources(Context context) {
    return LocaleHelper.onAttach(context).getResources();
}

所以现在我可以使用这个方法而不是通常的 getResources()。

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

我稍后会发布其中一些内容的链接......

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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的应用程序从本地化资源中获取错误的字符串? 的相关文章