From d5e7234ba7aba8cf4a4aa6a4b668fc0b6847b202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Wed, 31 Jan 2024 14:11:42 +0100 Subject: [PATCH] feat: store locale in translation bundle --- cloud-translations-core/build.gradle.kts | 2 +- .../ResourceBundleTranslatedCaptionProvider.java | 15 +++++++++++++-- .../ResourceBundleTranslationBundle.java | 2 +- .../translations/TranslatedCaptionProvider.java | 13 +++++++++++++ .../cloud/translations/TranslationBundle.java | 3 ++- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/cloud-translations-core/build.gradle.kts b/cloud-translations-core/build.gradle.kts index 70c8f55..842dea1 100644 --- a/cloud-translations-core/build.gradle.kts +++ b/cloud-translations-core/build.gradle.kts @@ -1,6 +1,6 @@ plugins { id("cloud-translations.base-conventions") - // id("cloud-translations.publishing-conventions") + id("cloud-translations.publishing-conventions") } dependencies { diff --git a/cloud-translations-core/src/main/java/org/incendo/cloud/translations/ResourceBundleTranslatedCaptionProvider.java b/cloud-translations-core/src/main/java/org/incendo/cloud/translations/ResourceBundleTranslatedCaptionProvider.java index 0a69ad9..9047ac8 100644 --- a/cloud-translations-core/src/main/java/org/incendo/cloud/translations/ResourceBundleTranslatedCaptionProvider.java +++ b/cloud-translations-core/src/main/java/org/incendo/cloud/translations/ResourceBundleTranslatedCaptionProvider.java @@ -23,6 +23,7 @@ // package org.incendo.cloud.translations; +import java.util.Locale; import java.util.Objects; import java.util.ResourceBundle; import org.apiguardian.api.API; @@ -34,9 +35,14 @@ final class ResourceBundleTranslatedCaptionProvider implements TranslatedCaptionProvider { private final ResourceBundle resourceBundle; + private final Locale locale; - ResourceBundleTranslatedCaptionProvider(final @NonNull ResourceBundle resourceBundle) { - this.resourceBundle = Objects.requireNonNull(resourceBundle); + ResourceBundleTranslatedCaptionProvider( + final @NonNull ResourceBundle resourceBundle, + final @NonNull Locale locale + ) { + this.resourceBundle = Objects.requireNonNull(resourceBundle, "resourceBundle"); + this.locale = Objects.requireNonNull(locale, "locale"); } @Override @@ -44,6 +50,11 @@ public boolean isEmpty() { return this.resourceBundle.keySet().isEmpty(); } + @Override + public @NonNull Locale locale() { + return this.locale; + } + @Override public @Nullable String provide(final @NonNull Caption caption, final @NonNull C recipient) { if (this.resourceBundle.containsKey(caption.key())) { diff --git a/cloud-translations-core/src/main/java/org/incendo/cloud/translations/ResourceBundleTranslationBundle.java b/cloud-translations-core/src/main/java/org/incendo/cloud/translations/ResourceBundleTranslationBundle.java index aad4713..06e2c05 100644 --- a/cloud-translations-core/src/main/java/org/incendo/cloud/translations/ResourceBundleTranslationBundle.java +++ b/cloud-translations-core/src/main/java/org/incendo/cloud/translations/ResourceBundleTranslationBundle.java @@ -67,7 +67,7 @@ final class ResourceBundleTranslationBundle implements TranslationBundle { private @NonNull TranslatedCaptionProvider loadTranslations(final @NonNull Locale locale) { try { - return new ResourceBundleTranslatedCaptionProvider<>(ResourceBundle.getBundle(this.key, locale)); + return new ResourceBundleTranslatedCaptionProvider<>(ResourceBundle.getBundle(this.key, locale), locale); } catch (final MissingResourceException ignored) { return TranslatedCaptionProvider.empty(); } diff --git a/cloud-translations-core/src/main/java/org/incendo/cloud/translations/TranslatedCaptionProvider.java b/cloud-translations-core/src/main/java/org/incendo/cloud/translations/TranslatedCaptionProvider.java index 4146b4c..3b1c874 100644 --- a/cloud-translations-core/src/main/java/org/incendo/cloud/translations/TranslatedCaptionProvider.java +++ b/cloud-translations-core/src/main/java/org/incendo/cloud/translations/TranslatedCaptionProvider.java @@ -23,6 +23,7 @@ // package org.incendo.cloud.translations; +import java.util.Locale; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.incendo.cloud.caption.Caption; @@ -53,6 +54,13 @@ public interface TranslatedCaptionProvider extends CaptionProvider { */ boolean isEmpty(); + /** + * Returns the provider locale. + * + * @return the locale + */ + @NonNull Locale locale(); + final class Empty implements TranslatedCaptionProvider { private static final Empty EMPTY = new Empty<>(); @@ -69,5 +77,10 @@ private Empty() { public boolean isEmpty() { return true; } + + @Override + public @NonNull Locale locale() { + return Locale.getDefault(); + } } } diff --git a/cloud-translations-core/src/main/java/org/incendo/cloud/translations/TranslationBundle.java b/cloud-translations-core/src/main/java/org/incendo/cloud/translations/TranslationBundle.java index 5080a23..7efa5a2 100644 --- a/cloud-translations-core/src/main/java/org/incendo/cloud/translations/TranslationBundle.java +++ b/cloud-translations-core/src/main/java/org/incendo/cloud/translations/TranslationBundle.java @@ -91,7 +91,8 @@ public interface TranslationBundle extends CaptionProvider { @Override default @Nullable String provide(final @NonNull Caption caption, final @NonNull C recipient) { - final TranslatedCaptionProvider translatedCaptionProvider = this.translations(this.localeExtractor().extract(recipient)); + final Locale locale = this.localeExtractor().extract(recipient); + final TranslatedCaptionProvider translatedCaptionProvider = this.translations(locale); if (translatedCaptionProvider == null) { return null; }