Java Android中的自定义资源(不同的词汇表)

Java Android中的自定义资源(不同的词汇表),java,android,resources,Java,Android,Resources,我在Android java中遇到了一个问题,我想在不同的语言中使用不同的词汇表 该应用程序被德国和瑞典的军队和警察使用。 当然,德国人想要德文版的应用程序,瑞典人想要瑞典语版的应用程序,这当然是通过将字符串资源放在res/values de/strings.xml和res/values sv/strings.xml下来实现的,并通过Android设备中的不同设置来选择(请参见)。 但我的问题是,警察部队使用的词汇与军事部队不同,我想在资源中以某种方式反映这一点是否有任何方法可以通过res/va

我在Android java中遇到了一个问题,我想在不同的语言中使用不同的词汇表

该应用程序被德国和瑞典的军队和警察使用。 当然,德国人想要德文版的应用程序,瑞典人想要瑞典语版的应用程序,这当然是通过将字符串资源放在res/values de/strings.xml和res/values sv/strings.xml下来实现的,并通过Android设备中的不同设置来选择(请参见)。


但我的问题是,警察部队使用的词汇与军事部队不同,我想在资源中以某种方式反映这一点

是否有任何方法可以通过res/values de mil/strings.xml或其他任何简单的方式在app f.ex中设置不同的词汇资源。

我认为这是不可能的

一个选项是在共享首选项中设置用户的首选项(当用户第一次打开应用程序时,您可以询问),并存储用户是警察还是军队

然后可以使用反射加载字符串资源

<string name="string_policeMan">Word for a policeman</string>
<string name="string_military">Word for a military</string>   

据我所知,由于android系统不知道用户是谁,所以不可能有res/values de mil。您可以使用两组字符串来执行此方法。 例如,你有一个

使之成为


现在,如果你知道用户是军人,那么
getResources().getString(R.string.mil\u Name)

实际上有一种方法可以做到这一点,即(ab)使用数量字符串。出于学术目的,我将在下面为您提供一个概念证明,但作为预先警告:要知道,这种方法不能推广到任何地区。不过,对于德语和瑞典语,你应该没问题

对于数量字符串(或复数,因为它们通常被称为Android资源)背后的理论,请给出一个例子。它还解释了为什么下面概述的方法不能推广到任何语言环境。我将假设这一切都是有意义的,所以让我们快速进入概念证明

value de/plurals.xml

<resources>
    <plurals name="hello_im_in_the">
        <item quantity="one">Hallo, ich bin in der Armee.</item>
        <item quantity="other">Hallo, ich bin auf der Polizei.</item>
    </plurals>
</resources>
<resources>
    <plurals name="hello_im_in_the">
        <item quantity="one">Hej, jag är på militären.</item>
        <item quantity="other">Hej, jag är på polisen.</item>
    </plurals>
</resources>
以上所有操作都是使用一定数量的
1
来“选择”军事短语,并使用任何其他值(
0
在本例中)来选择特定于警察部队的短语。在本例中,我只是在运行时更改区域设置,以便我们能够并排查看结果,而不必实际进入设备的区域设置

结果:

换句话说,这表明我们能够成功地利用Android的语言环境系统,并使用预定义的数量来选择军事短语或警察部队短语进行显示


不漂亮,而且肯定不是数量字符串的用途,但它将在您的特定用例中完成工作。

看起来也是一个可行的想法,非常感谢您的及时回答。这是一个有趣的解决方案,但正如您所说,有点脏。问题是,该应用程序可能会被翻译成其他语言,但无论如何,我可能能够使用这个。非常感谢。
<resources>
    <plurals name="hello_im_in_the">
        <item quantity="one">Hej, jag är på militären.</item>
        <item quantity="other">Hej, jag är på polisen.</item>
    </plurals>
</resources>
@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.res_activity_layout);

    int policeForceQuantity = 0;
    int militaryQuantity = 1;

    TextView militaryTextViewDe = (TextView) findViewById(R.id.military_de);
    TextView policeForceTextViewDe = (TextView) findViewById(R.id.police_force_de);

    Configuration config = new Configuration(getResources().getConfiguration());
    config.locale = Locale.GERMAN;
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
    String militaryText = getResources().getQuantityString(R.plurals.hello_im_in_the, militaryQuantity);
    String policeForceText = getResources().getQuantityString(R.plurals.hello_im_in_the, policeForceQuantity);
    militaryTextViewDe.setText(militaryText);
    policeForceTextViewDe.setText(policeForceText);

    TextView militaryTextViewSv = (TextView) findViewById(R.id.military_sv);
    TextView policeForceTextViewSv = (TextView) findViewById(R.id.police_force_sv);

    config = new Configuration(getResources().getConfiguration());
    config.locale = new Locale("sv");
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
    militaryText = getResources().getQuantityString(R.plurals.hello_im_in_the, militaryQuantity);
    policeForceText = getResources().getQuantityString(R.plurals.hello_im_in_the, policeForceQuantity);
    militaryTextViewSv.setText(militaryText);
    policeForceTextViewSv.setText(policeForceText);
}