make LocalizationTest log the troublesome string locale and key
This commit is contained in:
parent
dc6eccc218
commit
edc9941e8b
@ -9,6 +9,7 @@ import android.support.test.InstrumentationRegistry;
|
|||||||
import android.support.test.runner.AndroidJUnit4;
|
import android.support.test.runner.AndroidJUnit4;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
|
import android.util.Log;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -16,6 +17,7 @@ import org.junit.runner.RunWith;
|
|||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.IllegalFormatException;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@ -97,33 +99,33 @@ public class LocalizationTest {
|
|||||||
// Resources() requires DisplayMetrics, but they are only needed for drawables
|
// Resources() requires DisplayMetrics, but they are only needed for drawables
|
||||||
resources = new Resources(assets, new DisplayMetrics(), config);
|
resources = new Resources(assets, new DisplayMetrics(), config);
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
//Log.i(TAG, field.getName());
|
|
||||||
int resId = field.getInt(int.class);
|
int resId = field.getInt(int.class);
|
||||||
for (int quantity = 0; quantity < 22; quantity++) {
|
for (int quantity = 0; quantity < 22; quantity++) {
|
||||||
resources.getQuantityString(resId, quantity);
|
resources.getQuantityString(resId, quantity);
|
||||||
}
|
}
|
||||||
|
|
||||||
String formats = haveFormats.get(field.getName());
|
String formats = haveFormats.get(field.getName());
|
||||||
String formattedString = null;
|
try {
|
||||||
switch (formats) {
|
switch (formats) {
|
||||||
case "d":
|
case "d":
|
||||||
formattedString = resources.getQuantityString(resId, 1, 1);
|
resources.getQuantityString(resId, 1, 1);
|
||||||
break;
|
break;
|
||||||
case "s":
|
case "s":
|
||||||
formattedString = resources.getQuantityString(resId, 1, "ONE");
|
resources.getQuantityString(resId, 1, "ONE");
|
||||||
break;
|
break;
|
||||||
case "ds":
|
case "ds":
|
||||||
formattedString = resources.getQuantityString(resId, 2, 1, "TWO");
|
resources.getQuantityString(resId, 2, 1, "TWO");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (!TextUtils.isEmpty(formats)) {
|
if (!TextUtils.isEmpty(formats)) {
|
||||||
throw new IllegalStateException("Pattern not included in tests: " + formats);
|
throw new IllegalStateException("Pattern not included in tests: " + formats);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (formattedString != null) { // NOPMD
|
} catch (IllegalFormatException e) {
|
||||||
//Log.i(TAG, locale + " " + field.getName() + " FORMATTED: " + formattedString);
|
Log.i(TAG, locale + " " + field.getName());
|
||||||
|
throw new IllegalArgumentException("Bad '" + formats + "' format in " + locale + " "
|
||||||
|
+ field.getName() + ": " + e.getMessage());
|
||||||
}
|
}
|
||||||
//Log.i(TAG, field.getName() + ": " + string);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,52 +161,51 @@ public class LocalizationTest {
|
|||||||
haveFormats.put(field.getName(), new String(formats).trim());
|
haveFormats.put(field.getName(), new String(formats).trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
//for (Locale locale : new Locale[]{new Locale("es")}) {
|
|
||||||
for (Locale locale : locales) {
|
for (Locale locale : locales) {
|
||||||
config.locale = locale;
|
config.locale = locale;
|
||||||
// Resources() requires DisplayMetrics, but they are only needed for drawables
|
// Resources() requires DisplayMetrics, but they are only needed for drawables
|
||||||
resources = new Resources(assets, new DisplayMetrics(), config);
|
resources = new Resources(assets, new DisplayMetrics(), config);
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
//Log.i(TAG, field.getName());
|
|
||||||
int resId = field.getInt(int.class);
|
int resId = field.getInt(int.class);
|
||||||
resources.getString(resId);
|
resources.getString(resId);
|
||||||
|
|
||||||
String formats = haveFormats.get(field.getName());
|
String formats = haveFormats.get(field.getName());
|
||||||
String formattedString = null;
|
try {
|
||||||
switch (formats) {
|
switch (formats) {
|
||||||
case "d":
|
case "d":
|
||||||
formattedString = resources.getString(resId, 1);
|
resources.getString(resId, 1);
|
||||||
break;
|
break;
|
||||||
case "dd":
|
case "dd":
|
||||||
formattedString = resources.getString(resId, 1, 2);
|
resources.getString(resId, 1, 2);
|
||||||
break;
|
break;
|
||||||
case "s":
|
case "s":
|
||||||
formattedString = resources.getString(resId, "ONE");
|
resources.getString(resId, "ONE");
|
||||||
break;
|
break;
|
||||||
case "ss":
|
case "ss":
|
||||||
formattedString = resources.getString(resId, "ONE", "TWO");
|
resources.getString(resId, "ONE", "TWO");
|
||||||
break;
|
break;
|
||||||
case "sss":
|
case "sss":
|
||||||
formattedString = resources.getString(resId, "ONE", "TWO", "THREE");
|
resources.getString(resId, "ONE", "TWO", "THREE");
|
||||||
break;
|
break;
|
||||||
case "ssss":
|
case "ssss":
|
||||||
formattedString = resources.getString(resId, "ONE", "TWO", "THREE", "FOUR");
|
resources.getString(resId, "ONE", "TWO", "THREE", "FOUR");
|
||||||
break;
|
break;
|
||||||
case "ssd":
|
case "ssd":
|
||||||
formattedString = resources.getString(resId, "ONE", "TWO", 3);
|
resources.getString(resId, "ONE", "TWO", 3);
|
||||||
break;
|
break;
|
||||||
case "sssd":
|
case "sssd":
|
||||||
formattedString = resources.getString(resId, "ONE", "TWO", "THREE", 4);
|
resources.getString(resId, "ONE", "TWO", "THREE", 4);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (!TextUtils.isEmpty(formats)) {
|
if (!TextUtils.isEmpty(formats)) {
|
||||||
throw new IllegalStateException("Pattern not included in tests: " + formats);
|
throw new IllegalStateException("Pattern not included in tests: " + formats);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (formattedString != null) { //NOPMD
|
} catch (Exception e) {
|
||||||
//Log.i(TAG, "FORMATTED: " + formattedString);
|
Log.i(TAG, locale + " " + field.getName());
|
||||||
}
|
throw new IllegalArgumentException("Bad format in '" + locale + "' '" + field.getName() + "': "
|
||||||
//Log.i(TAG, field.getName() + ": " + string);
|
+ e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user