Add l10n text and parameter container
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 18 Nov 2016 19:23:14 +0000 (20:23 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 18 Nov 2016 19:23:14 +0000 (20:23 +0100)
src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java
src/main/java/net/pterodactylus/sone/freenet/L10nText.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt

index 3b4af2a..e56321d 100644 (file)
@@ -53,6 +53,13 @@ public class L10nFilter implements Filter {
         */
        @Override
        public String format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
+               if (data instanceof L10nText) {
+                       L10nText l10nText = (L10nText) data;
+                       if (l10nText.getParameters().isEmpty()) {
+                               return webInterface.getL10n().getString(l10nText.getText());
+                       }
+                       return new MessageFormat(webInterface.getL10n().getString(l10nText.getText()), new Locale(webInterface.getL10n().getSelectedLanguage().shortCode)).format(l10nText.getParameters().toArray());
+               }
                if (parameters.isEmpty()) {
                        return webInterface.getL10n().getString(String.valueOf(data));
                }
diff --git a/src/main/java/net/pterodactylus/sone/freenet/L10nText.kt b/src/main/java/net/pterodactylus/sone/freenet/L10nText.kt
new file mode 100644 (file)
index 0000000..0c975be
--- /dev/null
@@ -0,0 +1,6 @@
+package net.pterodactylus.sone.freenet
+
+/**
+ * Container for an l10n key and optional values.
+ */
+data class L10nText(val text: String, val parameters: List<Any?> = emptyList())
index f070df2..d5d801e 100644 (file)
@@ -46,4 +46,16 @@ class L10nFilterTest {
                assertThat(filter.format(templateContext, "data", mapOf("0" to 4.5, "1" to "data")), equalTo("translated 4.5 data"))
        }
 
+       @Test
+       fun `filter processes l10n text without parameters correctly`() {
+               translations["data"] = "translated data"
+           assertThat(filter.format(templateContext, L10nText("data"), emptyMap()), equalTo("translated data"))
+       }
+
+       @Test
+       fun `filter processes l10n text with parameters correctly`() {
+               translations["data"] = "translated {0,number} {1}"
+               assertThat(filter.format(templateContext, L10nText("data", listOf(4.5, "data")), emptyMap()), equalTo("translated 4.5 data"))
+       }
+
 }