- 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 aResourceBundle. This is designed to be compatible with the use of JSTL classes and taglibs.- Author:
- AO Industries, Inc.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceResources.ListenerListeners may be registered to be notified as messages are looked-up.
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_NAMEThe default bundle name.static StringDEFAULT_SUBPACKAGEThe default sub-package that should contain resource bundles.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidaddListener(Resources.Listener listener)Adds a listener.StringgetBaseName()Gets the baseName being accessed by these resources.StringgetMessage(String key)Gets the message with the given key in the current thread's locale.StringgetMessage(String key, Object... args)Gets the message with the given key in the current thread's locale, optionallymessage-formatted.StringgetMessage(Locale locale, String key)Gets the message with the given key in the provided locale.StringgetMessage(Locale locale, String key, Object... args)Gets the message with the given key in the provided locale, optionallymessage-formatted.StringgetPrefix()Gets the prefix applied to all keys directly used on these resources or an empty string when there is no prefix.ResourceBundlegetResourceBundle()Gets the bundle for the current thread's locale.ResourceBundlegetResourceBundle(Locale locale)Gets the bundle for the provided locale.static ResourcesgetResources(SerializableBiFunction<String,Locale,ResourceBundle> bundleAccessor, Class<?> clazz)Accesses the resources in theDEFAULT_SUBPACKAGEsub-package of the given class namedDEFAULT_NAMEwithclazz.getSimpleName() + '.'as the prefix.static ResourcesgetResources(SerializableBiFunction<String,Locale,ResourceBundle> bundleAccessor, Package pack)Accesses the resources in theDEFAULT_SUBPACKAGEsub-package of the given package namedDEFAULT_NAME.static ResourcesgetResources(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.static ResourcesgetResources(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.static ResourcesgetResources(SerializableBiFunction<String,Locale,ResourceBundle> bundleAccessor, String baseName)Deprecated.Please use one of the class- or package-relative techniques in locating the resource bundle, as they will correctly locate the resources after packages are renamed by code obfuscation.static ResourcesgetResources(SerializableBiFunction<String,Locale,ResourceBundle> bundleAccessor, String baseName, String prefix)Deprecated.Please use one of the class- or package-relative techniques in locating the resource bundle, as they will correctly locate the resources after packages are renamed by code obfuscation.static ResourcesgetResources(Class<?> clazz)Deprecated.Please usegetResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Class)instead.static ResourcesgetResources(Package pack)Deprecated.Please usegetResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Package)instead.static ResourcesgetResources(Package pack, String name)Deprecated.static ResourcesgetResources(Package pack, String name, String prefix)Deprecated.static ResourcesgetResources(String baseName)Deprecated.Please usegetResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.String)instead.static ResourcesgetResources(String baseName, String prefix)Deprecated.static voidremoveListener(Resources.Listener listener)Removes all occurrences of the provided listener.
-
-
-
Field Detail
-
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:
- Constant Field Values
-
DEFAULT_NAME
public static final String DEFAULT_NAME
The default bundle name.- See Also:
- Constant Field Values
-
-
Method Detail
-
getResources
@Deprecated public static Resources getResources(SerializableBiFunction<String,Locale,ResourceBundle> bundleAccessor, String baseName, String prefix)
Deprecated.Please use one of the class- or package-relative techniques in locating the resource bundle, as they will correctly locate the resources after packages are renamed by code obfuscation.getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Class)getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Package)getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Package, java.lang.String)getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Package, java.lang.String, java.lang.String)
Accesses the resources with the given base name and prefix.- Parameters:
bundleAccessor- Bi-function lookup for bundle, which will typically be the static method referenceResourceBundle::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 toResourceBundle.getBundle(java.lang.String, java.util.Locale), which requiresopens …;inmodule-info.java.
-
getResources
@Deprecated public static Resources getResources(String baseName, String prefix)
Deprecated.Please usegetResources(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 requiresopens …;inmodule-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)
Deprecated.Please use one of the class- or package-relative techniques in locating the resource bundle, as they will correctly locate the resources after packages are renamed by code obfuscation.getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Class)getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Package)getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Package, java.lang.String)getResources(com.aoapps.lang.function.SerializableBiFunction, java.lang.Package, java.lang.String, java.lang.String)
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 referenceResourceBundle::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 toResourceBundle.getBundle(java.lang.String, java.util.Locale), which requiresopens …;inmodule-info.java.
-
getResources
@Deprecated public static Resources getResources(String baseName)
Deprecated.Please usegetResources(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 requiresopens …;inmodule-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 aspack.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 referenceResourceBundle::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 toResourceBundle.getBundle(java.lang.String, java.util.Locale), which requiresopens …;inmodule-info.java.name- The name of the resource within the package, whennulldefaults toDEFAULT_SUBPACKAGE + "." + DEFAULT_NAME(to be withinDEFAULT_SUBPACKAGEsub-package).
-
getResources
@Deprecated public static Resources getResources(Package pack, String name, String prefix)
Deprecated.Please usegetResources(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 requiresopens …;inmodule-info.java.Accesses the resources in the given package (or sub-package) with the given name and prefix. The base name is derived aspack.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, whennulldefaults toDEFAULT_SUBPACKAGE + "." + DEFAULT_NAME(to be withinDEFAULT_SUBPACKAGEsub-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 aspack.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 referenceResourceBundle::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 toResourceBundle.getBundle(java.lang.String, java.util.Locale), which requiresopens …;inmodule-info.java.name- The name of the resource within the package, whennulldefaults toDEFAULT_SUBPACKAGE + "." + DEFAULT_NAME(to be withinDEFAULT_SUBPACKAGEsub-package).
-
getResources
@Deprecated public static Resources getResources(Package pack, String name)
Deprecated.Please usegetResources(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 requiresopens …;inmodule-info.java.Accesses the resources in the given package (or sub-package) with the given name and no prefix. The base name is derived aspack.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, whennulldefaults toDEFAULT_SUBPACKAGE + "." + DEFAULT_NAME(to be withinDEFAULT_SUBPACKAGEsub-package).
-
getResources
public static Resources getResources(SerializableBiFunction<String,Locale,ResourceBundle> bundleAccessor, Package pack)
Accesses the resources in theDEFAULT_SUBPACKAGEsub-package of the given package namedDEFAULT_NAME. The base name is derived aspack.getName() + "." + DEFAULT_SUBPACKAGE + "." + DEFAULT_NAME(to be withinDEFAULT_SUBPACKAGEsub-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 referenceResourceBundle::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 toResourceBundle.getBundle(java.lang.String, java.util.Locale), which requiresopens …;inmodule-info.java.
-
getResources
@Deprecated public static Resources getResources(Package pack)
Deprecated.Please usegetResources(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 requiresopens …;inmodule-info.java.Accesses the resources in theDEFAULT_SUBPACKAGEsub-package of the given package namedDEFAULT_NAME. The base name is derived aspack.getName() + "." + DEFAULT_SUBPACKAGE + "." + DEFAULT_NAME(to be withinDEFAULT_SUBPACKAGEsub-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 theDEFAULT_SUBPACKAGEsub-package of the given class namedDEFAULT_NAMEwithclazz.getSimpleName() + '.'as the prefix. The base name is derived asclazz.getPackage().getName() + "." + DEFAULT_SUBPACKAGE + "." + DEFAULT_NAME(to be withinDEFAULT_SUBPACKAGEsub-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
*.propertiesfiles 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 referenceResourceBundle::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 toResourceBundle.getBundle(java.lang.String, java.util.Locale), which requiresopens …;inmodule-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 implementsResourceBundle.
-
getResources
@Deprecated public static Resources getResources(Class<?> clazz)
Deprecated.Please usegetResources(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 requiresopens …;inmodule-info.java.Accesses the resources in theDEFAULT_SUBPACKAGEsub-package of the given class namedDEFAULT_NAMEwithclazz.getSimpleName() + '.'as the prefix. The base name is derived asclazz.getPackage().getName() + "." + DEFAULT_SUBPACKAGE + "." + DEFAULT_NAME(to be withinDEFAULT_SUBPACKAGEsub-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
*.propertiesfiles 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 implementsResourceBundle.
-
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:
ThreadLocale,getResourceBundle(java.util.Locale)
-
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
argsisnullor empty.- Parameters:
key- This will be combined with anyprefix
-
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 anyprefix
-
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
argsisnullor empty.- Parameters:
key- This will be combined with anyprefix- See Also:
ThreadLocale,getMessage(java.util.Locale, java.lang.String, java.lang.Object...)
-
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 anyprefix- See Also:
ThreadLocale,getMessage(java.util.Locale, java.lang.String)
-
-