java - Android 仅在应用程序中更改语言 : Some strings not translated properly

标签 java android string android-fragments localization

(我在帖子底部添加了更新)

我目前正在从事一个支持多种语言的项目。我已经为所有语言编写了所有字符串。

我使用 BaseActivity、Application 类和 LocaleHelper 来管理我的应用程序的本地化。 然后我在我的应用程序上发现了这种行为。在我将 Locale 设置为另一种语言后,某些 Activity 中的字符串已更改并正确,但在 MainActivity 中却没有,即使它们具有一些相同的字符串。我尝试重新启动应用程序,但它仍然无法正常工作。

我还有另一个 Activity , Activity 中的一些词得到了翻译,但 fragment 和 recyclerview 中的所有词根本没有被翻译。

我仍然找不到它没有正确翻译的原因。有人可以帮助我吗??

这是一些 fragment :

应用类

@Override
public void onCreate() {
    super.onCreate();
    LocaleUtil.setLocale(new Locale(LocaleUtil.with(this).getPreference()));
    LocaleUtil.updateConfig(this,getBaseContext().getResources().getConfiguration());
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    LocaleUtil.setLocale(new Locale(LocaleUtil.with(this).getPreference()));
    LocaleUtil.updateConfig(this, newConfig);
}

基础 Activity

public BaseActivity() {
    LocaleUtil.updateConfig(this);
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    initLocale();
    super.onCreate(savedInstanceState);
}

public void initLocale(){
    localeUtil = new LocaleUtil(this);
    localeUtil.setListener(this);
    localeUtil.setLanguageByCode(localeUtil.getPreference());
}

请帮帮我!谢谢!!

(更新)

我发现每当我想访问 Strings.xml 以获取 recyclerview 和 fragment 中的任何 View 时,我都必须再次设置区域设置。这完全不方便,我担心性能。我很想听到更好的建议来改变语言。

最佳答案

检查一下。也许不包括某些字符串。 来自文档。

https://developer.android.com/guide/topics/resources/localization.html#using-framework

Suppose that your application's default language is English. Suppose also that you want to localize all the text in your application to French, and most of the text in your application (everything except the application's title) to Japanese. In this case, you could create three alternative strings.xml files, each stored in a locale-specific resource directory:

res/values/strings.xml Contains English text for all the strings that the application uses, including text for a string named title. res/values-fr/strings.xml Contain French text for all the strings, including title. res/values-ja/strings.xml Contain Japanese text for all the strings except title. If your Java code refers to R.string.title, here is what will happen at runtime:

If the device is set to any language other than French, Android will load title from the res/values/strings.xml file. If the device is set to French, Android will load title from the res/values-fr/strings.xml file. Notice that if the device is set to Japanese, Android will look for title in the res/values-ja/strings.xml file. But because no such string is included in that file, Android will fall back to the default, and will load title in English from the res/values/strings.xml file.

我希望它能奏效。

关于java - Android 仅在应用程序中更改语言 : Some strings not translated properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47087904/

相关文章:

javascript - 连接 UTF-8 字符串

java - 我如何处理使用 Jaxb 解析的 xml 文件而不是从根元素解析

java - 从不同的目录编译java文件

android - MoPub SDK : how to remove the video ads ?

android - xamarin 应用程序很大,为什么以及它们可以更小

javascript - 如何在Javascript中更改数组中字符串的颜色,以便它可以在浏览器上显示

java - Scala 中的拆分正则表达式保留分隔符

java - jhat OQL AND in where 子句

java - 使用类型化类和 rdf 创建 RDF 模型 :id

android - RecyclerView ItemTouchHelper 轻扫