I18nMap implements Iterable<Map.Entry<Locale, String>> { public String valueFor(Locale locale) { String result = values.get(locale); if (result == null && !locale.getVariant().isEmpty()) { Locale fallbackLocale = new Locale(locale.getLanguage(), locale.getCountry()); result = values.get(fallbackLocale); } if (result == null && !locale.getCountry().isEmpty()) { Locale fallbackLocale = new Locale(locale.getLanguage()); result = values.get(fallbackLocale); } if (result == null) { result = value(); } return result; } I18nMap(Map<Locale, String> values);  I18nMap(String value); String value(); String valueFor(Locale locale); @Override String toString(); @Override Iterator<Map.Entry<Locale, String>> iterator(); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test public void exactLocaleMatch() { I18nMap value = new I18nMap(ImmutableMap.<Locale, String>builder() .put(AUSTRALIAN_STRINE_LOCALE, "g'day") .put(AUSTRALIAN_LOCALE, "hello") .put(ENGLISH_LOCALE, "greetings") .put(DEFAULT_LOCALE, "guten morgen") .put(Locale.ITALIAN, "ciao").build()); assertEquals("g'day", value.valueFor(AUSTRALIAN_STRINE_LOCALE)); assertEquals("hello", value.valueFor(AUSTRALIAN_LOCALE)); assertEquals("greetings", value.valueFor(ENGLISH_LOCALE)); }
@Test public void fallbackToCountry() { I18nMap value = new I18nMap(ImmutableMap.<Locale, String>builder() .put(AUSTRALIAN_LOCALE, "hello") .put(ENGLISH_LOCALE, "greetings") .put(DEFAULT_LOCALE, "guten morgen") .put(Locale.ITALIAN, "ciao").build()); assertEquals("hello", value.valueFor(AUSTRALIAN_STRINE_LOCALE)); }
@Test public void fallbackToLanguage() { I18nMap value = new I18nMap(ImmutableMap.<Locale, String>builder() .put(ENGLISH_LOCALE, "greetings") .put(DEFAULT_LOCALE, "guten morgen") .put(Locale.ITALIAN, "ciao").build()); assertEquals("greetings", value.valueFor(AUSTRALIAN_STRINE_LOCALE)); assertEquals("greetings", value.valueFor(AUSTRALIAN_LOCALE)); }
@Test public void fallbackEnglish() { I18nMap value = new I18nMap(ImmutableMap.<Locale, String>builder() .put(ENGLISH_LOCALE, "greetings") .put(Locale.ITALIAN, "ciao").build()); assertEquals("greetings", value.valueFor(Locale.CHINA)); }
@Test public void fallbackDefault() { I18nMap value = new I18nMap(ImmutableMap.<Locale, String>builder() .put(ENGLISH_LOCALE, "greetings") .put(DEFAULT_LOCALE, "guten morgen") .put(Locale.ITALIAN, "ciao").build()); assertEquals("guten morgen", value.valueFor(Locale.CHINA)); }
@Test public void fallbackToAvailable() { I18nMap value = new I18nMap(ImmutableMap.<Locale, String>builder() .put(Locale.ITALIAN, "ciao").build()); assertEquals("ciao", value.valueFor(Locale.CHINA)); }
@Test public void emptyStringIfUnresolvable() { I18nMap value = new I18nMap(Collections.<Locale, String>emptyMap()); assertEquals("", value.valueFor(Locale.CHINA)); }