Interface Translator

All Known Subinterfaces:
GlobalTranslator, TranslationRegistry, TranslationStore<T>, TranslationStore.StringBased<T>
All Known Implementing Classes:
AbstractTranslationStore, AbstractTranslationStore.StringBased

public interface Translator
A message translator.

To see how to create a Translator with a ResourceBundle see TranslationStore.StringBased.registerAll(Locale, ResourceBundle, boolean)

To bypass vanilla's MessageFormat-based translation system, see translate(TranslatableComponent, Locale)

After creating a Translator you can add it to the GlobalTranslator to enable automatic translations by the platforms.

Since:
4.0.0
See Also:
  • Method Details

    • parseLocale

      @Nullable static @Nullable Locale parseLocale(@NotNull @NotNull String string)
      Parses a Locale from a String.
      Parameters:
      string - the string
      Returns:
      a locale
      Since:
      4.0.0
    • name

      @NotNull @NotNull Key name()
      A key identifying this translation source.

      Intended to be used for display to users.

      Returns:
      an identifier for this translation source
      Since:
      4.0.0
    • hasAnyTranslations

      @NotNull default @NotNull TriState hasAnyTranslations()
      Checks if this translator has any translations.
      Returns:
      TriState.TRUE if any, TriState.NOT_SET if unknown, or TriState.FALSE if none
      Since:
      4.15.0
    • canTranslate

      default boolean canTranslate(@NotNull @NotNull String key, @NotNull @NotNull Locale locale)
      Checks if this translator can translate the given key and locale pair.
      Parameters:
      key - the key
      locale - the locale
      Returns:
      true if this translator will return a non-null value for either of the two translate methods
      Since:
      4.20.0
    • translate

      @Nullable @Nullable MessageFormat translate(@NotNull @NotNull String key, @NotNull @NotNull Locale locale)
      Gets a message format from a key and locale.

      When used in the GlobalTranslator, this method is called only if translate(TranslatableComponent, Locale) returns null.

      Parameters:
      locale - a locale
      key - a translation key
      Returns:
      a message format or null to skip translation
      Since:
      4.0.0
    • translate

      @Nullable default @Nullable Component translate(@NotNull @NotNull TranslatableComponent component, @NotNull @NotNull Locale locale)
      Gets a translated component from a translatable component and locale.

      Care should be taken to ensure you do not unintentionally remove the children of component. This can be avoided by copying over the children using the following code as an example:

      
       final Component myNewComponent = ...; // get your component here
       return myNewComponent.append(component.children()); // ensure it has the original components children as well
       
      Parameters:
      locale - a locale
      component - a translatable component
      Returns:
      a translated component or null to use translate(String, Locale) instead (if available)
      Since:
      4.13.0