首页 > 解决方案 > 我如何才能为我的 androidTest 提供本地化文本字符串

问题描述

我想要一些特定于语言的测试数据,并将它们放在特定值的 strings.xml 中-LANG 文件夹会是完美的,但我只希望这些测试数据可用于 androidTest 而不是主代码。这可以以某种方式完成吗?


谢谢索伦_

标签: androidandroid-espressoandroid-testing

解决方案


所以

您可以这样做并在运行测试后将其更改回来吗?

 Resources res = context.getResources();
 // Change locale settings in the app.
 DisplayMetrics dm = res.getDisplayMetrics();
 android.content.res.Configuration conf = res.getConfiguration();
 conf.setLocale(new Locale(language_code.toLowerCase())); // API 17+ only.
 // Use conf.locale = new Locale(...) if targeting lower versions
res.updateConfiguration(conf, dm);

推荐阅读