01: /**********************************************************************
02: Copyright (c) 2003 Erik Bengtson and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15: Contributors:
16: ...
17: **********************************************************************/package org.jpox.util;
18:
19: import java.util.Locale;
20:
21: /**
22: * Utility class for internationalization. This class provides a
23: * central location to do specialized formatting in both
24: * a default and a locale specfic manner.
25: *
26: * @version $Revision: 1.2 $
27: */
28: public final class I18nUtils {
29: private I18nUtils() {
30: // protects from instantiation
31: }
32:
33: /**
34: * Convert a string based locale into a Locale Object.
35: * Assumes the string has form "{language}_{country}_{variant}".
36: * Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr_MAC"
37: *
38: * @param localeString The String
39: * @return the Locale
40: */
41: public static Locale getLocaleFromString(String localeString) {
42: if (localeString == null) {
43: return null;
44: }
45: localeString = localeString.trim();
46: if (localeString.toLowerCase().equals("default")) {
47: return Locale.getDefault();
48: }
49:
50: // Extract language
51: int languageIndex = localeString.indexOf('_');
52: String language = null;
53: if (languageIndex == -1) {
54: // No further "_" so is "{language}" only
55: return new Locale(localeString, "");
56: } else {
57: language = localeString.substring(0, languageIndex);
58: }
59:
60: // Extract country
61: int countryIndex = localeString.indexOf('_', languageIndex + 1);
62: String country = null;
63: if (countryIndex == -1) {
64: // No further "_" so is "{language}_{country}"
65: country = localeString.substring(languageIndex + 1);
66: return new Locale(language, country);
67: } else {
68: // Assume all remaining is the variant so is "{language}_{country}_{variant}"
69: country = localeString.substring(languageIndex + 1,
70: countryIndex);
71: String variant = localeString.substring(countryIndex + 1);
72: return new Locale(language, country, variant);
73: }
74: }
75: }
|