Android:限制应用程序中的语言

时间:2012-06-22 11:50:38

标签: android android-menu

是否可以限制Android应用程序中的语言,即当用户更改语言时我只想要英语和法语语言需要生效。问题是当用户选择荷兰语时,选项菜单名称是英文但 MORE(系统生成的菜单选项,如果有更多选项可供显示)是荷兰语。我希望更多是英语。
请帮助
NITZ

3 个答案:

答案 0 :(得分:8)

您可以在应用中以编程方式设置区域设置:

  Configuration configuration = context.getResources().getConfiguration();
  configuration.locale = new Locale(selectedLanguage);
  context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());

其中selectedLanguage是一个包含语言缩写的变量,而context包含对Context的引用。

这些关于SO的早期答案进一步讨论了可能的解决方案:

Change language settings (locale) for the device

Changing Locale within the app itself

Change language programmatically in Android

答案 1 :(得分:1)

当您按下菜单键时,选项中显示的“更多”一词是系统生成的字符串,因此无论您的应用支持什么,它都将匹配系统语言。

如果您想要以“法语”或“英语”显示“更多”这个词,您必须减少所拥有的菜单项数量,以便系统不会显示它,然后手动添加“更多” “ 选项。您手动添加的选项不会自动显示其余菜单选项,因此您必须将它们移动到自己的上下文菜单中。

答案 2 :(得分:0)

我认为您可以通过两种方式解决您的问题。

首先,将“更多”资源字符串更改为资源文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"tools:ignore="MissingTranslation">
    <string name="more_btn">More</string>
</resources>

或者,您可以简单地将字符串值复制到values-fr和values-en目录中。