001: /**
002: * Copyright (C) 2001 Yasna.com. All rights reserved.
003: *
004: * ===================================================================
005: * The Apache Software License, Version 1.1
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * Yasna.com (http://www.yasna.com)."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "Yazd" and "Yasna.com" must not be used to
027: * endorse or promote products derived from this software without
028: * prior written permission. For written permission, please
029: * contact yazd@yasna.com.
030: *
031: * 5. Products derived from this software may not be called "Yazd",
032: * nor may "Yazd" appear in their name, without prior written
033: * permission of Yasna.com.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of Yasna.com. For more information
051: * on Yasna.com, please see <http://www.yasna.com>.
052: */package com.Yasna.forum.locale;
053:
054: import com.Yasna.forum.PropertyManager;
055:
056: import java.util.Locale;
057: import java.util.ResourceBundle;
058:
059: public class YazdLocale {
060: private static Locale yazdlocale = null;
061: public final static Locale[] supportedLocales = { Locale.CANADA,
062: Locale.CANADA_FRENCH, Locale.US, Locale.GERMAN,
063: Locale.SIMPLIFIED_CHINESE, Locale.ITALIAN,
064: // new Locale("bg"),
065: new Locale("no"), new Locale("es"), new Locale("da"),
066: new Locale("ar") };
067:
068: public static Locale getDefaultYazdLocale() {
069: if (yazdlocale == null) {
070: setDefaultLocale();
071: }
072: return yazdlocale;
073: }
074:
075: public static String getLocaleKey(String key, Locale locale) {
076: ResourceBundle values;
077: if (locale == null) {
078: values = ResourceBundle.getBundle("yazdLocale",
079: getDefaultYazdLocale());
080: } else {
081: values = ResourceBundle.getBundle("yazdLocale", locale);
082: }
083: return values.getString(key);
084: }
085:
086: private static void setDefaultLocale() {
087: if ("".equals(PropertyManager.getProperty("DefaultLocale"))
088: || PropertyManager.getProperty("DefaultLocale") == null) {
089: yazdlocale = Locale.CANADA;
090: } else {
091: String locale = PropertyManager
092: .getProperty("DefaultLocale");
093: String lang = null, country = null, variant = null;
094: int pos = 0;
095: int delPos = 0;
096: if ((delPos = locale.indexOf(",", pos)) != -1) {
097: lang = locale.substring(pos, delPos);
098: pos = delPos + 1; // next character after the delimiter
099: } else if (pos <= locale.length()) {
100: lang = locale.substring(pos);
101: pos = locale.length() + 1;
102: }
103: if ((delPos = locale.indexOf(",", pos)) != -1) {
104: country = locale.substring(pos, delPos);
105: pos = delPos + 1; // next character after the delimiter
106: } else if (pos <= locale.length()) {
107: country = locale.substring(pos);
108: pos = locale.length() + 1;
109: }
110: if (pos <= locale.length()) {
111: // add rest of String
112: variant = locale.substring(pos);
113:
114: }
115: if (variant != null) {
116: yazdlocale = new Locale(lang, country, variant);
117: } else if (country != null) {
118: //this is the one that should return most values.
119: yazdlocale = new Locale(lang, country);
120: } else {
121: yazdlocale = new Locale(lang);
122: }
123: }
124:
125: }
126: }
|