Class Resources

java.lang.Object
com.aoapps.lang.i18n.Resources
All Implemented Interfaces:
Serializable

public class Resources extends Object implements Serializable
Provides a simplified interface for obtaining localized and formatted values from a ResourceBundle. This is designed to be compatible with the use of JSTL classes and taglibs.
Author:
AO Industries, Inc.
See Also:
  • Field Details

    • DEFAULT_SUBPACKAGE

      public static final String DEFAULT_SUBPACKAGE
      The default sub-package that should contain resource bundles. Sub-packages are used so that resource bundles may be opened without exposing the entire package to reflection.
      See Also:
    • DEFAULT_NAME

      public static final String DEFAULT_NAME
      The default bundle name.
      See Also:
  • Method Details

    • getResources

      @Deprecated public static Resources getResources(SerializableBiFunction<String,Locale,ResourceBundle> bundleAccessor, String baseName, String prefix)
      Accesses the resources with the given base name and prefix.
      Parameters:
      bundleAccessor - Bi-function lookup for bundle, which will typically be the static method reference ResourceBundle::getBundle.

      As of Java 9, bundle access is affected by module descriptors. To access the bundle with caller permissions, pass a small lambda that performs the bundle access. This will typically be the static method reference ResourceBundle::getBundle, but may be of any arbitrary complexity. The bundle accessor is invoked for every message lookup, so the implementation should take care to perform well.

      When null, the bundle is looked-up via a direct call to ResourceBundle.getBundle(java.lang.String, java.util.Locale), which requires opens …; in module-info.java.

    • getResources

      @Deprecated public static Resources getResources(String baseName, String prefix)
      Deprecated.
      Please use getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.String, java.lang.String) instead.

      As of Java 9, bundle access is affected by module descriptors. The bundle is looked-up via a direct call to ResourceBundle.getBundle(java.lang.String, java.util.Locale), which requires opens …; in module-info.java.

      Accesses the resources with the given base name and prefix.
    • getResources

      @Deprecated public static Resources getResources(SerializableBiFunction<String,Locale,ResourceBundle> bundleAccessor, String baseName)
      Accesses the resources with the given base name and no prefix.
      Parameters:
      bundleAccessor - Bi-function lookup for bundle, which will typically be the static method reference ResourceBundle::getBundle.

      As of Java 9, bundle access is affected by module descriptors. To access the bundle with caller permissions, pass a small lambda that performs the bundle access. This will typically be the static method reference ResourceBundle::getBundle, but may be of any arbitrary complexity. The bundle accessor is invoked for every message lookup, so the implementation should take care to perform well.

      When null, the bundle is looked-up via a direct call to ResourceBundle.getBundle(java.lang.String, java.util.Locale), which requires opens …; in module-info.java.

    • getResources

      @Deprecated public static Resources getResources(String baseName)
      Deprecated.
      Please use getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.String) instead.

      As of Java 9, bundle access is affected by module descriptors. The bundle is looked-up via a direct call to ResourceBundle.getBundle(java.lang.String, java.util.Locale), which requires opens …; in module-info.java.

      Accesses the resources with the given base name and no prefix.
    • getResources

      public static Resources getResources(SerializableBiFunction<String,Locale,ResourceBundle> bundleAccessor, Package pack, String name, String prefix)
      Accesses the resources in the given package (or sub-package) with the given name and prefix. The base name is derived as pack.getName() + '.' + name.

      By default, resources are expected to be in a sub-package named DEFAULT_SUBPACKAGE. A sub-package is used because the module system does not allow opening for reflection separately from opening for resources. By using a separate sub-package, the resource bundles may be opened up without exposing the entire package to reflection.

      Note: Being accessed relative to the package, the resources can still be correctly located after packages are renamed by code obfuscation.

      Parameters:
      bundleAccessor - Bi-function lookup for bundle, which will typically be the static method reference ResourceBundle::getBundle.

      As of Java 9, bundle access is affected by module descriptors. To access the bundle with caller permissions, pass a small lambda that performs the bundle access. This will typically be the static method reference ResourceBundle::getBundle, but may be of any arbitrary complexity. The bundle accessor is invoked for every message lookup, so the implementation should take care to perform well.

      When null, the bundle is looked-up via a direct call to ResourceBundle.getBundle(java.lang.String, java.util.Locale), which requires opens …; in module-info.java.

      name - The name of the resource within the package, when null defaults to DEFAULT_SUBPACKAGE + "." + DEFAULT_NAME (to be within DEFAULT_SUBPACKAGE sub-package).
    • getResources

      @Deprecated public static Resources getResources(Package pack, String name, String prefix)
      Deprecated.
      Please use getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Package, java.lang.String, java.lang.String) instead.

      As of Java 9, bundle access is affected by module descriptors. The bundle is looked-up via a direct call to ResourceBundle.getBundle(java.lang.String, java.util.Locale), which requires opens …; in module-info.java.

      Accesses the resources in the given package (or sub-package) with the given name and prefix. The base name is derived as pack.getName() + '.' + name.

      By default, resources are expected to be in a sub-package named DEFAULT_SUBPACKAGE. A sub-package is used because the module system does not allow opening for reflection separately from opening for resources. By using a separate sub-package, the resource bundles may be opened up without exposing the entire package to reflection.

      Note: Being accessed relative to the package, the resources can still be correctly located after packages are renamed by code obfuscation.

      Parameters:
      name - The name of the resource within the package, when null defaults to DEFAULT_SUBPACKAGE + "." + DEFAULT_NAME (to be within DEFAULT_SUBPACKAGE sub-package).
    • getResources

      public static Resources getResources(SerializableBiFunction<String,Locale,ResourceBundle> bundleAccessor, Package pack, String name)
      Accesses the resources in the given package (or sub-package) with the given name and no prefix. The base name is derived as pack.getName() + '.' + name.

      By default, resources are expected to be in a sub-package named DEFAULT_SUBPACKAGE. A sub-package is used because the module system does not allow opening for reflection separately from opening for resources. By using a separate sub-package, the resource bundles may be opened up without exposing the entire package to reflection.

      Note: Being accessed relative to the package, the resources can still be correctly located after packages are renamed by code obfuscation.

      Parameters:
      bundleAccessor - Bi-function lookup for bundle, which will typically be the static method reference ResourceBundle::getBundle.

      As of Java 9, bundle access is affected by module descriptors. To access the bundle with caller permissions, pass a small lambda that performs the bundle access. This will typically be the static method reference ResourceBundle::getBundle, but may be of any arbitrary complexity. The bundle accessor is invoked for every message lookup, so the implementation should take care to perform well.

      When null, the bundle is looked-up via a direct call to ResourceBundle.getBundle(java.lang.String, java.util.Locale), which requires opens …; in module-info.java.

      name - The name of the resource within the package, when null defaults to DEFAULT_SUBPACKAGE + "." + DEFAULT_NAME (to be within DEFAULT_SUBPACKAGE sub-package).
    • getResources

      @Deprecated public static Resources getResources(Package pack, String name)
      Deprecated.
      Please use getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Package, java.lang.String) instead.

      As of Java 9, bundle access is affected by module descriptors. The bundle is looked-up via a direct call to ResourceBundle.getBundle(java.lang.String, java.util.Locale), which requires opens …; in module-info.java.

      Accesses the resources in the given package (or sub-package) with the given name and no prefix. The base name is derived as pack.getName() + '.' + name.

      By default, resources are expected to be in a sub-package named DEFAULT_SUBPACKAGE. A sub-package is used because the module system does not allow opening for reflection separately from opening for resources. By using a separate sub-package, the resource bundles may be opened up without exposing the entire package to reflection.

      Note: Being accessed relative to the package, the resources can still be correctly located after packages are renamed by code obfuscation.

      Parameters:
      name - The name of the resource within the package, when null defaults to DEFAULT_SUBPACKAGE + "." + DEFAULT_NAME (to be within DEFAULT_SUBPACKAGE sub-package).
    • getResources

      public static Resources getResources(SerializableBiFunction<String,Locale,ResourceBundle> bundleAccessor, Package pack)
      Accesses the resources in the DEFAULT_SUBPACKAGE sub-package of the given package named DEFAULT_NAME. The base name is derived as pack.getName() + "." + DEFAULT_SUBPACKAGE + "." + DEFAULT_NAME (to be within DEFAULT_SUBPACKAGE sub-package).

      Resources are expected to be in a sub-package named DEFAULT_SUBPACKAGE. A sub-package is used because the module system does not allow opening for reflection separately from opening for resources. By using a separate sub-package, the resource bundles may be opened up without exposing the entire package to reflection.

      Note: Being accessed relative to the package, the resources can still be correctly located after packages are renamed by code obfuscation.

      Parameters:
      bundleAccessor - Bi-function lookup for bundle, which will typically be the static method reference ResourceBundle::getBundle.

      As of Java 9, bundle access is affected by module descriptors. To access the bundle with caller permissions, pass a small lambda that performs the bundle access. This will typically be the static method reference ResourceBundle::getBundle, but may be of any arbitrary complexity. The bundle accessor is invoked for every message lookup, so the implementation should take care to perform well.

      When null, the bundle is looked-up via a direct call to ResourceBundle.getBundle(java.lang.String, java.util.Locale), which requires opens …; in module-info.java.

    • getResources

      @Deprecated public static Resources getResources(Package pack)
      Deprecated.
      Please use getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Package) instead.

      As of Java 9, bundle access is affected by module descriptors. The bundle is looked-up via a direct call to ResourceBundle.getBundle(java.lang.String, java.util.Locale), which requires opens …; in module-info.java.

      Accesses the resources in the DEFAULT_SUBPACKAGE sub-package of the given package named DEFAULT_NAME. The base name is derived as pack.getName() + "." + DEFAULT_SUBPACKAGE + "." + DEFAULT_NAME (to be within DEFAULT_SUBPACKAGE sub-package).

      Resources are expected to be in a sub-package named DEFAULT_SUBPACKAGE. A sub-package is used because the module system does not allow opening for reflection separately from opening for resources. By using a separate sub-package, the resource bundles may be opened up without exposing the entire package to reflection.

      Note: Being accessed relative to the package, the resources can still be correctly located after packages are renamed by code obfuscation.

    • getResources

      public static Resources getResources(SerializableBiFunction<String,Locale,ResourceBundle> bundleAccessor, Class<?> clazz)
      Accesses the resources in the DEFAULT_SUBPACKAGE sub-package of the given class named DEFAULT_NAME with clazz.getSimpleName() + '.' as the prefix. The base name is derived as clazz.getPackage().getName() + "." + DEFAULT_SUBPACKAGE + "." + DEFAULT_NAME (to be within DEFAULT_SUBPACKAGE sub-package).

      Resources are expected to be in a sub-package named DEFAULT_SUBPACKAGE. A sub-package is used because the module system does not allow opening for reflection separately from opening for resources. By using a separate sub-package, the resource bundles may be opened up without exposing the entire package to reflection.

      Note: Being accessed relative to the package, the resources may still be correctly located after packages are renamed by code obfuscation. However, if classes are also renamed, the prefix will change and the build system must also alter the contents of the underlying *.properties files correspondingly.

      When rewriting the contents of the underlying properties files is not possible, it may be best either use hard-coded prefix (may leak original class name, thus thwarting obfuscation a bit) or use a per-class properties file (tedious, also requires build system coordination).

      Parameters:
      bundleAccessor - Bi-function lookup for bundle, which will typically be the static method reference ResourceBundle::getBundle.

      As of Java 9, bundle access is affected by module descriptors. To access the bundle with caller permissions, pass a small lambda that performs the bundle access. This will typically be the static method reference ResourceBundle::getBundle, but may be of any arbitrary complexity. The bundle accessor is invoked for every message lookup, so the implementation should take care to perform well.

      When null, the bundle is looked-up via a direct call to ResourceBundle.getBundle(java.lang.String, java.util.Locale), which requires opens …; in module-info.java.

      clazz - This class is used for determining the package and prefix only. It will typically be the class that is using the resource, not the class that implements ResourceBundle.
    • getResources

      @Deprecated public static Resources getResources(Class<?> clazz)
      Deprecated.
      Please use getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Class) instead.

      As of Java 9, bundle access is affected by module descriptors. The bundle is looked-up via a direct call to ResourceBundle.getBundle(java.lang.String, java.util.Locale), which requires opens …; in module-info.java.

      Accesses the resources in the DEFAULT_SUBPACKAGE sub-package of the given class named DEFAULT_NAME with clazz.getSimpleName() + '.' as the prefix. The base name is derived as clazz.getPackage().getName() + "." + DEFAULT_SUBPACKAGE + "." + DEFAULT_NAME (to be within DEFAULT_SUBPACKAGE sub-package).

      Resources are expected to be in a sub-package named DEFAULT_SUBPACKAGE. A sub-package is used because the module system does not allow opening for reflection separately from opening for resources. By using a separate sub-package, the resource bundles may be opened up without exposing the entire package to reflection.

      Note: Being accessed relative to the package, the resources may still be correctly located after packages are renamed by code obfuscation. However, if classes are also renamed, the prefix will change and the build system must also alter the contents of the underlying *.properties files correspondingly.

      When rewriting the contents of the underlying properties files is not possible, it may be best either use hard-coded prefix (may leak original class name, thus thwarting obfuscation a bit) or use a per-class properties file (tedious, also requires build system coordination).

      Parameters:
      clazz - This class is used for determining the package and prefix only. It will typically be the class that is using the resource, not the class that implements ResourceBundle.
    • addListener

      public static void addListener(Resources.Listener listener)
      Adds a listener. Does not check for duplicates. If a listener is added more than once, it will simply by called multiple times.
    • removeListener

      public static void removeListener(Resources.Listener listener)
      Removes all occurrences of the provided listener.
    • getBaseName

      public String getBaseName()
      Gets the baseName being accessed by these resources.
    • getPrefix

      public String getPrefix()
      Gets the prefix applied to all keys directly used on these resources or an empty string when there is no prefix.
    • getResourceBundle

      public ResourceBundle getResourceBundle(Locale locale)
      Gets the bundle for the provided locale. Direct use of this bundle will not have any prefix applied.
    • getResourceBundle

      public ResourceBundle getResourceBundle()
      Gets the bundle for the current thread's locale. Direct use of this bundle will not have any prefix applied.
      See Also:
    • getMessage

      public String getMessage(Locale locale, String key, Object... args)

      Gets the message with the given key in the provided locale, optionally message-formatted. If missing, will generate a Struts-like value including the locale and (prefix + key).

      Substitutes arguments in the text where it finds {0}, {1}, {2}, … Message formatting is not performed when args is null or empty.

      Parameters:
      key - This will be combined with any prefix
    • getMessage

      public String getMessage(Locale locale, String key)
      Gets the message with the given key in the provided locale. If missing, will generate a Struts-like value including the locale and (prefix + key).
      Parameters:
      key - This will be combined with any prefix
    • getMessage

      public String getMessage(String key, Object... args)

      Gets the message with the given key in the current thread's locale, optionally message-formatted. If missing, will generate a Struts-like value including the locale and (prefix + key).

      Substitutes arguments in the text where it finds {0}, {1}, {2}, … Message formatting is not performed when args is null or empty.

      Parameters:
      key - This will be combined with any prefix
      See Also:
    • getMessage

      public String getMessage(String key)
      Gets the message with the given key in the current thread's locale. If missing, will generate a Struts-like value including the locale and (prefix + key).
      Parameters:
      key - This will be combined with any prefix
      See Also: