001: package org.enhydra.jawe.misc;
002:
003: import java.util.Locale;
004:
005: import org.enhydra.jawe.ResourceManager;
006:
007: /**
008: * Represents a locale that will be shown within the list. Contains a factory
009: * method that creates instance of the class by parsing the locale string that
010: * could be made of language, country and variant part.
011: * This class is introduced mainly because of presenting the default locale by
012: * "Default" key.
013: */
014: public class PFLocale {
015: private Locale loc;
016:
017: /**
018: * Parses given string representing property file extendion. The given string
019: * could be made of language, country and variant part.
020: */
021: public static PFLocale createPFLocale(String pfExt) {
022: PFLocale pel = null;
023: try {
024: pfExt = pfExt.trim();
025: if (pfExt.equals("default")) {
026: pel = new PFLocale();
027: } else {
028: boolean hasCountry = pfExt.length() > 2
029: && pfExt.substring(2, 3).equals("_");
030: if (hasCountry) {
031: boolean hasVariant = pfExt.length() > 5
032: && pfExt.substring(5, 6).equals("_");
033: if (hasVariant) {
034: pel = new PFLocale(pfExt.substring(0, 2), pfExt
035: .substring(3, 5), pfExt.substring(6,
036: pfExt.length()));
037: } else {
038: if (pfExt.length() == 5) {
039: pel = new PFLocale(pfExt.substring(0, 2),
040: pfExt.substring(3, 5));
041: }
042: }
043: } else {
044: if (pfExt.length() == 2) {
045: pel = new PFLocale(pfExt);
046: }
047: }
048: }
049: } catch (Exception ex) {
050: }
051:
052: return pel;
053: }
054:
055: public PFLocale() {
056: loc = ResourceManager.getDefaultLocale();
057: }
058:
059: public PFLocale(Locale l) {
060: loc = l;
061: }
062:
063: public PFLocale(String lang) {
064: loc = new Locale(lang);
065: }
066:
067: public PFLocale(String lang, String country) {
068: loc = new Locale(lang, country);
069: }
070:
071: public PFLocale(String lang, String country, String variant) {
072: loc = new Locale(lang, country, variant);
073: }
074:
075: public String toString() {
076: if (loc != ResourceManager.getDefaultLocale()) {
077: return loc.getDisplayName(ResourceManager
078: .getChoosenLocale());
079: }
080:
081: return ResourceManager
082: .getLanguageDependentString("EnglishDefaultKey");
083: }
084:
085: public Locale getLocale() {
086: return loc;
087: }
088:
089: public String getLanguage() {
090: return loc.getLanguage();
091: }
092:
093: public String getCountry() {
094: return loc.getCountry();
095: }
096:
097: public String getVariant() {
098: return loc.getVariant();
099: }
100:
101: public String getLocaleString() {
102: String locStr = "";
103: if (loc == ResourceManager.getDefaultLocale()) {
104: return "default";
105: }
106:
107: locStr = loc.getLanguage();
108: if (loc.getCountry().length() > 0) {
109: locStr += "_" + loc.getCountry();
110: if (loc.getVariant().length() > 0) {
111: locStr += "_" + loc.getVariant();
112: }
113: }
114:
115: return locStr;
116: }
117:
118: public boolean equals(Object pfLoc) {
119: if (pfLoc instanceof PFLocale) {
120: return loc.equals(((PFLocale) pfLoc).loc);
121: }
122:
123: return false;
124: }
125:
126: }
|