From ac3bffb484b6c4fe02aec4cd326c1fc35c6bbe10 Mon Sep 17 00:00:00 2001 From: Aurora Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Sun, 9 Jun 2024 10:23:22 +0300 Subject: [PATCH] Fix a problem with French translation The locale file didn't contain correct punctuation for SimpleDateFormat. Wrote a test that catches these regressions in the future. This test failed before the fix to locale_FR.yml Affects issues: - Fixed #3658 --- .../plan/settings/locale/LocaleSystem.java | 2 +- .../assets/plan/locale/locale_FR.yml | 2 +- .../delivery/formatting/FormattersTest.java | 71 +++++++++++++++++++ .../java/extension/FullSystemExtension.java | 6 ++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 Plan/common/src/test/java/com/djrapitops/plan/delivery/formatting/FormattersTest.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/settings/locale/LocaleSystem.java b/Plan/common/src/main/java/com/djrapitops/plan/settings/locale/LocaleSystem.java index 47b002ccec..ca5f60efb1 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/settings/locale/LocaleSystem.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/settings/locale/LocaleSystem.java @@ -215,7 +215,7 @@ private void convertFromLegacyFormat() { } } - private Optional loadSettingLocale() { + public Optional loadSettingLocale() { try { String setting = config.get(PluginSettings.LOCALE); if ("write-all".equalsIgnoreCase(setting)) { diff --git a/Plan/common/src/main/resources/assets/plan/locale/locale_FR.yml b/Plan/common/src/main/resources/assets/plan/locale/locale_FR.yml index c4984fbff4..08f094bbda 100644 --- a/Plan/common/src/main/resources/assets/plan/locale/locale_FR.yml +++ b/Plan/common/src/main/resources/assets/plan/locale/locale_FR.yml @@ -905,7 +905,7 @@ plugin: loadedServerInfo: "Identifiant de serveur chargé : ${0}" loadingServerInfo: "Chargement des informations d'identification du serveur" no: "Non" - today: "Aujourd'hui" + today: "'Aujourd''hui'" unavailable: "Indisponible" unknown: "Inconnu" yes: "Oui" diff --git a/Plan/common/src/test/java/com/djrapitops/plan/delivery/formatting/FormattersTest.java b/Plan/common/src/test/java/com/djrapitops/plan/delivery/formatting/FormattersTest.java new file mode 100644 index 0000000000..c86a6dd899 --- /dev/null +++ b/Plan/common/src/test/java/com/djrapitops/plan/delivery/formatting/FormattersTest.java @@ -0,0 +1,71 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.delivery.formatting; + +import com.djrapitops.plan.delivery.formatting.time.SecondFormatter; +import com.djrapitops.plan.delivery.formatting.time.YearFormatter; +import com.djrapitops.plan.settings.config.PlanConfig; +import com.djrapitops.plan.settings.config.paths.FormatSettings; +import com.djrapitops.plan.settings.config.paths.PluginSettings; +import com.djrapitops.plan.settings.locale.LangCode; +import com.djrapitops.plan.settings.locale.Locale; +import com.djrapitops.plan.settings.locale.LocaleSystem; +import extension.FullSystemExtension; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; +import org.junit.jupiter.api.extension.ExtendWith; + +import java.util.Arrays; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +/** + * Tests against formatting issues. + * + * @author AuroraLS3 + */ +@ExtendWith(FullSystemExtension.class) +class FormattersTest { + + @TestFactory + @DisplayName("Locale for recent day names does not cause formatting issues.") + Stream localeFormatRegressionTest(PlanConfig config, LocaleSystem localeSystem) { + config.set(FormatSettings.DATE_RECENT_DAYS, true); + config.set(FormatSettings.DATE_FULL, "MMM d YYYY, HH:mm:ss"); + config.set(FormatSettings.DATE_NO_SECONDS, "MMM d YYYY, HH:mm"); + config.set(FormatSettings.DATE_RECENT_DAYS_PATTERN, "MMM d YYYY"); + return Arrays.stream(LangCode.values()) + .filter(langCode -> langCode != LangCode.CUSTOM) + .map(langCode -> + DynamicTest.dynamicTest("Lang " + langCode.name() + " does not cause formatting issues with plugin.generic.today or plugin.generic.yesterday lang keys.", () -> { + config.set(PluginSettings.LOCALE, langCode.name()); + Locale locale = localeSystem.loadSettingLocale().orElseThrow(() -> new AssertionError("Could not load " + langCode + "locale")); + YearFormatter yearFormatter = new YearFormatter(config, locale); + SecondFormatter secondFormatter = new SecondFormatter(config, locale); + long today = System.currentTimeMillis(); + assertDoesNotThrow(() -> yearFormatter.apply(today), "plugin.generic.today is causing Formatting issues for lang " + langCode); + assertDoesNotThrow(() -> secondFormatter.apply(today), "plugin.generic.today is causing Formatting issues for lang " + langCode); + long yesterday = today - TimeUnit.DAYS.toMillis(1L) - 1000L; + assertDoesNotThrow(() -> yearFormatter.apply(yesterday), "plugin.generic.yesterday is causing Formatting issues for lang " + langCode); + assertDoesNotThrow(() -> secondFormatter.apply(yesterday), "plugin.generic.yesterday is causing Formatting issues for lang " + langCode); + }) + ); + } +} \ No newline at end of file diff --git a/Plan/common/src/test/java/extension/FullSystemExtension.java b/Plan/common/src/test/java/extension/FullSystemExtension.java index fb169045a3..8553a25189 100644 --- a/Plan/common/src/test/java/extension/FullSystemExtension.java +++ b/Plan/common/src/test/java/extension/FullSystemExtension.java @@ -20,11 +20,13 @@ import com.djrapitops.plan.commands.PlanCommand; import com.djrapitops.plan.delivery.DeliveryUtilities; import com.djrapitops.plan.delivery.export.Exporter; +import com.djrapitops.plan.delivery.formatting.Formatters; import com.djrapitops.plan.delivery.webserver.Addresses; import com.djrapitops.plan.identification.ServerUUID; import com.djrapitops.plan.settings.ConfigSystem; import com.djrapitops.plan.settings.config.PlanConfig; import com.djrapitops.plan.settings.config.paths.WebserverSettings; +import com.djrapitops.plan.settings.locale.LocaleSystem; import com.djrapitops.plan.storage.database.Database; import com.djrapitops.plan.storage.file.PlanFiles; import com.djrapitops.plan.storage.file.PublicHtmlFiles; @@ -57,6 +59,8 @@ public class FullSystemExtension implements ParameterResolver, BeforeAllCallback private final Map parameterResolvers; public FullSystemExtension() { + // You can't use method references here because planSystem is null when this method is initialized. + this.parameterResolvers = Maps.builder(Class.class, Supplier.class) .put(PlanSystem.class, () -> planSystem) .put(PlanFiles.class, () -> planSystem.getPlanFiles()) @@ -79,6 +83,8 @@ public FullSystemExtension() { }) .put(Database.class, () -> planSystem.getDatabaseSystem().getDatabase()) .put(DeliveryUtilities.class, () -> planSystem.getDeliveryUtilities()) + .put(Formatters.class, () -> planSystem.getDeliveryUtilities().getFormatters()) + .put(LocaleSystem.class, () -> planSystem.getLocaleSystem()) .put(Addresses.class, () -> planSystem.getDeliveryUtilities().getAddresses()) .put(PublicHtmlFiles.class, () -> planSystem.getDeliveryUtilities().getPublicHtmlFiles()) .put(Webserver.class, () -> planSystem.getWebServerSystem().getWebServer())