| java.lang.Object org.mmbase.util.LocalizedString
All known Subclasses: org.mmbase.util.WrappedLocalizedString,
LocalizedString | public class LocalizedString implements java.io.Serializable,Cloneable(Code) | |
A String which is localized. There are two mechanisms to find and provide translations: They can
explicitely be set with
LocalizedString.set (e.g. during parsing an XML), or a resource-bundle can be
associated with
LocalizedString.setBundle , which will be used to find translations based on the key of
this object.
The 'set' mechanism can also be driven by
LocalizedString.fillFromXml , which provides a sensible way to fill the LocalizedString with
setting from a sub element of XMLs.
The idea is that objects of this type can be used in stead of normal String objects, for error
messages, descriptions and other texts which need localization (e.g. because they are exposed to
end-users).
author: Michiel Meeuwissen version: $Id: LocalizedString.java,v 1.31 2007/05/23 13:19:59 michiel Exp $ since: MMBase-1.8 |
Method Summary | |
public Map<Locale, String> | asMap() Returns a Map representation of the localisation setting represented by this
LocalizedString. | public Object | clone() | public static Locale | degrade(Locale locale, Locale originalLocale) Degrades a Locale object to a more general Locale. | public boolean | equals(Object o) | public void | fillFromXml(String tagName, Element element) Given a certain tagname, and a DOM parent element, it configures this LocalizedString, using
subtags with this tagname with 'xml:lang' attributes. | public String | get(Locale locale) Gets the value for a certain locale. | public static Locale | getDefault() Returns the default locale if set, or otherwise the system default (
java.util.Locale.getDefault ). | public String | getKey() | public static Locale | getLocale(Element element) | public static Locale | getLocale(String xmlLang) | public static String | getXmlLang(Locale locale) This utility determines the value of an xml:lang attribute. | public int | hashCode() | public void | set(String value, Locale locale) Sets the value for a certain locale. | public void | setBundle(String b) A resource-bundle with given name can be associated to this LocalizedString. | public static Locale | setDefault(Locale locale) Sets a default locale for this JVM or web-app. | public void | setKey(String key) | public static void | setXmlLang(Element element, Locale locale) This utility takes care of setting the xml:lang attribute on an element. | public String | toString() For LocalizedString this returns the String for the default Locale (see
LocalizedString.getDefault ). | public static Collection<String> | toStrings(Collection<LocalizedString> col, Locale locale) Converts a collection of localized strings to a collection of normal strings. | public void | toXml(String tagName, String ns, Element element, String path) Writes this LocalizedString object back to an XML, i.e. |
LocalizedString | protected LocalizedString()(Code) | | |
LocalizedString | public LocalizedString(String k)(Code) | | Parameters: k - The key of this String, if k == null then the first set will define it. |
asMap | public Map<Locale, String> asMap()(Code) | | Returns a Map representation of the localisation setting represented by this
LocalizedString. It is an unmodifiable mapping: Locale -> localized value.
|
degrade | public static Locale degrade(Locale locale, Locale originalLocale)(Code) | | Degrades a Locale object to a more general Locale. Principally this means that first the
'variant' will be dropped and then the country. As an extra the 'variant' is also degraded
progressively. This is done by taking away parts (from the end) which are separated by
underscore characters. Also, after degrading the country, also locales are tried with no
country, but with a variant only.
So e.g. nl_BE_a_b is degraded to nl_BE_a, then nl_BE, then nl__a_b, then nl__a, then nl.
Parameters: locale - The locale to be degraded Parameters: originalLocale - The original locale (used to find back the original variant afterdropping the country) A degraded Locale of null if the locale could not be degraded any further. since: MMBase-1.8.5 |
fillFromXml | public void fillFromXml(String tagName, Element element)(Code) | | Given a certain tagname, and a DOM parent element, it configures this LocalizedString, using
subtags with this tagname with 'xml:lang' attributes. This boils down to repeative calls to
LocalizedString.set(String,Locale) .
|
get | public String get(Locale locale)(Code) | | Gets the value for a certain locale. If no match is found, it falls back to the key.
|
getKey | public String getKey()(Code) | | Gets the key to use as a default and/or for obtaining a value from the bundle
|
getLocale | public static Locale getLocale(Element element)(Code) | | This utility takes care of reading the xml:lang attribute from an element
Parameters: element - a DOM element A java.util.Locale object, or null if the element did not have,or had an empty, xml:lang attribute |
getXmlLang | public static String getXmlLang(Locale locale)(Code) | | This utility determines the value of an xml:lang attribute. So, given a
java.util.Locale it produces a String.
Parameters: locale - A java locale A string that can be used as the value for an XML xml:lang attribute. since: MMBase-1.8.1 |
hashCode | public int hashCode()(Code) | | |
set | public void set(String value, Locale locale)(Code) | | Sets the value for a certain locale. If the value for a more general locale is still unset,
it will also set that (so, it sets also nl when setting nl_BE if nl still is unset).
|
setBundle | public void setBundle(String b)(Code) | | A resource-bundle with given name can be associated to this LocalizedString. If no
translations were explicitely added, it can be used to look up the translation in the bundle,
using the key.
|
setDefault | public static Locale setDefault(Locale locale)(Code) | | Sets a default locale for this JVM or web-app. When not using it, the locale is the system
default. Several web-apps do run in one JVM however and it is very imaginable that you want a
different default for the Locale.
So, this function can be called only once. Calling it the second time will not do
anything. It returns the already set default locale then, which should probably prompt you to log an error
or throw an exception or so. Otherwise it returns null indicating that the
default locale is now what you just set.
|
setKey | public void setKey(String key)(Code) | | Sets the key to use as a default and/or for obtaining a value from the bundle
|
setXmlLang | public static void setXmlLang(Element element, Locale locale)(Code) | | This utility takes care of setting the xml:lang attribute on an element.
Parameters: element - Element on which the xml:lang attribute is going to be set Parameters: locale - Java's Locale object since: MMBase-1.8.1 |
toXml | public void toXml(String tagName, String ns, Element element, String path)(Code) | | Writes this LocalizedString object back to an XML, i.e. it searches for and creates
sub-elements (identified by xml:lang attributes) of a certain given parent element, and sets
the node-text-value of those elements corresponding to the locale.
Parameters: tagName - Tag-name of the to be used sub-elements Parameters: ns - Namespace of the to be created sub-elements, or null Parameters: element - The parent element which must contain the localized elements. Parameters: path - A comma separated list of names of tags which must skipped, before appendingchilds. See org.mmbase.util.xml.DocumentReader.appendChild(ElementElementString). since: MMBase-1.8.1 |
|
|