Class MessageSourceAPI

java.lang.Object
com.jbstrap.core.utils.MessageSourceAPI

public final class MessageSourceAPI extends Object

It's possible to load previously defined text messages in the application. After they are loaded, they can be accessed in the application any time, by using their assigned custom keys.

When loading the messages, they first get stored in an inner cache. When reading them, they don't have to be loaded again. The messages have to be stored in a file, in the following format: messageKey1=message1. There can be multiple messages in a file. There is no cardinality limit. The files have to be separated by a line break:
messageKey1=message1
messageKey2=message2
The message keys have to be unique, if they are not, the new ones will overwrite the already existing ones.

The messages supports multi-language solutions. A message, with the same key can be loaded multiple times, if they are in a different language. This language property can be specified in the filename, for example: messageFile1_en-US.properties or messageFile1_en-GB.properties. The filenames don't need to have the text before the "_" character, be identical, since when loading messages, the language query is the first. If a language postfix exists on the file, then all the messages in the file are loaded in that language.

The messages can be queried, by using their loaded keys. During the query, the language can be passed too, but it is not necessary for the query.

Since:
4.0
Author:
JBStrap
  • Method Details

    • loadMessagesMap

      public static void loadMessagesMap(String... resourceBundleNames)
      Loads the file (message map) containing the messages to the cache. The full path of the files must be specified. The files have to be accessible for the current ClassLoader, otherwise the files will not be loaded. The messages will be loaded with the UTF-8 encoding.
      The messages supports multi-language solutions. A message, with the same key can be loaded multiple times, if they are in a different language. This language property can be specified in the filename, for example: messageFile1_en-US.properties or messageFile1_en-GB.properties. The filenames don't need to have the text before the "_" character, be identical, since when loading messages, the language query is the first. If a language postfix exists on the file, then all the messages in the file are loaded in that language. If a language was not specified in the filename, the message will be loaded without a language.
      Parameters:
      resourceBundleNames - The full path of the message file
      Throws:
      IllegalArgumentException - Occurs if any of the resourceBundleNames parameters are null
      See Also:
    • loadMessagesMap

      public static void loadMessagesMap(ClassLoader classLoader, String... resourceBundleNames)
      Loads the file (message map) containing the messages to the cache. The full path of the files must be specified. The files have to be accessible for the current ClassLoader, otherwise the files will not be loaded. The messages will be loaded with the UTF-8 encoding.
      The messages supports multi-language solutions. A message, with the same key can be loaded multiple times, if they are in a different language. This language property can be specified in the filename, for example: messageFile1_en-US.properties or messageFile1_en-GB.properties. The filenames don't need to have the text before the "_" character, be identical, since when loading messages, the language query is the first. If a language postfix exists on the file, then all the messages in the file are loaded in that language. If a language was not specified in the filename, the message will be loaded without a language.
      Parameters:
      classLoader - The ClassLoader instance, in whose context the files are accessible. If null, then the current thread's class loader will be used instead.
      resourceBundleNames - The full path of the message file
      Throws:
      IllegalArgumentException - Occurs if any of the resourceBundleNames parameters are null
      See Also:
    • clearMessagesMap

      public static void clearMessagesMap()
      Clears the cache containing the full messages. After the operation, none of the loaded messages will be accessible.
    • getMessage

      public static String getMessage(String key)
      Gets a previously loaded message, specified by it's key (no language code)
      The message will be first looked for in the JBStrapParamType.DEFAULT_LANGUAGE language, and if not found, it will be searched for without the language. If not found, returns with null.
      Parameters:
      key - The message key, not case-sensitive.
      Returns:
      The message corresponding to the key, or null if not found.
      Throws:
      IllegalArgumentException - If the key was specified as null.
    • getMessage

      public static String getMessage(String key, String languageOrArgument)
      Gets a previously loaded message, specified by it's key (with language). The languageOrArgument parameter will be processed first as a language.
      The message search order:
      It will look for the specified language and country
      If not found, will only look for the language, ignoring the country
      If not found, will only look for the language, with any country.
      If not found, then the languageOrArgument parameter will be used as a message argument. It will look in the JBStrapParamType.DEFAULT_LANGUAGE language first, then if not found, it will search without any language.
      If not found, it will return with null
      Parameters:
      key - The message key, not case-sensitive.
      languageOrArgument - Language or message argument. The language and country must be separated by a "_" character, just like in the name of the file that contains the messages. (en, en_us, en_gb.)
      If an argument, then the argument values that are used as replacement. In messages, the argument must be placed as {x} In place of the "x" , the argument number must be placed, and the arguments (that are received in the parameter) will be placed there, in order.
      Returns:
      The message corresponding to the key, or null if not found.
      Throws:
      IllegalArgumentException - If the key was specified as null.
    • getMessage

      public static String getMessage(String key, Object... arguments)
      Gets a previously loaded message, specified by its key, with arguments.
      Parameters:
      key - The message key, not case-sensitive.
      arguments - The argument values that are used as replacement. In messages, the argument must be placed as {x} In place of the "x" , the argument number must be placed, and the arguments (that are received in the parameter) will be placed there, in order.
      Returns:
      The message corresponding to the key, or null if not found.
      Throws:
      IllegalArgumentException - If the key was specified as null.
    • getMessage

      public static String getMessage(String key, String language, Object... arguments)
      Gets a previously loaded message, specified by its key, with arguments and language code. The message search order:
      It will look for the specified language and country
      If not found, will only look for the language, ignoring the country
      If not found, will only look for the language, with any country.
      If not found, it will look in the JBStrapParamType.DEFAULT_LANGUAGE language. If not found, it will search without any language. If not found, it will return with null
      Parameters:
      key - The message key, not case-sensitive.
      language - The language in which the message will be searched.
      arguments - The argument values that are used as replacement. In messages, the argument must be placed as {x} In place of the "x" , the argument number must be placed, and the arguments (that are received in the parameter) will be placed there, in order.
      Returns:
      The message corresponding to the key, or null if not found.
      Throws:
      IllegalArgumentException - If the key was specified as null.
    • containsKey

      public static boolean containsKey(String key)
      Determines if the specified message key is contained within the cache or not. It will look for it first in the JBStrapParamType.DEFAULT_LANGUAGE language, if not found, it will look for it, while ignoring the language
      Parameters:
      key - The message key, not case-sensitive.
      Returns:
      true if the key is in the cache, otherwise false
    • containsKey

      public static boolean containsKey(String key, String language)
      Determines if the specified message key is contained within the cache or not, in the specified language. The message search order:
      First it will search with the specified country
      If not found, it will look while ignoring the country. If not found, will only look for the language, with any country.
      If not found, it will look in the JBStrapParamType.DEFAULT_LANGUAGE language. If not found, it will look while ignoring the language. If not found, it will return with null
      Parameters:
      key - The message key, not case-sensitive.
      language - The language in which the search will be done
      Returns:
      true if the key is in the cache, otherwise false
      Throws:
      IllegalArgumentException - If the key was specified as null.