Interface I18nService
Using property files for localization is described in Internationalising your plugin.
Fallback messages should never be specified by internal developers. All internal messages should be added to properties files for translation. The variants accepting a fallback message are intended for external plugin developers only. Internal developers should use:
Plugin developers are also encouraged to use properties files but the ability to specify the message directly in the call is provided as a convenience, especially for in-house plugins which will only be used by speakers of a single language.-
Method Summary
Modifier and TypeMethodDescriptioncreateKeyedMessage
(String key, Object... arguments) Given a property key and a list of arguments, it returns: aKeyedMessage
that composes the localized and formatted message for the current locale, the localized and formatted message for the root locale and the original key for the message.getAllTranslationsForPrefix
(String prefix) Given a property key prefix, this method will return all translations where the key starts with the given prefix as key to value mappings using the current locale.getAllTranslationsForPrefix
(String prefix, Locale locale) Given a property key prefix, this method will return all translations where the key starts with the given prefix as key -> value mappings.getKeyedText
(I18nKey i18nKey) Given anI18nKey
it returns: aKeyedMessage
that composes the localized and formatted message for the current locale, the localized and formatted message for the root locale and the original key for the message.getKeyedText
(String key, String fallbackMessage, Object... arguments) Given a property key, a fallback message and a list of arguments, it returns: aKeyedMessage
that composes the localized and formatted message for the current locale, the localized and formatted message for the root locale and the original key for the message.getMessage
(I18nKey key) Given akey
it returns: the localized text formatted with the given arguments if the key can be resolved.getMessage
(String key, Object... arguments) Given a property key and a list of arguments, it returns: the localized text formatted with the given arguments if the key can be resolved.getMessage
(Locale locale, String key, Object... arguments) Given a locale, a property key and a list of arguments, it returns: the localized text formatted with the given arguments if the key can be resolved.getMessagePattern
(String key) Given a property key it returns: the associated message pattern if the key can be resolved.getMessagePattern
(String key, String fallbackPattern) Given a property key and the fallback text message, it returns: the associated message pattern if the key can be resolved.getMessagePattern
(Locale locale, String key) Given a locale and a property key it returns: the localized message pattern if the key can be resolved.Given a property key, the fallback text message and a list of arguments, it returns: the localized text formatted with the given arguments if the key can be resolved.Given a property key, a locale, a fallback text message and a list of arguments, it returns: the localized text formatted with the given arguments if the key can be resolved.
-
Method Details
-
createKeyedMessage
Given a property key and a list of arguments, it returns:- a
KeyedMessage
that composes the localized and formatted message for the current locale, the localized and formatted message for the root locale and the original key for the message. Otherwise... - if the message lookup for the key fails a KeyedMessage with the given key and a string representation of the arguments supplied
The KeyedMessage returned is useful for priming Exceptions where the localized and non-localized messages need to differ to ensure the end use sees the message localized in their language but the logs record the message for the root locale.
- Parameters:
key
- the key for the i18ned messagearguments
- optional list of arguments for the message.- Returns:
- a KeyedMessage
- a
-
getMessage
Given a property key and a list of arguments, it returns:- the localized text formatted with the given arguments if the key can be resolved. Otherwise...
- the key and a string representation of the arguments supplied
- Parameters:
key
- the key for the i18ned messagearguments
- optional list of arguments for the message.- Returns:
- I18ned string
-
getMessage
Given akey
it returns:- the localized text formatted with the given arguments if the key can be resolved. Otherwise...
- the key and a string representation of the arguments supplied
- Parameters:
key
- the key for the i18ned message- Returns:
- I18ned string
-
getMessage
@Nonnull String getMessage(@Nonnull Locale locale, @Nonnull String key, @Nullable Object... arguments) Given a locale, a property key and a list of arguments, it returns:- the localized text formatted with the given arguments if the key can be resolved. Otherwise...
- the key and a string representation of the arguments supplied
- Parameters:
locale
- the locale for the lookupkey
- the key for the i18ned messagearguments
- optional list of arguments for the message.- Returns:
- I18ned string
- Since:
- 5.3
-
getText
@Nonnull String getText(@Nonnull Locale locale, @Nonnull String key, @Nullable String fallbackMessage, @Nonnull Object... arguments) Given a property key, a locale, a fallback text message and a list of arguments, it returns:- the localized text formatted with the given arguments if the key can be resolved. Otherwise...
- the fallbackMessage formatted with the arguments will be returned if non-null. Otherwise...
- the key and a string representation of the arguments supplied
- Parameters:
locale
- the locale for the lookupkey
- key for the i18ned messagefallbackMessage
- the optional message to fallback to if lookup failsarguments
- optional list of arguments for the message.- Returns:
- I18ned string
-
getText
@Nonnull String getText(@Nonnull String key, @Nullable String fallbackMessage, @Nonnull Object... arguments) Given a property key, the fallback text message and a list of arguments, it returns:- the localized text formatted with the given arguments if the key can be resolved. Otherwise...
- the fallbackMessage formatted with the arguments will be returned if non-null. Otherwise...
- the key and a string representation of the arguments supplied
- Parameters:
key
- the key for the i18ned messagefallbackMessage
- the optional message to fallback to if lookup failsarguments
- optional list of arguments for the message.- Returns:
- I18ned string
-
getMessagePattern
Given a property key it returns:- the associated message pattern if the key can be resolved. Otherwise...
- null
- Parameters:
key
- the key for the i18n pattern- Returns:
- a message pattern suitable for use with
java.text.MessageFormat
or withAJS.format()
-
getMessagePattern
Given a property key and the fallback text message, it returns:- the associated message pattern if the key can be resolved. Otherwise...
- the fallbackPattern
- Parameters:
key
- the key for the i18n patternfallbackPattern
- the optional pattern to fallback to if lookup fails- Returns:
- a message pattern suitable for use with
java.text.MessageFormat
or withAJS.format()
-
getMessagePattern
Given a locale and a property key it returns:- the localized message pattern if the key can be resolved. Otherwise...
- null
- Parameters:
locale
- the locale for the lookupkey
- the key for the i18n pattern- Returns:
- a message pattern suitable for use with
java.text.MessageFormat
or withAJS.format()
-
getKeyedText
Given anI18nKey
it returns:- a
KeyedMessage
that composes the localized and formatted message for the current locale, the localized and formatted message for the root locale and the original key for the message. Otherwise... - if the message lookup for the key fails a KeyedMessage with the given key and a string representation of the arguments supplied
The KeyedMessage returned is useful for priming Exceptions where the localized and non-localized messages need to differ to ensure the end use sees the message localized in their language but the logs record the message for the root locale. The result of this method should be the same as calling
#createKeyedMessage(i18nKey.getKey(), i18nKey.getArguments())
- Parameters:
i18nKey
- the key for the i18ned message- Returns:
- a KeyedMessage
- a
-
getKeyedText
@Nonnull KeyedMessage getKeyedText(@Nonnull String key, @Nullable String fallbackMessage, @Nonnull Object... arguments) Given a property key, a fallback message and a list of arguments, it returns:- a
KeyedMessage
that composes the localized and formatted message for the current locale, the localized and formatted message for the root locale and the original key for the message. Otherwise... - if the message lookup for the key fails for either the current locale or the root locale, the formatted fallback message is used if non-null. Otherwise...
- a KeyedMessage with the given key and a string representation of the arguments supplied
The KeyedMessage returned is useful for priming Exceptions where the localized and non-localized messages need to differ to ensure the end user sees the message localized in their language but the logs record the message for the root locale.
- Parameters:
key
- the key for the i18ned messagefallbackMessage
- the optional message to fallback to if lookup failsarguments
- optional list of arguments for the message.- Returns:
- a KeyedMessage
- a
-
getAllTranslationsForPrefix
Given a property key prefix, this method will return all translations where the key starts with the given prefix as key to value mappings using the current locale. The current locale used is the one for the request currently in scope or the default locale if that is not available.- Parameters:
prefix
- the prefix for a particular key to start with. Empty string will match everything, which may be slow. ThrowsNullPointerException
ifnull
.- Returns:
- a
Map
of i18nKey to translation mappings where i18nKey starts with the prefix. An empty map if no matches. - Throws:
NullPointerException
- iflink
isnull
-
getAllTranslationsForPrefix
@Nonnull Map<String,String> getAllTranslationsForPrefix(@Nonnull String prefix, @Nonnull Locale locale) Given a property key prefix, this method will return all translations where the key starts with the given prefix as key -> value mappings.- Parameters:
prefix
- the prefix for a particular key to start with. Empty string will match everything, which may be slow. ThrowsNullPointerException
ifnull
.locale
- the locale for which to lookup translations. ThrowsNullPointerException
ifnull
.- Returns:
- a
Map
of i18nKey to translation mappings where i18nKey starts with the prefix. An empty map if no matches. - Throws:
NullPointerException
- ifprefix
orlink
arenull
-