Android - Unit testing localizations
From what I can tell, there is no way to load a string from a specific locale's language file.
What I want to do is run all my locales' (I have 24 开发者_开发知识库of them) time format strings through unit tests to make sure they don't crash. For example, here's a date format string in French: "'Hier à' h:mm a".
These really need to be unit tested, because translators aren't great about properly escaping these strings, causing crashes unless we manually verify every string in every language every time we get a translation pass back.
This question has a sort-of answer. Is that really the only way? Is there a better way to specify the locale of a Context in unit tests?
Have you tried the following? Change language programmatically in Android
Robolectric supports this from v2.0 alpha 3. See here:
http://robolectric.blogspot.com/2013/05/robolectric-20-alpha-3.html
All you need to do is add an annotation to your test method:
@Test @Config(qualifiers = "fr")
public void testGetValuesResFromSpecificQualifiers() {
assertThat(resources.getString(R.string.hello)).isEqualTo("Bonjour");
}
精彩评论