Class UResourceBundle


  • public abstract class UResourceBundle
    extends ResourceBundle
    [icu enhancement] ICU's replacement for java.util.ResourceBundle. Methods, fields, and other functionality specific to ICU are labeled '[icu]'.

    A class representing a collection of resource information pertaining to a given locale. A resource bundle provides a way of accessing locale- specific information in a data file. You create a resource bundle that manages the resources for a given locale and then ask it for individual resources.

    In ResourceBundle, an object is created and the sub-items are fetched using the getString and getObject methods. In UResourceBundle, each individual element of a resource is a resource by itself.

    Resource bundles in ICU are currently defined using text files that conform to the following BNF definition. More on resource bundle concepts and syntax can be found in the Users Guide.

    The packaging of ICU *.res files can be of two types ICU4C:

           root.res
             |
          --------
         |        |
       fr.res  en.res
         |
       --------
      |        |
     fr_CA.res fr_FR.res
     
    JAVA/JDK:
        LocaleElements.res
             |
          -------------------
         |                   |
     LocaleElements_fr.res  LocaleElements_en.res
         |
       ---------------------------
      |                            |
     LocaleElements_fr_CA.res   LocaleElements_fr_FR.res
     
    Depending on the organization of your resources, the syntax to getBundleInstance will change. To open ICU style organization use:
          UResourceBundle bundle =
              UResourceBundle.getBundleInstance("com/mycompany/resources",
                                                "en_US", myClassLoader);
     
    To open Java/JDK style organization use:
          UResourceBundle bundle =
              UResourceBundle.getBundleInstance("com.mycompany.resources.LocaleElements",
                                                "en_US", myClassLoader);
     

    Note: Please use pass a class loader for loading non-ICU resources. Java security does not allow loading of resources across jar files. You must provide your class loader to load the resources

    Author:
    ram
    Status:
    Stable ICU 3.0.
    • Field Detail

      • NONE

        public static final int NONE
        [icu] Resource type constant for "no resource".
        See Also:
        Constant Field Values
        Status:
        Stable ICU 3.8.
      • STRING

        public static final int STRING
        [icu] Resource type constant for strings.
        See Also:
        Constant Field Values
        Status:
        Stable ICU 3.8.
      • BINARY

        public static final int BINARY
        [icu] Resource type constant for binary data.
        See Also:
        Constant Field Values
        Status:
        Stable ICU 3.8.
      • TABLE

        public static final int TABLE
        [icu] Resource type constant for tables of key-value pairs.
        See Also:
        Constant Field Values
        Status:
        Stable ICU 3.8.
      • INT

        public static final int INT
        [icu] Resource type constant for a single 28-bit integer, interpreted as signed or unsigned by the getInt() function.
        See Also:
        getInt(), Constant Field Values
        Status:
        Stable ICU 3.8.
      • ARRAY

        public static final int ARRAY
        [icu] Resource type constant for arrays of resources.
        See Also:
        Constant Field Values
        Status:
        Stable ICU 3.8.
    • Constructor Detail

      • UResourceBundle

        public UResourceBundle()
        [icu] Sole constructor. (For invocation by subclass constructors, typically implicit.) This is public for compatibility with Java, whose compiler will generate public default constructors for an abstract class.
        Status:
        Stable ICU 3.0.
    • Method Detail

      • getBundleInstance

        public static UResourceBundle getBundleInstance​(String baseName,
                                                        String localeName)
        [icu] Creates a resource bundle using the specified base name and locale. ICU_DATA_CLASS is used as the default root.
        Parameters:
        baseName - string containing the name of the data package. If null the default ICU package name is used.
        localeName - the locale for which a resource bundle is desired
        Returns:
        a resource bundle for the given base name and locale
        Throws:
        MissingResourceException - If no resource bundle for the specified base name can be found
        Status:
        Stable ICU 3.0.
      • getBundleInstance

        public static UResourceBundle getBundleInstance​(String baseName,
                                                        String localeName,
                                                        ClassLoader root)
        [icu] Creates a resource bundle using the specified base name, locale, and class root.
        Parameters:
        baseName - string containing the name of the data package. If null the default ICU package name is used.
        localeName - the locale for which a resource bundle is desired
        root - the class object from which to load the resource bundle
        Returns:
        a resource bundle for the given base name and locale
        Throws:
        MissingResourceException - If no resource bundle for the specified base name can be found
        Status:
        Stable ICU 3.0.
      • getBundleInstance

        protected static UResourceBundle getBundleInstance​(String baseName,
                                                           String localeName,
                                                           ClassLoader root,
                                                           boolean disableFallback)
        [icu] Creates a resource bundle using the specified base name, locale, and class root.
        Parameters:
        baseName - string containing the name of the data package. If null the default ICU package name is used.
        localeName - the locale for which a resource bundle is desired
        root - the class object from which to load the resource bundle
        disableFallback - Option to disable locale inheritance. If true the fallback chain will not be built.
        Returns:
        a resource bundle for the given base name and locale
        Throws:
        MissingResourceException - if no resource bundle for the specified base name can be found
        Status:
        Stable ICU 3.0.
      • getBundleInstance

        public static UResourceBundle getBundleInstance​(ULocale locale)
        [icu] Creates a UResourceBundle for the locale specified, from which users can extract resources by using their corresponding keys.
        Parameters:
        locale - specifies the locale for which we want to open the resource. If null the bundle for default locale is opened.
        Returns:
        a resource bundle for the given locale
        Status:
        Stable ICU 3.0.
      • getBundleInstance

        public static UResourceBundle getBundleInstance​(String baseName)
        [icu] Creates a UResourceBundle for the default locale and specified base name, from which users can extract resources by using their corresponding keys.
        Parameters:
        baseName - string containing the name of the data package. If null the default ICU package name is used.
        Returns:
        a resource bundle for the given base name and default locale
        Status:
        Stable ICU 3.0.
      • getBundleInstance

        public static UResourceBundle getBundleInstance​(String baseName,
                                                        Locale locale)
        [icu] Creates a UResourceBundle for the specified locale and specified base name, from which users can extract resources by using their corresponding keys.
        Parameters:
        baseName - string containing the name of the data package. If null the default ICU package name is used.
        locale - specifies the locale for which we want to open the resource. If null the bundle for default locale is opened.
        Returns:
        a resource bundle for the given base name and locale
        Status:
        Stable ICU 3.0.
      • getBundleInstance

        public static UResourceBundle getBundleInstance​(String baseName,
                                                        ULocale locale)
        [icu] Creates a UResourceBundle, from which users can extract resources by using their corresponding keys.
        Parameters:
        baseName - string containing the name of the data package. If null the default ICU package name is used.
        locale - specifies the locale for which we want to open the resource. If null the bundle for default locale is opened.
        Returns:
        a resource bundle for the given base name and locale
        Status:
        Stable ICU 3.0.
      • getBundleInstance

        public static UResourceBundle getBundleInstance​(String baseName,
                                                        Locale locale,
                                                        ClassLoader loader)
        [icu] Creates a UResourceBundle for the specified locale and specified base name, from which users can extract resources by using their corresponding keys.
        Parameters:
        baseName - string containing the name of the data package. If null the default ICU package name is used.
        locale - specifies the locale for which we want to open the resource. If null the bundle for default locale is opened.
        loader - the loader to use
        Returns:
        a resource bundle for the given base name and locale
        Status:
        Stable ICU 3.8.
      • getBundleInstance

        public static UResourceBundle getBundleInstance​(String baseName,
                                                        ULocale locale,
                                                        ClassLoader loader)
        [icu] Creates a UResourceBundle, from which users can extract resources by using their corresponding keys.

        Note: Please use this API for loading non-ICU resources. Java security does not allow loading of resources across jar files. You must provide your class loader to load the resources
        Parameters:
        baseName - string containing the name of the data package. If null the default ICU package name is used.
        locale - specifies the locale for which we want to open the resource. If null the bundle for default locale is opened.
        loader - the loader to use
        Returns:
        a resource bundle for the given base name and locale
        Status:
        Stable ICU 3.8.
      • getULocale

        public abstract ULocale getULocale()
        [icu] Returns the RFC 3066 conformant locale id of this resource bundle. This method can be used after a call to getBundleInstance() to determine whether the resource bundle returned really corresponds to the requested locale or is a fallback.
        Returns:
        the locale of this resource bundle
        Status:
        Stable ICU 3.0.
      • getLocaleID

        protected abstract String getLocaleID()
        [icu] Returns the localeID
        Returns:
        The string representation of the localeID
        Status:
        Stable ICU 3.0.
      • getBaseName

        protected abstract String getBaseName()
        [icu] Returns the base name of the resource bundle
        Returns:
        The string representation of the base name
        Status:
        Stable ICU 3.0.
      • getParent

        protected abstract UResourceBundle getParent()
        [icu] Returns the parent bundle
        Returns:
        The parent bundle
        Status:
        Stable ICU 3.0.
      • getLocale

        public Locale getLocale()
        Returns the locale of this bundle
        Overrides:
        getLocale in class ResourceBundle
        Returns:
        the locale of this resource bundle
        Status:
        Stable ICU 3.0.
      • instantiateBundle

        protected static UResourceBundle instantiateBundle​(String baseName,
                                                           String localeName,
                                                           ClassLoader root,
                                                           boolean disableFallback)
        [icu] Loads a new resource bundle for the given base name, locale and class loader. Optionally will disable loading of fallback bundles.
        Parameters:
        baseName - string containing the name of the data package. If null the default ICU package name is used.
        localeName - the locale for which a resource bundle is desired
        root - the class object from which to load the resource bundle
        disableFallback - disables loading of fallback lookup chain
        Returns:
        a resource bundle for the given base name and locale
        Throws:
        MissingResourceException - If no resource bundle for the specified base name can be found
        Status:
        Stable ICU 3.0.
      • getBinary

        public byte[] getBinary​(byte[] ba)
        [icu] Returns a binary data from a binary resource, as a byte array with a copy of the bytes from the resource bundle.
        Parameters:
        ba - The byte array to write the bytes to. A null variable is OK.
        Returns:
        an array of bytes containing the binary data from the resource.
        Throws:
        MissingResourceException - If resource bundle is missing.
        UResourceTypeMismatchException - If resource bundle has a type mismatch.
        See Also:
        getIntVector(), getInt()
        Status:
        Stable ICU 3.8.
      • getIntVector

        public int[] getIntVector()
        [icu] Returns a 32 bit integer array from a resource.
        Returns:
        a pointer to a chunk of unsigned bytes which live in a memory mapped/DLL file.
        Throws:
        MissingResourceException - If resource bundle is missing.
        UResourceTypeMismatchException - If resource bundle has a type mismatch.
        See Also:
        getBinary(), getInt()
        Status:
        Stable ICU 3.8.
      • get

        public UResourceBundle get​(String aKey)
        [icu] Returns a resource in a given resource that has a given key.
        Parameters:
        aKey - a key associated with the wanted resource
        Returns:
        a resource bundle object representing the resource
        Throws:
        MissingResourceException - If resource bundle is missing.
        Status:
        Stable ICU 3.8.
      • findTopLevel

        @Deprecated
        protected UResourceBundle findTopLevel​(String aKey)
        Deprecated.
        This API is ICU internal only.
        Returns a resource in a given resource that has a given key, or null if the resource is not found.
        Parameters:
        aKey - the key associated with the wanted resource
        Returns:
        the resource, or null
        See Also:
        get(String)
        Status:
        Internal. This API is ICU internal only.
      • getString

        public String getString​(int index)
        Returns the string in a given resource at the specified index.
        Parameters:
        index - an index to the wanted string.
        Returns:
        a string which lives in the resource.
        Throws:
        IndexOutOfBoundsException - If the index value is out of bounds of accepted values.
        UResourceTypeMismatchException - If resource bundle type mismatch.
        Status:
        Stable ICU 3.8.
      • get

        public UResourceBundle get​(int index)
        [icu] Returns the resource in a given resource at the specified index.
        Parameters:
        index - an index to the wanted resource.
        Returns:
        the sub resource UResourceBundle object
        Throws:
        IndexOutOfBoundsException - If the index value is out of bounds of accepted values.
        MissingResourceException - If the resource bundle is missing.
        Status:
        Stable ICU 3.8.
      • findTopLevel

        @Deprecated
        protected UResourceBundle findTopLevel​(int index)
        Deprecated.
        This API is ICU internal only.
        Returns a resource in a given resource that has a given index, or null if the resource is not found.
        Parameters:
        index - the index of the resource
        Returns:
        the resource, or null
        See Also:
        get(int)
        Status:
        Internal. This API is ICU internal only.
      • getKeys

        public Enumeration<String> getKeys()
        Returns the keys in this bundle as an enumeration
        Specified by:
        getKeys in class ResourceBundle
        Returns:
        an enumeration containing key strings, which is empty if this is not a bundle or a table resource
        Status:
        Stable ICU 3.8.
      • keySet

        @Deprecated
        public Set<String> keySet()
        Deprecated.
        This API is ICU internal only.
        Returns a Set of all keys contained in this ResourceBundle and its parent bundles.
        Overrides:
        keySet in class ResourceBundle
        Returns:
        a Set of all keys contained in this ResourceBundle and its parent bundles, which is empty if this is not a bundle or a table resource
        Status:
        Internal. This API is ICU internal only.
      • handleKeySet

        @Deprecated
        protected Set<String> handleKeySet()
        Deprecated.
        This API is ICU internal only.
        Returns a Set of the keys contained only in this ResourceBundle. This does not include further keys from parent bundles.
        Overrides:
        handleKeySet in class ResourceBundle
        Returns:
        a Set of the keys contained only in this ResourceBundle, which is empty if this is not a bundle or a table resource
        Status:
        Internal. This API is ICU internal only.
      • getSize

        public int getSize()
        [icu] Returns the size of a resource. Size for scalar types is always 1, and for vector/table types is the number of child resources.
        Note: Integer array is treated as a scalar type. There are no APIs to access individual members of an integer array. It is always returned as a whole.
        Returns:
        number of resources in a given resource.
        Status:
        Stable ICU 3.8.
      • getType

        public int getType()
        [icu] Returns the type of a resource. Available types are INT, ARRAY, BINARY, INT_VECTOR, STRING, TABLE.
        Returns:
        type of the given resource.
        Status:
        Stable ICU 3.8.
      • getVersion

        public VersionInfo getVersion()
        [icu] Return the version number associated with this UResourceBundle as an VersionInfo object.
        Returns:
        VersionInfo object containing the version of the bundle
        Status:
        Stable ICU 3.8.
      • getIterator

        public UResourceBundleIterator getIterator()
        [icu] Returns the iterator which iterates over this resource bundle
        Returns:
        UResourceBundleIterator that iterates over the resources in the bundle
        Status:
        Stable ICU 3.8.
      • getKey

        public String getKey()
        [icu] Returns the key associated with a given resource. Not all the resources have a key - only those that are members of a table.
        Returns:
        a key associated to this resource, or null if it doesn't have a key
        Status:
        Stable ICU 3.8.
      • handleGet

        protected UResourceBundle handleGet​(String aKey,
                                            HashMap<String,​String> aliasesVisited,
                                            UResourceBundle requested)
        [icu] Actual worker method for fetching a resource based on the given key. Sub classes must override this method if they support resources with keys.
        Parameters:
        aKey - the key string of the resource to be fetched
        aliasesVisited - hashtable object to hold references of resources already seen
        requested - the original resource bundle object on which the get method was invoked. The requested bundle and the bundle on which this method is invoked are the same, except in the cases where aliases are involved.
        Returns:
        UResourceBundle a resource associated with the key
        Status:
        Stable ICU 3.8.
      • handleGet

        protected UResourceBundle handleGet​(int index,
                                            HashMap<String,​String> aliasesVisited,
                                            UResourceBundle requested)
        [icu] Actual worker method for fetching a resource based on the given index. Sub classes must override this method if they support arrays of resources.
        Parameters:
        index - the index of the resource to be fetched
        aliasesVisited - hashtable object to hold references of resources already seen
        requested - the original resource bundle object on which the get method was invoked. The requested bundle and the bundle on which this method is invoked are the same, except in the cases where aliases are involved.
        Returns:
        UResourceBundle a resource associated with the index
        Status:
        Stable ICU 3.8.
      • handleGetStringArray

        protected String[] handleGetStringArray()
        [icu] Actual worker method for fetching the array of strings in a resource. Sub classes must override this method if they support arrays of strings.
        Returns:
        String[] An array of strings containing strings
        Status:
        Stable ICU 3.8.
      • handleGetKeys

        protected Enumeration<String> handleGetKeys()
        [icu] Actual worker method for fetching the keys of resources contained in the resource. Sub classes must override this method if they support keys and associated resources.
        Returns:
        Enumeration An enumeration of all the keys in this resource.
        Status:
        Stable ICU 3.8.
      • isTopLevelResource

        @Deprecated
        protected boolean isTopLevelResource()
        Deprecated.
        This API is ICU internal only.
        Is this a top-level resource, that is, a whole bundle?
        Returns:
        true if this is a top-level resource
        Status:
        Internal. This API is ICU internal only.