KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > LocaleUtility


1 /*
2  ******************************************************************************
3  * Copyright (C) 1996-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  ******************************************************************************
6  *
7  ******************************************************************************
8  */

9  
10 package com.ibm.icu.impl;
11
12 import java.util.Locale JavaDoc;
13
14 /**
15  * A class to hold utility functions missing from java.util.Locale.
16  */

17 public class LocaleUtility {
18
19     /**
20      * A helper function to convert a string of the form
21      * aa_BB_CC to a locale object. Why isn't this in Locale?
22      */

23     public static Locale JavaDoc getLocaleFromName(String JavaDoc name) {
24         String JavaDoc language = "";
25         String JavaDoc country = "";
26         String JavaDoc variant = "";
27
28         int i1 = name.indexOf('_');
29         if (i1 < 0) {
30             language = name;
31         } else {
32             language = name.substring(0, i1);
33             ++i1;
34             int i2 = name.indexOf('_', i1);
35             if (i2 < 0) {
36                 country = name.substring(i1);
37             } else {
38                 country = name.substring(i1, i2);
39                 variant = name.substring(i2+1);
40             }
41         }
42
43         return new Locale JavaDoc(language, country, variant);
44     }
45
46     /**
47      * Compare two locale strings of the form aa_BB_CC, and
48      * return true if parent is a 'strict' fallback of child, that is,
49      * if child =~ "^parent(_.+)*" (roughly).
50      */

51     public static boolean isFallbackOf(String JavaDoc parent, String JavaDoc child) {
52         if (!child.startsWith(parent)) {
53             return false;
54         }
55         int i = parent.length();
56         return (i == child.length() ||
57                 child.charAt(i) == '_');
58     }
59
60     /**
61      * Compare two locales, and return true if the parent is a
62      * 'strict' fallback of the child (parent string is a fallback
63      * of child string).
64      */

65     public static boolean isFallbackOf(Locale JavaDoc parent, Locale JavaDoc child) {
66         return isFallbackOf(parent.toString(), child.toString());
67     }
68
69
70     /**
71      * Convenience method that calls canonicalLocaleString(String) with
72      * locale.toString();
73      */

74     public static String JavaDoc canonicalLocaleString(Locale JavaDoc locale) {
75         return canonicalLocaleString(locale.toString());
76     }
77
78     /**
79      * You'd think that Locale canonicalizes, since it munges the
80      * renamed languages, but it doesn't quite. It forces the region
81      * to be upper case but doesn't do anything about the language or
82      * variant. Our canonical form is 'lower_UPPER_UPPER'.
83      */

84     public static String JavaDoc canonicalLocaleString(String JavaDoc id) {
85         if (id != null) {
86             int x = id.indexOf("_");
87             if (x == -1) {
88                 id = id.toLowerCase(Locale.ENGLISH);
89             } else {
90                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
91                 buf.append(id.substring(0, x).toLowerCase(Locale.ENGLISH));
92                 buf.append(id.substring(x).toUpperCase(Locale.ENGLISH));
93
94                 int len = buf.length();
95                 int n = len;
96                 while (--n >= 0 && buf.charAt(n) == '_') {
97                 }
98                 if (++n != len) {
99                     buf.delete(n, len);
100                 }
101                 id = buf.toString();
102             }
103         }
104         return id;
105     }
106
107     /**
108      * Fallback from the given locale name by removing the rightmost _-delimited
109      * element. If there is none, return the root locale ("", "", ""). If this
110      * is the root locale, return null. NOTE: The string "root" is not
111      * recognized; do not use it.
112      *
113      * @return a new Locale that is a fallback from the given locale, or null.
114      */

115     public static Locale JavaDoc fallback(Locale JavaDoc loc) {
116
117         // Split the locale into parts and remove the rightmost part
118
String JavaDoc[] parts = new String JavaDoc[]
119             { loc.getLanguage(), loc.getCountry(), loc.getVariant() };
120         int i;
121         for (i=2; i>=0; --i) {
122             if (parts[i].length() != 0) {
123                 parts[i] = "";
124                 break;
125             }
126         }
127         if (i<0) {
128             return null; // All parts were empty
129
}
130         return new Locale JavaDoc(parts[0], parts[1], parts[2]);
131     }
132 }
133
Popular Tags