Class TranslationBundle

java.lang.Object
nl.colorize.util.TranslationBundle

public final class TranslationBundle extends Object
Used to access text with different translations for different locale. Each text string is identified by a key rather than the actual text. The bundle will then return the matching text for the requested locale, with a fallback to the bundle's default translation if no specific translation is available for the requested locale. The text can contain placeholders that follow the MessageFormat notation.

The purpose of this class is similar to ResourceBundle. Older versions of this library used to contain a direct ResourceBundle subclass, but ResourceBundle is very old and contains a lot of obscure functionality. This class provides a similar API to ResourceBundle, and supports loading text from properties files. However, it is more flexible and allows to load text from various other sources. It also allows custom file names and locations, instead of the predefined naming convention required by ResourceBundle.

  • Method Details

    • withTranslation

      public TranslationBundle withTranslation(Locale locale, TranslationBundle translation)
      Returns a new TranslationBundle that adds the specified translation. This TranslationBundle's default translation will act as a fallback for any keys that are not included in the translation.
      Throws:
      IllegalArgumentException - if this TranslationBundle already includes a translation for the same locale.
    • getText

      public String getText(Locale locale, String key, Object... params)
      Returns the text string with the specified key, for the requested locale. If no suitable translation exists, the default translation is used as a fallback. If the default translation is also not available, the key is returned as a last resort.
    • getText

      public String getText(String key, Object... params)
      Returns the text string with the specified key for the default translation. If no translation is also available, the key is returned as a fallback.
    • getString

      public String getString(String key, Object... params)
      Provided for API compatibility with ResourceBundle and the DynamicResourceBundle subclass that used to be included in previous versions of this library. This method will redirect to getText(Locale, String, Object...) and will use the default translation.
    • getKeys

      public Set<String> getKeys(Locale locale)
      Returns all translation keys that exist for the translation for the specified locale, using the default translation as a fallback where necessary. The keys will be returned in random order.
    • getKeys

      public Set<String> getKeys()
      Returns all translation keys that exist for the default translation. The keys will be returned in random order.
    • from

      public static TranslationBundle from(Properties defaultTranslation)
      Returns a TranslationBundle that will use the specified Properties as its default translation. Additional translations can be added afterward.
    • from

      public static TranslationBundle from(ResourceFile propertiesFile)
      Loads the specified .properties file, then uses the resulting properties as the default translation for a TranslationBundle. Additional translations can be added afterward.

      PropertyUtils.loadProperties(ResourceFile) is used to load the file. Refer to the documentation of that method for more information on supported character encodings for different platforms.

    • fromPropertiesFile

      public static TranslationBundle fromPropertiesFile(String propertiesFileContents)
      Returns a TranslationBundle that loads the contents of a .properties file, then uses the resulting properties as the default translation. Additional translations can be added afterward.

      PropertyUtils.loadProperties(String) is used to load the file. Refer to the documentation of that method for more information on supported character encodings for different platforms.

    • fromMap

      public static TranslationBundle fromMap(Map<String,String> defaultTranslation)
      Returns a TranslationBundle that will use the key/value properties in the specified map as its default translation. Additional translations can be added afterward.