Package net.kyori.adventure.translation
Interface TranslationRegistry
-
- All Superinterfaces:
Translator
public interface TranslationRegistry extends Translator
A registry of translations. Used to register localized strings for translation keys. The registry can be submitted to theGlobalTranslator
or can translate manually throughtranslate(String, Locale)
.The recommended way to register translations is through
registerAll(Locale, ResourceBundle, boolean)
- Since:
- 4.0.0
-
-
Field Summary
Fields Modifier and Type Field Description static Pattern
SINGLE_QUOTE_PATTERN
A pattern which matches a single quote.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static @NonNull TranslationRegistry
create(net.kyori.adventure.key.Key name)
Creates a new standalone translation registry.void
defaultLocale(@NonNull Locale locale)
Sets the default locale used by this registry.void
register(@NonNull String key, @NonNull Locale locale, @NonNull MessageFormat format)
Registers a translation.default void
registerAll(@NonNull Locale locale, @NonNull Path path, boolean escapeSingleQuotes)
Registers a resource bundle of translations.default void
registerAll(@NonNull Locale locale, @NonNull Map<String,MessageFormat> formats)
Registers a map of translations.default void
registerAll(@NonNull Locale locale, @NonNull ResourceBundle bundle, boolean escapeSingleQuotes)
Registers a resource bundle of translations.default void
registerAll(@NonNull Locale locale, @NonNull Set<String> keys, Function<String,MessageFormat> function)
Registers a resource bundle of translations.@Nullable MessageFormat
translate(@NonNull String key, @NonNull Locale locale)
Gets a message format from a key and locale.void
unregister(@NonNull String key)
Unregisters a translation key.-
Methods inherited from interface net.kyori.adventure.translation.Translator
name
-
-
-
-
Field Detail
-
SINGLE_QUOTE_PATTERN
static final Pattern SINGLE_QUOTE_PATTERN
A pattern which matches a single quote.- Since:
- 4.0.0
-
-
Method Detail
-
create
static @NonNull TranslationRegistry create(net.kyori.adventure.key.Key name)
Creates a new standalone translation registry.- Returns:
- a translation registry
- Since:
- 4.0.0
-
translate
@Nullable MessageFormat translate(@NonNull String key, @NonNull Locale locale)
Gets a message format from a key and locale.If a translation for
locale
is not found, we will then trylocale
without a country code, and then finally fallback to a default locale.- Specified by:
translate
in interfaceTranslator
- Parameters:
locale
- a localekey
- a translation key- Returns:
- a message format or
null
to skip translation - Since:
- 4.0.0
-
defaultLocale
void defaultLocale(@NonNull Locale locale)
Sets the default locale used by this registry.- Parameters:
locale
- the locale to use a default- Since:
- 4.0.0
-
register
void register(@NonNull String key, @NonNull Locale locale, @NonNull MessageFormat format)
Registers a translation.final TranslationRegistry registry; registry.register("example.hello", Locale.US, new MessageFormat("Hi, {0}. How are you?"));
- Parameters:
key
- a translation keylocale
- a localeformat
- a translation format- Throws:
IllegalArgumentException
- if the translation key is already exists- Since:
- 4.0.0
-
registerAll
default void registerAll(@NonNull Locale locale, @NonNull Map<String,MessageFormat> formats)
Registers a map of translations.final TranslationRegistry registry; final Map<String, MessageFormat> translations; translations.put("example.greeting", new MessageFormat("Greetings {0}. Doing ok?)); translations.put("example.goodbye", new MessageFormat("Goodbye {0}. Have a nice day!)); registry.registerAll(Locale.US, translations);
- Parameters:
locale
- a localeformats
- a map of translation keys to formats- Throws:
IllegalArgumentException
- if a translation key is already exists- Since:
- 4.0.0
- See Also:
register(String, Locale, MessageFormat)
-
registerAll
default void registerAll(@NonNull Locale locale, @NonNull Path path, boolean escapeSingleQuotes)
Registers a resource bundle of translations.- Parameters:
locale
- a localepath
- a path to the resource bundleescapeSingleQuotes
- whether to escape single quotes- Throws:
IllegalArgumentException
- if a translation key is already exists- Since:
- 4.0.0
- See Also:
registerAll(Locale, ResourceBundle, boolean)
-
registerAll
default void registerAll(@NonNull Locale locale, @NonNull ResourceBundle bundle, boolean escapeSingleQuotes)
Registers a resource bundle of translations.It is highly recommended to create your bundle using
UTF8ResourceBundleControl
as your bundle control for UTF-8 support - for example:final ResourceBundle bundle = ResourceBundle.getBundle("my_bundle", Locale.GERMANY, UTF8ResourceBundleControl.get()); registry.registerAll(Locale.GERMANY, bundle, false);
- Parameters:
locale
- a localebundle
- a resource bundleescapeSingleQuotes
- whether to escape single quotes- Throws:
IllegalArgumentException
- if a translation key is already exists- Since:
- 4.0.0
- See Also:
UTF8ResourceBundleControl
-
registerAll
default void registerAll(@NonNull Locale locale, @NonNull Set<String> keys, Function<String,MessageFormat> function)
Registers a resource bundle of translations.- Parameters:
locale
- a localekeys
- the translation keys to registerfunction
- a function to transform a key into a message format- Throws:
IllegalArgumentException
- if a translation key is already exists- Since:
- 4.0.0
-
unregister
void unregister(@NonNull String key)
Unregisters a translation key.- Parameters:
key
- a translation key- Since:
- 4.0.0
-
-