|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object | +--com.ca.commons.cbutil.CBResourceBundle
Java's PropertyResourceBundle class is tragically bad. Why a class intended for i18n use deliberately restricts itself to 8859-1 characters, while using a bizarre unicode escaping format rather than utf8, is beyond me (and certainly beyond any translators I need to send stuff to.)
This class is a reimplementation that automatically selects whether a file is 16bit unicode, utf-8, or local character encoding, and loads it accordingly. Otherwise it is intended to be functionally very similar to PropertiesResourceBundle.
Note that this class does not extend ResourceBundle, as ResourceBundle is difficult to extend - all the functionality is hidden away in private methods. So even though ResourceBundle does some neat things, we won't be doing anything but a bare bones rewrite here. Important: This uses a simplified form of the properties file - keys and phrases are separated by an '=' sign, and while, for backward compatibility, keys can have escaped characters, except for '=', they don't have to. A further restriction is that the '=' sign may not be immediately preceeded by an escaped escape character (i.e. '\\=' is illegal) - the '=' sign must be preceeded by a space character in this case. Any space characters at the start and end of a key/description are trimmed.
e.g. [values in square brackets represent byte values within the file] potato = kartoffel // normal ascii (german) help = [30 d8 30 eb 30d7] // 16 bit unicode (japanese) file = [e6 96 87 e6 a1 a3] // utf-8 (chinese)
| Constructor Summary | |
CBResourceBundle(java.lang.String baseName)
This creates a Resource bundle using only the name of the the resource bundle (e.g. |
|
CBResourceBundle(java.lang.String baseName,
java.util.Locale locale)
This creates a Resource bundle the name of the the resource bundle (e.g. |
|
CBResourceBundle(java.lang.String baseName,
java.util.Locale locale,
java.lang.ClassLoader loader)
This creates a Resource bundle the name of the the resource bundle (e.g. |
|
| Method Summary | |
java.lang.Object |
get(java.lang.Object key)
Returns the object corresponding to a given key. |
protected static java.util.Vector |
getBundleNames(java.lang.String baseName,
java.util.Locale locale)
Calculate the bundles along the search path from the base bundle to the bundle specified by baseName and locale. |
java.util.Enumeration |
getKeys()
returns the translation keys. |
java.lang.Object |
getObject(java.lang.Object key)
Returns the object corresponding to a given key. |
java.lang.String |
getString(java.lang.String key)
Convenience class returning a particular object as a String. |
java.util.Enumeration |
keys()
returns the translation keys. |
protected void |
loadBundle(java.lang.String baseName,
java.util.Locale locale,
java.lang.ClassLoader loader)
This method searches through all the valid permutations of the base bundle name (modified for locale - e.g. |
protected boolean |
loadData(java.net.URL url)
This loads the data from a translation file, checking on the way what file format it is in. |
protected boolean |
parseData(java.lang.String text)
parses the byte array as per a normal resource file (i.e. looking for key/data pairs seperated by an unescaped '=' sign) after first converting the byte array into a String, using whichever language encoding (unicode16, utf8, locale-specific) seems appropriate. |
protected java.lang.String |
unescape(java.lang.String escapeMe)
Removes all escapes ('\?' |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public CBResourceBundle(java.lang.String baseName)
baseName - the name of the translation file to look up.
this name is extended using the standard locality rules
to try to find localised files (e.g. "language.JX" becomes
language/JX_fr_CA in french-speaking canada).
public CBResourceBundle(java.lang.String baseName,
java.util.Locale locale)
baseName - the name of the translation file to look up.
this name is extended using the standard locality rules
to try to find localised files (e.g. "language.JX" becomes
language/JX_fr_CA in french-speaking canada).locale - a specific locale to use in place of the default
system locale.
public CBResourceBundle(java.lang.String baseName,
java.util.Locale locale,
java.lang.ClassLoader loader)
baseName - the name of the translation file to look up.
this name is extended using the standard locality rules
to try to find localised files (e.g. "language.JX" becomes
language/JX_fr_CA in french-speaking canada).locale - a specific locale to use in place of the default
system locale.loader - a custom class loader (such as CBClassLoader) used to
retrieve the translation file.| Method Detail |
protected void loadBundle(java.lang.String baseName,
java.util.Locale locale,
java.lang.ClassLoader loader)
If successful, it loads the data (the translation strings) into a local data store (Hashtable).
baseName - the name of the translation file to look up.
this name is extended using the standard locality rules
to try to find localised files (e.g. "language.JX" becomes
language/JX_fr_CA in french-speaking canada).locale - a specific locale to use in place of the default
system locale.loader - a custom class loader (such as CBClassLoader) used to
retrieve the translation file.
protected static java.util.Vector getBundleNames(java.lang.String baseName,
java.util.Locale locale)
baseName - the base bundle namelocale - the localeprotected boolean loadData(java.net.URL url)
url - the URL to read the data InputStream from.
protected boolean parseData(java.lang.String text)
protected java.lang.String unescape(java.lang.String escapeMe)
public java.util.Enumeration keys()
public java.util.Enumeration getKeys()
public java.lang.Object get(java.lang.Object key)
key - the original text to translate/look up
public java.lang.Object getObject(java.lang.Object key)
key - the original text to translate/look up
public java.lang.String getString(java.lang.String key)
key - the original text to translate/look up
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||