简体   繁体   English

根据Android上使用的语言,如何检查消息的正确性?

[英]How to check the correctness of the message, depending on the language used on Android?

I check the text on the screen method by Robotium: 我通过Robotium检查屏幕方法上的文本:

assertTrue(solo.waitForText("Wrong password"));

or 要么

assertTrue(solo.searchText("Wrong password"));

However, my application can work in several languages​​, and at a time working with the language specified in Android as the language used. 但是,我的应用程序可以使用多种语言工作,并且一次可以使用Android中指定的语言作为使用的语言。 Application is written in Java. 应用程序是用Java编写的。 How to check the correctness of the message, depending on the language used on Android? 根据Android上使用的语言,如何检查消息的正确性?

You can reach the string resources of the original project from the test project, so you can access to the ids of the original strings. 您可以从测试项目中访问原始项目的字符串资源,因此可以访问原始字符串的ID。 If you would like support more languages than you should use localized string resources, here is the link: http://developer.android.com/guide/topics/resources/localization.html 如果您想要支持的语言多于应使用本地化字符串资源的语言,请访问以下链接: http : //developer.android.com/guide/topics/resources/localization.html

If you want to get localised String without changing your current configuration (API>=17): 如果要在不更改当前配置的情况下获取本地化的String(API> = 17):

Configuration config = new Configuration(context.getResources().getConfiguration()); 
config.setLocale(locale);   
return context.createConfigurationContext(config).getText(resId);

Where locale is one you want to test against. 您要针对哪个locale进行测试。

If you're targeting lower API levels: 如果您定位较低的API级别:

Configuration conf = context.getResources().getConfiguration();
conf.locale = new Locale("de");
// Then update resources based on this Configuration. 
context.getResources().updateConfiguration(conf, context.getResources().getDisplayMetrics());
// Afterwards, resources will retrieve localised strings instead of default locale
String deLocaleString = context.getResources().getString(R.string.my_string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM