Class TranslationUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic TranslationsSupplierUsers might need to stub the translations for their unit tests.static final String -
Method Summary
Modifier and TypeMethodDescriptionstatic TranslationUtilsget()Creates a new TranslationUtils instance using the default translations supplierstatic TranslationUtilsget(@NotNull TranslationsSupplier translationsSupplier) Creates a new TranslationsUtils instance using the provided supplierstatic TranslationUtilsget(@NotNull Supplier<TranslationsUnit> supplier) Creates a new TranslationsUtils instance using the provided suppliergetTranslation(@NotNull String identifier, @NotNull String languageCode) Gets the translation for a given identifier and language.getTranslation(String identifier) Gets the translation for a given identifier using the UI language of the current session, if this has been set.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.Gets the translations provided by the given Supplier or configured in the given repository filesgetTranslations(String languageCode) Gets all translations for a given language as a simple Map<String,String>Gets a localized instance of this TranslationUtils instance.voidReloads the translations from this TranslationUtils instance supplier
-
Field Details
-
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
- See Also:
-
-
Method Details
-
get
Creates a new TranslationsUtils instance using the provided supplier
- Parameters:
translationsSupplier- the supplier to use- Returns:
- TranslationUtils instance with given supplier
-
get
Creates a new TranslationsUtils instance using the provided supplier
- Parameters:
supplier- the supplier to use- Returns:
- TranslationUtils instance with given supplier
-
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
Gets the translations provided by the given Supplier or configured in the given repository files
- Returns:
- all available translations as Translations object
-
getTranslationsUnit
-
getTranslation
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
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 entrylanguageCode- 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
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 entrylanguageCode- 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
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>
-