Skip to content

Commit

Permalink
feat: Allow specifying ClassLoader for resource loading
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Feb 29, 2024
1 parent 1e885a5 commit ec3b5fe
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public final class BukkitTranslationBundle {
public static <C> @NonNull TranslationBundle<C> bukkit(
final @NonNull LocaleExtractor<C> localeExtractor
) {
return TranslationBundle.resourceBundle("org.incendo.cloud.bukkit.lang.messages", localeExtractor);
return TranslationBundle.resourceBundle(
"org.incendo.cloud.bukkit.lang.messages",
localeExtractor,
BukkitTranslationBundle.class.getClassLoader()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public final class BungeeTranslationBundle {
public static <C> @NonNull TranslationBundle<C> bungee(
final @NonNull LocaleExtractor<C> localeExtractor
) {
return TranslationBundle.resourceBundle("org.incendo.cloud.bungee.lang.messages", localeExtractor);
return TranslationBundle.resourceBundle(
"org.incendo.cloud.bungee.lang.messages",
localeExtractor,
BungeeTranslationBundle.class.getClassLoader()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ final class ResourceBundleTranslationBundle<C> implements TranslationBundle<C> {

private final String key;
private final LocaleExtractor<C> localeExtractor;
private final ClassLoader classLoader;
private final Map<Locale, TranslatedCaptionProvider<C>> translations = new HashMap<>();

ResourceBundleTranslationBundle(
final @NonNull String baseName,
final @NonNull LocaleExtractor<C> localeExtractor
final @NonNull LocaleExtractor<C> localeExtractor,
final @NonNull ClassLoader classLoader
) {
this.key = requireNonNull(baseName, "baseName");
this.localeExtractor = requireNonNull(localeExtractor, "localeExtractor");
this.classLoader = requireNonNull(classLoader, "classLoader");
}

@Override
Expand All @@ -79,7 +82,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, new Control()),
ResourceBundle.getBundle(this.key, locale, this.classLoader, new Control()),
locale
);
} catch (final MissingResourceException ignored) {
Expand All @@ -96,7 +99,7 @@ private final class Control extends ResourceBundle.Control {
final String last = split[split.length - 1];
split[split.length - 1] = last + "-locales.list";
final String path = String.join("/", split);
final @Nullable URL url = this.getClass().getClassLoader().getResource(path);
final @Nullable URL url = ResourceBundleTranslationBundle.this.classLoader.getResource(path);
if (url == null) {
this.availableLocales = List.of();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,40 @@
public interface TranslationBundle<C> extends CaptionProvider<C> {

/**
* Creates a new translation bundle backed by a{@link java.util.ResourceBundle} with the given {@code baseName}.
* Creates a new translation bundle backed by a {@link java.util.ResourceBundle} with the given {@code baseName}.
*
* <p>The bundle will use the {@link ClassLoader} of {@link TranslationBundle} for lookups.</p>
*
* @param <C> command sender type
* @param baseName resource bundle base name
* @param localeExtractor locale extractor
* @see #resourceBundle(String, LocaleExtractor, ClassLoader)
* @return the translation bundle
*/
static <C> @NonNull TranslationBundle<C> resourceBundle(
final @NonNull String baseName,
final @NonNull LocaleExtractor<C> localeExtractor
) {
return new ResourceBundleTranslationBundle<>(baseName, localeExtractor);
return resourceBundle(baseName, localeExtractor, TranslationBundle.class.getClassLoader());
}

/**
* Creates a new translation bundle backed by a {@link java.util.ResourceBundle} with the given {@code baseName}.
*
* <p>The provided {@link ClassLoader} will be used for lookups.</p>
*
* @param <C> command sender type
* @param baseName resource bundle base name
* @param localeExtractor locale extractor
* @param classLoader class loader
* @return the translation bundle
*/
static <C> @NonNull TranslationBundle<C> resourceBundle(
final @NonNull String baseName,
final @NonNull LocaleExtractor<C> localeExtractor,
final @NonNull ClassLoader classLoader
) {
return new ResourceBundleTranslationBundle<>(baseName, localeExtractor, classLoader);
}

/**
Expand All @@ -71,7 +93,11 @@ public interface TranslationBundle<C> extends CaptionProvider<C> {
static <C> @NonNull TranslationBundle<C> core(
final @NonNull LocaleExtractor<C> localeExtractor
) {
return new ResourceBundleTranslationBundle<>("org.incendo.cloud.core.lang.messages", localeExtractor);
return new ResourceBundleTranslationBundle<>(
"org.incendo.cloud.core.lang.messages",
localeExtractor,
TranslationBundle.class.getClassLoader()
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public final class MinecraftExtrasTranslationBundle {
public static <C> @NonNull TranslationBundle<C> minecraftExtras(
final @NonNull LocaleExtractor<C> localeExtractor
) {
return TranslationBundle.resourceBundle("org.incendo.cloud.minecraft.extras.lang.messages", localeExtractor);
return TranslationBundle.resourceBundle(
"org.incendo.cloud.minecraft.extras.lang.messages",
localeExtractor,
MinecraftExtrasTranslationBundle.class.getClassLoader()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public final class VelocityTranslationBundle {
public static <C> @NonNull TranslationBundle<C> velocity(
final @NonNull LocaleExtractor<C> localeExtractor
) {
return TranslationBundle.resourceBundle("org.incendo.cloud.velocity.lang.messages", localeExtractor);
return TranslationBundle.resourceBundle(
"org.incendo.cloud.velocity.lang.messages",
localeExtractor,
VelocityTranslationBundle.class.getClassLoader()
);
}
}

0 comments on commit ec3b5fe

Please sign in to comment.