我在Android java中遇到问题,我希望在不同的语言中使用不同的词汇表。
该应用程序由德国和瑞典的军队和警察部队使用。
当然,德国人想要德语应用程序,瑞典人希望用瑞典语进行应用程序,当然要把字符串资源放在res / values-de / strings.xml和res / values-sv / strings.xml下来处理。由Android设备中的不同设置选择(请参阅Resource handling)。
但我的问题是,警察使用的词汇量不同于我希望以某种方式反映在资源中的军事力量。
是否有任何方法可以通过res / values-de-mil / strings.xml或任何简单的方式在app f.ex中设置不同的词汇资源。
答案 0 :(得分:1)
我不认为这是可能的。
一个选项是在共享首选项中设置用户的首选项(当用户第一次打开应用程序时可以询问)并存储(如果是警察或军人)。
然后你可以使用反射加载你的字符串资源。
<string name="string_policeMan">Word for a policeman</string>
<string name="string_military">Word for a military</string>
SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
boolean isPoliceMan = shared.getBoolean(policeMan, false);
int resId;
if(isPoliceMan)
resId = R.string.class.getField("string_policeMan").getInt(null);
else
resId = R.string.class.getField("string_military").getInt(null);
String field = context.getResources().getString(resId);
答案 1 :(得分:1)
实际上有一种方法可以做到这一点,即通过( ab )使用数量字符串。仅出于学术目的,我将在下面给出一个概念证明,但作为预先警告:知道这种方法不概括为任何语言环境。但是,对于德语和瑞典语,你应该没问题。
对于数量字符串背后的理论(或复数形式,因为它们通常被称为Android资源),请使用read here。它还解释了为什么下面概述的方法不能推广到任何语言环境。我将假设这一切都有意义,所以让我们快速进行概念验证。
<强>值-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>
<强>值-SV / plurals.xml 强>
<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>
让我们在具有四个TextView
s的活动中使用上述资源:每个区域设置两个,每个“强制”一个(警察与军队)。
@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);
}
以上所做的就是使用一定数量的1
来“选择”军事短语,以及特定于政治力量的短语中的任何其他值(本例中为0
)。为了这个例子,我只是在运行时更改语言环境,这样我们就可以并排查看结果,而无需实际进入设备的语言环境设置。
<强>结果:强>
换句话说,这表明我们能够成功利用Android的语言环境系统,并使用预定义的数量来选择军事短语或警力短语进行显示。
不漂亮,绝对不是数字字符串的用途,但它会在您的特定用例中完成工作。
答案 2 :(得分:0)
据我所知,由于android系统不知道用户是谁,因此不可能有res / values-de-mil。你可以做这种方法有2套字符串。 例如,你有一个
<string name="Name">...</string>
将其设为
<string name="mil_Name">...</string>
<string name="cop_Name">...</string>
现在,如果你知道用户是军人,那么getResources().getString(R.string.mil_Name);