Class TranslationUtils

java.lang.Object
com.priint.pubserver.lang.TranslationUtils

public class TranslationUtils extends Object
  • Field Details

    • 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 String FALLBACK_LANGUAGE_CODE
      See Also:
  • Method Details

    • 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 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 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
    • getTranslationsUnit

      public TranslationsUnit getTranslationsUnit()
    • getTranslation

      public String getTranslation(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 String getTranslation(String identifier, String languageCode, Map<String,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 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 String getTranslation(@NotNull @NotNull String identifier, @NotNull @NotNull 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 Map<String,String> getTranslations(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>