@Override public String translate(LocaleProvider localeProvider, String[] keys) { return translate(localeProvider, null, keys); }
@Override public String translate(LocaleProvider localeProvider, String basename, String[] keys) { if (translationOff) { return keys == null || keys.length == 0 ? null : keys[0]; } else { return super.translate(localeProvider, basename, keys); } }
@Test public void handleLegacyTranslationSanitation() throws Exception { // GIVEN / WHEN String notSanitisedHarmlessCode = translationService.translate(userLocaleProvider, LEGACY_BUNDLE, new String[] { "foo" }); String sanitizedMaliciousHtml = translationService.translate(userLocaleProvider, LEGACY_BUNDLE, new String[] { "bar" }); String weirdChoiceFormat = translationService.translate(userLocaleProvider, LEGACY_BUNDLE, new String[] { "baz" }); String imageTagShouldBeSanitized = translationService.translate(userLocaleProvider, LEGACY_BUNDLE, new String[] { "bax" }); // THEN assertThat(sanitizedMaliciousHtml, is("")); assertThat(notSanitisedHarmlessCode, is("<strong>This will stay strong</strong>")); assertThat(imageTagShouldBeSanitized, is("")); assertThat(weirdChoiceFormat, is("Failed to mark {0,choice,1# folder|1< folders} as deleted.")); }
@Test public void messageFallsBackToClosestAvailableMessage() { assertEquals("Version Deutsch Schweiz", translationService.translate(newFixedLocaleProvider(new Locale("de", "CH", "Non-Existant-Variant")), new String[] { "about.version" })); assertEquals("Only German", translationService.translate(newFixedLocaleProvider(new Locale("de", "CH", "Non-Existant-Variant")), new String[] { "only.de" })); assertEquals("Only in English", translationService.translate(newFixedLocaleProvider(new Locale("de", "CH", "Non-Existant-Variant")), new String[] { "only.en" })); }
@Test public void shouldPreferUserLocaleWithSecondaryKey() { // GIVEN final String[] keys = new String[] { "only.en", "about.version" }; // WHEN final String translated = translationService.translate(userLocaleProvider, keys); // THEN assertEquals("Version Deutsch", translated); }
@Test public void messageWithNoKeys() { try { translationService.translate(userLocaleProvider, new String[0]); fail("should have failed"); } catch (IllegalArgumentException e) { assertEquals("Keys can't be null or empty", e.getMessage()); } }
@Test public void messageWithNullKeys() { try { translationService.translate(userLocaleProvider, null, null); fail("should have failed"); } catch (IllegalArgumentException e) { assertEquals("Keys can't be null or empty", e.getMessage()); } }
@Test public void messageWithBasenameAndLocaleFallsBackToDefaultLocaleIfNotFound() throws Exception { // WHEN String message = translationService.translate(newFixedLocaleProvider(USERLOCALE), LEGACY_BUNDLE, new String[] { "only.en" }); // THEN assertEquals("Only in English legacy", message); }
@Test public void messageFallsBackToDefaultLanguage() { // this key doesn't exist in the french message bundle assertEquals("Version English", translationService.translate(newFixedLocaleProvider(Locale.FRENCH), new String[] { "about.version" })); }
@Test public void messageWithBasename() throws Exception { // WHEN String message = translationService.translate(newFixedLocaleProvider(USERLOCALE), LEGACY_BUNDLE, new String[] { "about.title" }); // THEN assertEquals("Das ist ein Test legacy", message); }
@Test public void givenBasenameFallbacksToDefaultLegacy() throws Exception { // WHEN String message = translationService.translate(newFixedLocaleProvider(USERLOCALE), LEGACY_BUNDLE, new String[] { "about.version" }); // THEN // This is not the behavior we want, but this is the behavior of MessagesManager. assertEquals("Version English from default bundle", message); }
@Test public void messageWithBasenameAndLocaleFallsBackToGlobalBundleIfNotFound() throws Exception { // WHEN String message = translationService.translate(newFixedLocaleProvider(USERLOCALE), LEGACY_BUNDLE, new String[] { "only.in.global.bundle" }); // THEN assertEquals("I am only in global bundle English", message); }
@Test public void messageFallsBackToDefautLocaleFromAVariantLocale() { assertEquals("Only in English", translationService.translate(newFixedLocaleProvider(GERMAN_CH_BASEL), new String[] { "only.en" })); }
@Test public void messageWithGermanLocale() { assertEquals("Das ist ein Titel", translationService.translate(newFixedLocaleProvider(Locale.GERMAN), new String[] { "about.title" })); }
@Test public void messageWithSwissGermanVariantLocale() { assertEquals("Titel Schweiz Basel", translationService.translate(newFixedLocaleProvider(GERMAN_CH_BASEL), new String[] { "about.title" })); }
@Test public void messageNotFound() { assertEquals("not.there", translationService.translate(newFixedLocaleProvider(GERMAN_CH_BASEL), new String[] { "not.there" })); }
@Test public void messageNotFoundWithBasename() { assertEquals("not.there", translationService.translate(newFixedLocaleProvider(GERMAN_CH_BASEL), LEGACY_BUNDLE, new String[] { "not.there" })); }
@Test public void debugNewI18nMissingTranslation() throws Exception { //GIVEN i18nModule.setDebug(true); // WHEN String message = translationService.translate(newFixedLocaleProvider(USERLOCALE), new String[] { "nonexisting.key" }); // THEN assertEquals("nonexisting.key\n[nonexisting.key]Locale:de", message); }
@Test public void nullKey() throws Exception { //GIVEN NullKeyGenerator keyGenerator = new NullKeyGenerator(); String[] keys = keyGenerator.keysFor(null, null, null); // WHEN translationService.translate(newFixedLocaleProvider(Locale.ENGLISH), null, keys); // THEN doesn't fail with NPE in TranslationServiceImpl.doGetMessage() }
@Test public void debugNewI18n() throws Exception { //GIVEN i18nModule.setDebug(true); // WHEN String message = translationService.translate(newFixedLocaleProvider(USERLOCALE), new String[] { "more.specific.key", "specific.key", "key" }); // THEN assertEquals("Some translation\n[more.specific.key, >specific.key<, key]Locale:en", message); }