Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain strings in all languages in Robolectric

I want to make a Robolectric test to ensure that all strings in French contains the same number of placeholders (i'm talking about %1$s) like the strings in English.

I tried to add @Config(qualifiers="fr-land") but it gives all strings in English (default) and only gives in French the strings that are not defined in English.

Also tried to create a new Resource object and to provide custom local - seems to work in Android but not in Robolectric.

Please help!

like image 545
Buda Florin Avatar asked Apr 30 '14 15:04

Buda Florin


2 Answers

try to call following snippet at each test. this was reported as a workaround here https://github.com/robolectric/robolectric/issues/914

public static void setLocale( final Context context, final String language )
{
    final Locale locale = new Locale( language );
    Robolectric.shadowOf( context.getResources().getConfiguration() ).setLocale( locale );
    Locale.setDefault( locale );
}
like image 148
nenick Avatar answered Oct 18 '22 20:10

nenick


I'm using Robolectric 3.1 and nenick's solution didn't work for me, but the following solution did:

private void setLocale(Locale locale) {
    Locale.setDefault(locale);
    RuntimeEnvironment.setQualifiers(locale.getLanguage());
}
like image 43
ThomasW Avatar answered Oct 18 '22 21:10

ThomasW