generated from xpdustry/template-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Added distributor 3 localization
- Loading branch information
Showing
12 changed files
with
499 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...tor-core/src/main/java/com/xpdustry/distributor/core/localization/LocalizationSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.core.localization; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Locale; | ||
import org.jspecify.annotations.Nullable; | ||
|
||
/** | ||
* A helper class for adding localization support to your plugin. | ||
*/ | ||
public interface LocalizationSource { | ||
|
||
/** | ||
* Returns a {@code LocalizationSource} for the router language {@code :^)}. | ||
*/ | ||
static LocalizationSource router() { | ||
return RouterLocalizationSource.INSTANCE; | ||
} | ||
|
||
/** | ||
* Returns the localized string for the given key or {@code null} if absent. | ||
* | ||
* <pre> {@code | ||
* // Send a localized message to every player | ||
* final LocalizationSource source = ...; | ||
* Groups.player.each(player -> { | ||
* final var locale = Locale.forLanguageTag(player.locale().replace('_', '-')); | ||
* final var message = source.localize("example.key", locale); | ||
* player.sendMessage(message == null ? "???example.key???" : message); | ||
* } | ||
* } </pre> | ||
* | ||
* @param key the key of the string to localize | ||
* @return the localized string contained in a {@link MessageFormat}, or {@code null} if no string was found. | ||
*/ | ||
@Nullable MessageFormat localize(final String key, final Locale locale); | ||
|
||
/** | ||
* Shorthand method to directly format a localized string, with a failover to a default value {@code ???key???}. | ||
* | ||
* <pre> {@code | ||
* // Send a localized message to every player | ||
* final LocalizationSource source = ...; | ||
* Groups.player.each(player -> { | ||
* final var locale = Locale.forLanguageTag(player.locale().replace('_', '-')); | ||
* player.sendMessage(source.format("example.key", locale)); | ||
* } | ||
* } </pre> | ||
* | ||
* @param key the key of the string to localize | ||
* @param locale the locale to use | ||
* @param args the arguments to pass to the {@link MessageFormat#format(Object)} | ||
* @return the formatted string, or {@code ???key???} if no string was found. | ||
*/ | ||
default String format(final String key, final Locale locale, final Object... args) { | ||
final var format = this.localize(key, locale); | ||
return format == null ? "???" + key + "???" : format.format(args); | ||
} | ||
} |
169 changes: 169 additions & 0 deletions
169
.../src/main/java/com/xpdustry/distributor/core/localization/LocalizationSourceRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.core.localization; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.text.MessageFormat; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.PropertyResourceBundle; | ||
import java.util.ResourceBundle; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A mutable localization source that can register localized strings. | ||
*/ | ||
public interface LocalizationSourceRegistry extends LocalizationSource { | ||
|
||
/** | ||
* Creates a new {@code LocalizationSourceRegistry} instance. | ||
* | ||
* @param defaultLocale the default locale of the localization source | ||
* @return a new {@code LocalizationSourceRegistry} instance | ||
*/ | ||
static LocalizationSourceRegistry create(final Locale defaultLocale) { | ||
return new LocalizationSourceRegistryImpl(defaultLocale); | ||
} | ||
|
||
/** | ||
* Registers a map of localized strings. | ||
* | ||
* <pre> {@code | ||
* final var strings = new HashMap<String, MessageFormat>(); | ||
* strings.put("example.hello", new MessageFormat("Hello {0}!", Locale.ENGLISH)); | ||
* strings.put("example.goodbye", new MessageFormat("Goodbye {0}!", Locale.ENGLISH)); | ||
* registry.registerAll(Locale.ENGLISH, strings); | ||
* } </pre> | ||
* | ||
* @param locale the locale to register the strings to | ||
* @param formats the map of localized strings | ||
* @throws IllegalArgumentException if a key is already registered | ||
*/ | ||
default void registerAll(final Locale locale, final Map<String, MessageFormat> formats) { | ||
this.registerAll(locale, formats.keySet(), formats::get); | ||
} | ||
|
||
/** | ||
* Registers a resource bundle of localized strings. | ||
* | ||
* @param locale the locale to register the strings to | ||
* @param bundle the resource bundle to use | ||
* @throws IllegalArgumentException if a key is already registered | ||
*/ | ||
default void registerAll(final Locale locale, final ResourceBundle bundle) { | ||
this.registerAll(locale, bundle.keySet(), key -> new MessageFormat(bundle.getString(key), locale)); | ||
} | ||
|
||
/** | ||
* Registers a resource bundle of localized strings via the classpath. | ||
* | ||
* <pre> {@code | ||
* final Plugin plugin = ...; | ||
* registry.registerAll(Locale.ENGLISH, "bundle", plugin.getClass().getClassLoader()); | ||
* registry.registerAll(Locale.FRENCH, "bundle", plugin.getClass().getClassLoader()); | ||
* } </pre> | ||
* | ||
* @param locale the locale to register the strings to | ||
* @param baseName the base name of the resource bundle | ||
* @param loader the class loader to use | ||
* @throws IllegalArgumentException if a key is already registered | ||
*/ | ||
default void registerAll(final Locale locale, final String baseName, final ClassLoader loader) { | ||
this.registerAll(locale, ResourceBundle.getBundle(baseName, locale, loader)); | ||
} | ||
|
||
/** | ||
* Registers a resource bundle of localized strings via a file system. | ||
* | ||
* <pre> {@code | ||
* final var english = Paths.get("bundle_en.properties"); | ||
* registry.registerAll(Locale.ENGLISH, path); | ||
* final var english = Paths.get("bundle_fr.properties"); | ||
* registry.registerAll(Locale.FRENCH, path); | ||
* } </pre> | ||
* | ||
* @param locale the locale to register the strings to | ||
* @param path the path to the bundle file | ||
* @throws IllegalArgumentException if a key is already registered | ||
*/ | ||
default void registerAll(final Locale locale, final Path path) throws IOException { | ||
try (final BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) { | ||
this.registerAll(locale, new PropertyResourceBundle(reader)); | ||
} | ||
} | ||
|
||
/** | ||
* Registers a set of localized strings by using a mapping function to obtain each string. | ||
* | ||
* @param locale the locale to register the strings to | ||
* @param keys the set of keys to register | ||
* @param function the mapping function | ||
* @throws IllegalArgumentException if the key is already registered | ||
*/ | ||
default void registerAll( | ||
final Locale locale, final Set<String> keys, final Function<String, MessageFormat> function) { | ||
for (final var key : keys) { | ||
this.register(key, locale, function.apply(key)); | ||
} | ||
} | ||
|
||
/** | ||
* Registers a localized string. | ||
* | ||
* @param key the key of the string | ||
* @param locale the locale to register the string to | ||
* @param format the localized string | ||
* @throws IllegalArgumentException if the key is already registered | ||
*/ | ||
void register(final String key, final Locale locale, final MessageFormat format); | ||
|
||
/** | ||
* Checks if a key is already registered, for any locale. | ||
* | ||
* @param key the key to check | ||
* @return {@code true} if the key is already registered, {@code false} otherwise | ||
*/ | ||
boolean registered(final String key); | ||
|
||
/** | ||
* Checks if a key is already registered for a specific locale. | ||
* | ||
* @param key the key to check | ||
* @param locale the locale to check | ||
* @return {@code true} if the key is already registered for the specific locale, {@code false} otherwise | ||
*/ | ||
boolean registered(final String key, final Locale locale); | ||
|
||
/** | ||
* Unregisters a localized string. | ||
* | ||
* @param key the key of the string | ||
*/ | ||
void unregister(final String key); | ||
|
||
/** | ||
* Returns the default locale of this source. | ||
*/ | ||
Locale getDefaultLocale(); | ||
} |
Oops, something went wrong.