org.enhydra.xml.xmlc.misc
Class I18nUtil

java.lang.Object
  |
  +--org.enhydra.xml.xmlc.misc.I18nUtil

public class I18nUtil
extends Object


Constructor Summary
I18nUtil()
           
 
Method Summary
static String canonicalLocaleString(String baseNameLessSuffix, String suffix, Locale locale)
           
static String escapeRegexMetaChars(String literal)
          Transforms any String literal into a valid regex pattern by escaping reserved regex characters.
static Locale locale(String localeStr)
          Translates a Locale String into a Locale, e.g....
static List localeStrings(String baseNameLessSuffix, String suffix, Locale locale)
           
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

I18nUtil

public I18nUtil()
Method Detail

canonicalLocaleString

public static String canonicalLocaleString(String baseNameLessSuffix,
                                           String suffix,
                                           Locale locale)
Parameters:
baseNameLessSuffix - the base name path, less any suffix, defaults to empty string if null
suffix - the suffix to append to the baseNameLessSuffix + Locale information, e.g., ".html". May be null, in which case it is ignored.
locale - the Locale
Returns:
the canonical, or longest, string generated based on the given Locale information in the form baseNameLessSuffix + "_[language]_[country]_[variant]" + suffix, or null if locale is empty, i.e., new Locale("", "", "")
Throws:
NullPointerException - if locale is null

localeStrings

public static List localeStrings(String baseNameLessSuffix,
                                 String suffix,
                                 Locale locale)
Parameters:
baseNameLessSuffix - the base name path, less any suffix, defaults to empty string if null
suffix - the suffix to append to the baseNameLessSuffix + Locale information, e.g., ".html". May be null, in which case it is ignored.
locale - the Locale
Returns:
a list of Locale-contextualized strings in the form baseNameLessSuffix + "_[language]_[country]_[variant]" + suffix, max size 3 elements, in the order of longest path to shortest path.
Throws:
NullPointerException - if locale is null

locale

public static Locale locale(String localeStr)
Translates a Locale String into a Locale, e.g....

"_en", "en", "en_", "en__", "_en__", "en_US", "en__US", "en_POSIX", "en__POSIX", "en___POSIX", "en_US_POSIX", "en__US__POSIX", "US", "US_", "_US", "_US_", "US_POSIX", "US__POSIX", "_US_POSIX", "_US__POSIX", "_POSIX", "__POSIX"

Matches any Locale.toString() output plus more using the following regular expression...

 ^[\s_]*?(?:_?([a-z]{2})_?)?(?:_?(?:([A-Z]{2})_?)?(?:_+?(\S+?))?)?\s*?$
 --OR--
 ^[\s_]*?(?:_?({language}[a-z]{2})_?)?(?:_?(?:({country}[A-Z]{2})_?)?(?:_+?({variant}\S+?))?)?\s*?$
 

Preceding and trailing whitespace is ignored. Single preceding underscores, connected or not to locale text, are ignored (though at least one is required to define a stand-alone variant). Two preceding contiguous underscores define a variant when either alone or connected to language or country locale text (or variant, obviously). Trailing underscores connected to the locale text are ignored unless part of the variant: three contiguous underscores trailing language, two contiguous underscores trailing country, or any trailing variant, e.g., ...

Parameters:
localeStr - a String representing a Locale
Returns:
the Locale corresponding to the provided localeStr or null if the localeStr is null or doesn't match any Locale parts

escapeRegexMetaChars

public static String escapeRegexMetaChars(String literal)
Transforms any String literal into a valid regex pattern by escaping reserved regex characters.

In lieu of (PatTwo supports this, but not JRegex)... String literal = "^some $ literal \\ [string] {containing} (various) * ? special | meta + characters."; literal = (literal = new Pattern("(\\S+)\\s*").replacer("\\\\Q$1\\\\E\\\\s+").replace(literal)).substring(0, literal.lastIndexOf("\\s+")); System.err.println(literal); //yields... \Q^some\E\s+\Q$\E\s+\Qliteral\E\s+\Q\\E\s+\Q[string]\E\s+\Q{containing}\E\s+\Q(various)\E\s+\Q*\E\s+\Q\E\s+\Q?\E\s+\Qspecial\E\s+\Q|\E\s+\Qmeta\E\s+\Q+\E\s+\Qcharacters.\E

Parameters:
literal - a String literal
Returns:
a valid regex pattern translation of the String literal


Copyright © 1999-2007 enhydra.org (Mark Diekhans, David Li, Richard Kunze, Jacob Kjome). All Rights reserved.