Develop and Download Open Source Software

Browse CVS Repository

Contents of /pal/libraries/common-utils/src/main/java/jp/sf/pal/common/util/LocaleUtil.java

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Thu Jan 25 22:41:33 2007 UTC (17 years, 1 month ago) by shinsuke
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-java
added common-util library

1 /*
2 * Copyright 2004-2007 The Portal Application Laboratory Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13 * either express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
15 */
16 package jp.sf.pal.common.util;
17
18 import java.util.Locale;
19 import java.util.StringTokenizer;
20
21 public class LocaleUtil {
22 private static final String DEFAULT_DELIM = "_";
23
24 public static Locale fallback(Locale locale1, Locale locale2) {
25 if (locale1 == null && locale2 == null) {
26 return null;
27 }
28 if ((locale1 == null && locale2 != null)
29 || (locale1 != null && locale2 == null)) {
30 return null;
31 }
32 if (locale1.equals(locale2)) {
33 return locale1;
34 }
35
36 if (locale1.getLanguage() != null && locale1.getCountry() != null
37 && locale1.getLanguage().equals(locale2.getLanguage())
38 && locale1.getCountry().equals(locale2.getCountry())) {
39 return new Locale(locale1.getLanguage(), locale2.getCountry());
40 }
41
42 if (locale1.getLanguage() != null
43 && locale1.getLanguage().equals(locale2.getLanguage())) {
44 return new Locale(locale1.getLanguage());
45 }
46
47 return null;
48 }
49
50 public static String convert(Locale locale) {
51 return convert(locale, DEFAULT_DELIM);
52 }
53
54 public static String convert(Locale locale, String delim) {
55 if (locale == null) {
56 return null;
57 }
58 String country = locale.getCountry();
59 String language = locale.getLanguage();
60 String variant = locale.getVariant();
61 StringBuffer buffer = new StringBuffer(40);
62 if (language != null) {
63 buffer.append(language);
64 }
65 buffer.append(delim);
66
67 if (country != null) {
68 buffer.append(country);
69 }
70 buffer.append(delim);
71
72 if (variant != null) {
73 buffer.append(variant);
74 }
75
76 return buffer.toString().trim();
77 }
78
79 public static Locale convert(String localeString) {
80 return convert(localeString, DEFAULT_DELIM);
81 }
82
83 public static Locale convert(String localeString, String delim) {
84 if (localeString == null) {
85 return null;
86 }
87 StringTokenizer tokenizer = new StringTokenizer(localeString,
88 DEFAULT_DELIM);
89
90 String language = tokenizer.nextToken().trim();
91 String country = null;
92 String variant = null;
93 if (tokenizer.hasMoreTokens()) {
94 country = tokenizer.nextToken().trim();
95 }
96
97 if (tokenizer.hasMoreTokens()) {
98 variant = tokenizer.nextToken().trim();
99 }
100
101 if (country != null && language != null && variant != null) {
102 return new Locale(language, country, variant);
103 } else if (country != null && language != null) {
104 return new Locale(language, country);
105 } else if (language != null) {
106 return new Locale(language, ""); // JDK 1.3 compatibility
107 } else {
108 return null;
109 }
110
111 }
112
113 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26