Class SimpleTextResourcesImpl
java.lang.Object
ch.tocco.nice2.textresources.impl.SimpleTextResourcesImpl
- All Implemented Interfaces:
TextResources
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionformatTextMessage
(TextMessage textMessage) Formats a text message object and returns the formatted string.formatTextMessage
(TextMessage textMessage, Locale locale) Formats a text message object and returns the formatted string.Get information for this key.Same behaviour asTextResources.getAll(Pattern, Locale)
but theLocale
of the current Thread will be used.Same behaviour asTextResources.getAll(Pattern, Locale)
but this method will determine the bestLocale
from the givenList
itself.Returns all texts for the given locale which match a regex pattern.getFormattedTextWithParameter
(String textResourceKey, String parameter) Replaces [param] in the text resource with the passed parameter and formats the text messagegetLocalesWithText
(String key) Tells which locales have a (possibly empty) text defined.Get a text for this specific key.Get a text for this specific key and locale.void
boolean
isTextMissingString
(String message) Tells if the message returned by theTextResources.getText(java.lang.String)
methods is a constructed fallback message because a real translation was missing.void
onReload()
void
setTextResourceProviders
(List<TextResourceProvider> providers) @Nullable String
tryGetText
(String key) Same asTextResources.getText(String)
, but returns null if the resource doesn't exist, and logs no error.@Nullable String
tryGetText
(String key, Locale locale) Same asTextResources.getText(String)
, but returns null if the resource doesn't exist, and logs no error.
-
Constructor Details
-
SimpleTextResourcesImpl
-
-
Method Details
-
initialize
@PostConstruct public void initialize() -
formatTextMessage
Description copied from interface:TextResources
Formats a text message object and returns the formatted string.- Specified by:
formatTextMessage
in interfaceTextResources
- Returns:
- The formatted string text.
-
getFormattedTextWithParameter
Description copied from interface:TextResources
Replaces [param] in the text resource with the passed parameter and formats the text message- Specified by:
getFormattedTextWithParameter
in interfaceTextResources
- Returns:
- The formatted string text.
-
formatTextMessage
Description copied from interface:TextResources
Formats a text message object and returns the formatted string.- Specified by:
formatTextMessage
in interfaceTextResources
- Returns:
- The formatted string text.
-
get
- Specified by:
get
in interfaceTextResources
-
get
Description copied from interface:TextResources
Get information for this key. The fallback behavior depends on the environment. If there is no message defined in a desired language then: + in production: any other language is chosen (preferably a close one, or a common one, no guarantee) the Boolean is set to false, and the Locale tells which language was finally chosen. in case there isn't a message in ANY language then the behavior is as in dev/test. + in development and test: a message containing the message key is used, clearly stating that such a message is missing. the string does not use placeholders where variables could be put in, thus any variables (message formatting) should be appended by the formatter.- Specified by:
get
in interfaceTextResources
- Returns:
- the elements are: String message, Boolean if succeeded (otherwise fallback message), Locale the chosen one (null if none)
-
get
- Specified by:
get
in interfaceTextResources
-
onReload
public void onReload()- Specified by:
onReload
in interfaceTextResources
-
getAll
Description copied from interface:TextResources
Same behaviour asTextResources.getAll(Pattern, Locale)
but theLocale
of the current Thread will be used.- Specified by:
getAll
in interfaceTextResources
- Parameters:
pattern
- eg "^client\\..*"- Returns:
- May be empty, may contain null values!
-
getAll
Description copied from interface:TextResources
Returns all texts for the given locale which match a regex pattern. May contain null values. That is the case if there is no definition for a key in the desired locale.- Specified by:
getAll
in interfaceTextResources
- Parameters:
pattern
- eg "^client\\..*"
-
getAll
Description copied from interface:TextResources
Same behaviour asTextResources.getAll(Pattern, Locale)
but this method will determine the bestLocale
from the givenList
itself.- Specified by:
getAll
in interfaceTextResources
- Parameters:
pattern
- eg "^client\\..*"- Returns:
- May be empty, may contain null values!
-
getText
Description copied from interface:TextResources
Get a text for this specific key. Same behaviour asTextResources.getText(String, Locale)
but uses theLocale
provided byL10N.currentLocale()
and the calling module as module. Reports (logs) in case the text is missing.- Specified by:
getText
in interfaceTextResources
- Parameters:
key
- the key- Returns:
- the text
-
getText
Description copied from interface:TextResources
Get a text for this specific key and locale. Reports (logs) in case the text is missing.- Specified by:
getText
in interfaceTextResources
- Parameters:
key
- the keypreferredLocale
- the locale- Returns:
- the text
-
tryGetText
Description copied from interface:TextResources
Same asTextResources.getText(String)
, but returns null if the resource doesn't exist, and logs no error.- Specified by:
tryGetText
in interfaceTextResources
- Parameters:
key
- the key- Returns:
- the text, or null if not there
-
tryGetText
Description copied from interface:TextResources
Same asTextResources.getText(String)
, but returns null if the resource doesn't exist, and logs no error.- Specified by:
tryGetText
in interfaceTextResources
- Parameters:
key
- the key- Returns:
- the text, or null if not there
-
isTextMissingString
Description copied from interface:TextResources
Tells if the message returned by theTextResources.getText(java.lang.String)
methods is a constructed fallback message because a real translation was missing. Maybe we'll have to add more overloaded getText() methods with another param by ref that tells if the resource was missing, or which locale was chosen, null if failed. This method just checks if the message starts with the failing indicator "##". Works for now.- Specified by:
isTextMissingString
in interfaceTextResources
- Parameters:
message
- the result of aTextResources.getText(java.lang.String)
call- Returns:
- if the message was missing
-
getLocalesWithText
Tells which locales have a (possibly empty) text defined. This can be used for choosing a fallback language to at least display something human-readable in a production environment. This method does not log anything missing.- Returns:
- may be empty
-
setTextResourceProviders
-