Class SimpleTextResourcesImpl

java.lang.Object
ch.tocco.nice2.textresources.impl.SimpleTextResourcesImpl
All Implemented Interfaces:
TextResources

@Component public class SimpleTextResourcesImpl extends Object implements TextResources
  • Constructor Details

    • SimpleTextResourcesImpl

      public SimpleTextResourcesImpl(org.slf4j.Logger log, L10N l10N)
  • Method Details

    • initialize

      @PostConstruct public void initialize()
    • formatTextMessage

      public String formatTextMessage(TextMessage textMessage)
      Description copied from interface: TextResources
      Formats a text message object and returns the formatted string.
      Specified by:
      formatTextMessage in interface TextResources
      Returns:
      The formatted string text.
    • getFormattedTextWithParameter

      public String getFormattedTextWithParameter(String textResourceKey, String parameter)
      Description copied from interface: TextResources
      Replaces [param] in the text resource with the passed parameter and formats the text message
      Specified by:
      getFormattedTextWithParameter in interface TextResources
      Returns:
      The formatted string text.
    • formatTextMessage

      public String formatTextMessage(TextMessage textMessage, Locale locale)
      Description copied from interface: TextResources
      Formats a text message object and returns the formatted string.
      Specified by:
      formatTextMessage in interface TextResources
      Returns:
      The formatted string text.
    • get

      public Tuple3<String,Boolean,Locale> get(String key, Locale locale)
      Specified by:
      get in interface TextResources
    • get

      public Tuple3<String,Boolean,Locale> get(String key)
      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 interface TextResources
      Returns:
      the elements are: String message, Boolean if succeeded (otherwise fallback message), Locale the chosen one (null if none)
    • get

      public Tuple3<String,Boolean,Locale> get(String key, List<Locale> preferredLocales)
      Specified by:
      get in interface TextResources
    • onReload

      public void onReload()
      Specified by:
      onReload in interface TextResources
    • getAll

      public Map<String,String> getAll(Pattern pattern)
      Description copied from interface: TextResources
      Same behaviour as TextResources.getAll(Pattern, Locale) but the Locale of the current Thread will be used.
      Specified by:
      getAll in interface TextResources
      Parameters:
      pattern - eg "^client\\..*"
      Returns:
      May be empty, may contain null values!
    • getAll

      public Map<String,String> getAll(Pattern pattern, Locale preferredLocale)
      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 interface TextResources
      Parameters:
      pattern - eg "^client\\..*"
    • getAll

      public Map<String,String> getAll(Pattern pattern, List<Locale> preferredLocales)
      Description copied from interface: TextResources
      Same behaviour as TextResources.getAll(Pattern, Locale) but this method will determine the best Locale from the given List itself.
      Specified by:
      getAll in interface TextResources
      Parameters:
      pattern - eg "^client\\..*"
      Returns:
      May be empty, may contain null values!
    • getText

      public String getText(String key)
      Description copied from interface: TextResources
      Get a text for this specific key. Same behaviour as TextResources.getText(String, Locale) but uses the Locale provided by L10N.currentLocale() and the calling module as module. Reports (logs) in case the text is missing.
      Specified by:
      getText in interface TextResources
      Parameters:
      key - the key
      Returns:
      the text
    • getText

      public String getText(String key, Locale preferredLocale)
      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 interface TextResources
      Parameters:
      key - the key
      preferredLocale - the locale
      Returns:
      the text
    • tryGetText

      @Nullable public @Nullable String tryGetText(String key)
      Description copied from interface: TextResources
      Same as TextResources.getText(String), but returns null if the resource doesn't exist, and logs no error.
      Specified by:
      tryGetText in interface TextResources
      Parameters:
      key - the key
      Returns:
      the text, or null if not there
    • tryGetText

      @Nullable public @Nullable String tryGetText(String key, Locale locale)
      Description copied from interface: TextResources
      Same as TextResources.getText(String), but returns null if the resource doesn't exist, and logs no error.
      Specified by:
      tryGetText in interface TextResources
      Parameters:
      key - the key
      Returns:
      the text, or null if not there
    • isTextMissingString

      public boolean isTextMissingString(String message)
      Description copied from interface: TextResources
      Tells if the message returned by the TextResources.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 interface TextResources
      Parameters:
      message - the result of a TextResources.getText(java.lang.String) call
      Returns:
      if the message was missing
    • getLocalesWithText

      public Set<Locale> getLocalesWithText(String key)
      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

      @Autowired public void setTextResourceProviders(List<TextResourceProvider> providers)