android - 从资源中加载语言特定的字符串?

标签 android string resources

在我的一个 Android 应用程序中,有一次我需要加载特定语言的字符串。例如:

 values:    <string name="txt_help">Help</string>
 values-de: <string name="txt_help">Hilfe</string>
 values-fr: <string name="txt_help">Aider</string>

现在我需要默认(值)文本。

有没有办法用给定的语言“en”加载键“txt_help”?我找不到可以将语言环境设置为附加参数的方法。

或者我可以用给定的语言环境创建一个新的 ResourceManager 并通过这个资源对象读取字符串吗?

提前非常感谢。 hjw

最佳答案

查看 the documentation ,这看起来很有希望:

Resources standardResources = context.getResources();
AssetManager assets = standardResources.getAssets();
DisplayMetrics metrics = standardResources.getDisplayMetrics();
Configuration config = new Configuration(standardResources.getConfiguration());
config.locale = Locale.US;
Resources defaultResources = new Resources(assets, metrics, config);

换句话说,创建一个配置为标准语言环境的新资源对象。

关于android - 从资源中加载语言特定的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5244889/

相关文章:

android - 适用于 Android 的 Bluemix 推送通知未发送任何通知

c# - 无法从使用 C# 开发的 Android 应用程序接收 json

c# - 使用 StringComparison.Ordinal 对 String 进行排序的最简单方法

java - Spring SpEL 不适用于 <mvc :resources> location parameter

android - 如何在 NativeScript 项目中从 JS 访问 Android 颜色资源?

android - 错误 "package android.support.v7.app does not exist"

android - 鼠标指针在 Genymotion 虚拟机中不可见/不可用

javascript - JS : need a css color code based on a string

java - JLabel:重音字母问题

Java try/catch/finally 获取/关闭资源时的最佳实践