Skip to content

Commit

Permalink
feat: store locale in translation bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Jan 31, 2024
1 parent 0694c6c commit d5e7234
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cloud-translations-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id("cloud-translations.base-conventions")
// id("cloud-translations.publishing-conventions")
id("cloud-translations.publishing-conventions")
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,16 +35,26 @@
final class ResourceBundleTranslatedCaptionProvider<C> implements TranslatedCaptionProvider<C> {

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
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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ final class ResourceBundleTranslationBundle<C> implements TranslationBundle<C> {

private @NonNull TranslatedCaptionProvider<C> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,6 +54,13 @@ public interface TranslatedCaptionProvider<C> extends CaptionProvider<C> {
*/
boolean isEmpty();

/**
* Returns the provider locale.
*
* @return the locale
*/
@NonNull Locale locale();

final class Empty<C> implements TranslatedCaptionProvider<C> {

private static final Empty<?> EMPTY = new Empty<>();
Expand All @@ -69,5 +77,10 @@ private Empty() {
public boolean isEmpty() {
return true;
}

@Override
public @NonNull Locale locale() {
return Locale.getDefault();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public interface TranslationBundle<C> extends CaptionProvider<C> {

@Override
default @Nullable String provide(final @NonNull Caption caption, final @NonNull C recipient) {
final TranslatedCaptionProvider<C> translatedCaptionProvider = this.translations(this.localeExtractor().extract(recipient));
final Locale locale = this.localeExtractor().extract(recipient);
final TranslatedCaptionProvider<C> translatedCaptionProvider = this.translations(locale);
if (translatedCaptionProvider == null) {
return null;
}
Expand Down

0 comments on commit d5e7234

Please sign in to comment.