Class TranslationUtils


  • public class TranslationUtils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static TranslationUtils get()
      Creates a new TranslationUtils instance using the default translations supplier
      static TranslationUtils get​(@NotNull TranslationsSupplier translationsSupplier)
      Creates a new TranslationsUtils instance using the provided supplier
      static TranslationUtils get​(@NotNull java.util.function.Supplier<TranslationsUnit> supplier)
      Creates a new TranslationsUtils instance using the provided supplier
      java.lang.String getTranslation​(@NotNull java.lang.String identifier, @NotNull java.lang.String languageCode)
      Gets the translation for a given identifier and language.
      java.lang.String getTranslation​(java.lang.String identifier)
      Gets the translation for a given identifier using the UI language of the current session, if this has been set.
      java.lang.String getTranslation​(java.lang.String identifier, java.lang.String languageCode, java.util.Map<java.lang.String,​java.lang.String> parameters)
      Gets the translation with replaced parameters for a given identifier using the given language If language is empty or no translation can be found, the identifier is returned.
      java.util.List<Translation> getTranslations()
      Gets the translations provided by the given Supplier or configured in the given repository files
      java.util.Map<java.lang.String,​java.lang.String> getTranslations​(java.lang.String languageCode)
      Gets all translations for a given language as a simple Map<String,String>
      TranslationsUnit getTranslationsUnit()  
      TranslationUtils localized​(@NotNull java.lang.String languageCode)
      Gets a localized instance of this TranslationUtils instance.
      void reloadTranslations()
      Reloads the translations from this TranslationUtils instance supplier
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • defaultTranslationsSupplier

        public static TranslationsSupplier defaultTranslationsSupplier

        Users might need to stub the translations for their unit tests. As this is a static class with static methods, a static supplier setting is offered. This should only be set if a manual override of Translations providers is required which usually only happens in test scopes.

      • FALLBACK_LANGUAGE_CODE

        public static final java.lang.String FALLBACK_LANGUAGE_CODE
        See Also:
        Constant Field Values
    • Method Detail

      • get

        public static TranslationUtils get​(@NotNull
                                           @NotNull TranslationsSupplier translationsSupplier)

        Creates a new TranslationsUtils instance using the provided supplier

        Parameters:
        translationsSupplier - the supplier to use
        Returns:
        TranslationUtils instance with given supplier
      • get

        public static TranslationUtils get​(@NotNull
                                           @NotNull java.util.function.Supplier<TranslationsUnit> supplier)

        Creates a new TranslationsUtils instance using the provided supplier

        Parameters:
        supplier - the supplier to use
        Returns:
        TranslationUtils instance with given supplier
      • get

        public static TranslationUtils get()

        Creates a new TranslationUtils instance using the default translations supplier

        Returns:
        TranslationUtils instance with default supplier
      • reloadTranslations

        public void reloadTranslations()

        Reloads the translations from this TranslationUtils instance supplier

      • getTranslations

        public java.util.List<Translation> getTranslations()

        Gets the translations provided by the given Supplier or configured in the given repository files

        Returns:
        all available translations as Translations object
      • getTranslation

        public java.lang.String getTranslation​(java.lang.String identifier)

        Gets the translation for a given identifier using the UI language of the current session, if this has been set. If language is empty or no translation can be found, the identifier is returned.

        Parameters:
        identifier - identifier of the translation entry
        Returns:
        localized String or identifier, if no language is set for this session or no translation can be found
      • getTranslation

        public java.lang.String getTranslation​(java.lang.String identifier,
                                               java.lang.String languageCode,
                                               java.util.Map<java.lang.String,​java.lang.String> parameters)

        Gets the translation with replaced parameters for a given identifier using the given language If language is empty or no translation can be found, the identifier is returned.

        Parameters:
        identifier - identifier of the translation entry
        languageCode - 2 digit language code, e.g. "en" or "de"
        parameters - Key Value List of parameters to replace
        Returns:
        localized String or identifier, if no language is set for this session or no translation can be found
      • localized

        public TranslationUtils localized​(@NotNull
                                          @NotNull java.lang.String languageCode)

        Gets a localized instance of this TranslationUtils instance.

        The result contains language entries for the given language only, so that the following code snippets lead to the same result: TranslationUtils allTranslations; // ... TranslationUtils localizedTranslations = allTranslations.localized("en"); String label = allTranslations.getTranslation("identifier", "en"); String label2 = localizedTranslations.getTranslation("identifier");

        Parameters:
        languageCode - the language code to extract
        Returns:
        localized TranslationUtils instance
      • getTranslation

        public java.lang.String getTranslation​(@NotNull
                                               @NotNull java.lang.String identifier,
                                               @NotNull
                                               @NotNull java.lang.String languageCode)

        Gets the translation for a given identifier and language. If either language is null or empty, identifier cannot be found or no translation can be found for this identifier / language, the original identifier is returned.

        Parameters:
        identifier - identifier of the translation entry
        languageCode - 2 digit language code, e.g. "en" or "de"
        Returns:
        translation for the given identifier / language, or identifier, if no translation can be found
      • getTranslations

        public java.util.Map<java.lang.String,​java.lang.String> getTranslations​(java.lang.String languageCode)

        Gets all translations for a given language as a simple Map<String,String>

        Gets all available translations for a given language as a String Map using the entry identifier as identifier and translation as value

        Parameters:
        languageCode - 2 digit language code, e.g. "en" or "de"
        Returns:
        all translations as Map<String,String>