Class PropertiesTranslationProvider

java.lang.Object
eu.cloudnetservice.driver.language.PropertiesTranslationProvider
All Implemented Interfaces:
TranslationProvider

public final class PropertiesTranslationProvider extends Object implements TranslationProvider
An implementation of a translation provider which loads the translations from a properties source and uses a message format to insert arguments into the translations. This provider supports a special argument placeholder for the message format: {[index]$[name]$}, for example {0$name$}. These placeholders are just replaced with the provided index (so in the example the replacement would be {0}) to simplify the understanding which argument actually is represented by the index.
Since:
4.0
  • Field Details

    • TRANSLATION_ARG_FORMAT_PATTERN

      private static final Pattern TRANSLATION_ARG_FORMAT_PATTERN
    • translationFormatLock

      private final Lock translationFormatLock
    • translations

      private final Map<String,Object> translations
  • Constructor Details

    • PropertiesTranslationProvider

      private PropertiesTranslationProvider(@NonNull @NonNull Map<String,Object> translations)
      Constructs a new translation provider instance for the given translations. This constructor is sealed as the map value type is very open and should only be set by trusted code.
      Parameters:
      translations - the translations that are available to this provider.
      Throws:
      NullPointerException - if the given translations map is null.
  • Method Details

    • fromProperties

      @NonNull public static @NonNull TranslationProvider fromProperties(@NonNull @NonNull InputStream inputStream)
      Loads a properties document in UTF-8 format from the given input stream. The keys of the properties are used as the translation keys while the values are used as the actual translations.
      Parameters:
      inputStream - the input stream from which the properties should be loaded.
      Returns:
      a translation provider based on the properties provided by the given input stream.
      Throws:
      NullPointerException - if the given input stream is null.
      IllegalArgumentException - if the translations couldn't be loaded from the stream.
    • fromProperties

      @NonNull public static @NonNull TranslationProvider fromProperties(@NonNull @NonNull Reader reader)
      Loads a properties document from the given reader. The keys of the properties are used as the translation keys while the values are used as the actual translations.
      Parameters:
      reader - the reader from which the properties should be loaded.
      Returns:
      a translation provider based on the properties provided by the given reader.
      Throws:
      NullPointerException - if the given reader is null.
      IllegalArgumentException - if the translations couldn't be loaded from the stream.
    • fromProperties

      @NonNull public static @NonNull TranslationProvider fromProperties(@NonNull @NonNull Properties properties)
      Constructs a translation provider based on the given properties. The keys of the properties are used as the translation keys while the values are used as the actual translations.
      Parameters:
      properties - the properties which hold the key-translation mappings for the provider.
      Returns:
      a translation provider based on the given properties.
      Throws:
      NullPointerException - if the given properties instance is null.
      IllegalArgumentException - if the translations couldn't be loaded from properties.
    • translate

      @Nullable public @Nullable String translate(@NonNull @NonNull String key, Object... args)
      Get the translation for the given key with the given arguments inserted into it. This method returns null in case the given translation key is not mapped to a translation in this provider.
      Specified by:
      translate in interface TranslationProvider
      Parameters:
      key - the key of the translation to get.
      args - the arguments that can be inserted into the message.
      Returns:
      the translated message, filled with the given arguments or null if the message key is unknown.