]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/impl/LocaleUtility.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / impl / LocaleUtility.java
1 /*\r
2  ******************************************************************************\r
3  * Copyright (C) 1996-2007, International Business Machines Corporation and   *\r
4  * others. All Rights Reserved.                                               *\r
5  ******************************************************************************\r
6  *\r
7  ******************************************************************************\r
8  */\r
9  \r
10 package com.ibm.icu.impl;\r
11 \r
12 import java.util.Locale;\r
13 \r
14 /**\r
15  * A class to hold utility functions missing from java.util.Locale.\r
16  */\r
17 public class LocaleUtility {\r
18 \r
19     /**\r
20      * A helper function to convert a string of the form\r
21      * aa_BB_CC to a locale object.  Why isn't this in Locale?\r
22      */\r
23     public static Locale getLocaleFromName(String name) {\r
24         String language = "";\r
25         String country = "";\r
26         String variant = "";\r
27 \r
28         int i1 = name.indexOf('_');\r
29         if (i1 < 0) {\r
30             language = name;\r
31         } else {\r
32             language = name.substring(0, i1);\r
33             ++i1;\r
34             int i2 = name.indexOf('_', i1);\r
35             if (i2 < 0) {\r
36                 country = name.substring(i1);\r
37             } else {\r
38                 country = name.substring(i1, i2);\r
39                 variant = name.substring(i2+1);\r
40             }\r
41         }\r
42 \r
43         return new Locale(language, country, variant);\r
44     }\r
45 \r
46     /**\r
47      * Compare two locale strings of the form aa_BB_CC, and\r
48      * return true if parent is a 'strict' fallback of child, that is,\r
49      * if child =~ "^parent(_.+)*" (roughly).\r
50      */\r
51     public static boolean isFallbackOf(String parent, String child) {\r
52         if (!child.startsWith(parent)) {\r
53             return false;\r
54         }\r
55         int i = parent.length();\r
56         return (i == child.length() ||\r
57                 child.charAt(i) == '_');\r
58     }\r
59 \r
60     /**\r
61      * Compare two locales, and return true if the parent is a\r
62      * 'strict' fallback of the child (parent string is a fallback\r
63      * of child string).\r
64      */\r
65     public static boolean isFallbackOf(Locale parent, Locale child) {\r
66         return isFallbackOf(parent.toString(), child.toString());\r
67     }\r
68 \r
69 \r
70     /*\r
71      * Convenience method that calls canonicalLocaleString(String) with\r
72      * locale.toString();\r
73      */\r
74     /*public static String canonicalLocaleString(Locale locale) {\r
75         return canonicalLocaleString(locale.toString());\r
76     }*/\r
77 \r
78     /*\r
79      * You'd think that Locale canonicalizes, since it munges the\r
80      * renamed languages, but it doesn't quite.  It forces the region\r
81      * to be upper case but doesn't do anything about the language or\r
82      * variant.  Our canonical form is 'lower_UPPER_UPPER'.  \r
83      */\r
84     /*public static String canonicalLocaleString(String id) {\r
85         if (id != null) {\r
86             int x = id.indexOf("_");\r
87             if (x == -1) {\r
88                 id = id.toLowerCase(Locale.ENGLISH);\r
89             } else {\r
90                 StringBuffer buf = new StringBuffer();\r
91                 buf.append(id.substring(0, x).toLowerCase(Locale.ENGLISH));\r
92                 buf.append(id.substring(x).toUpperCase(Locale.ENGLISH));\r
93 \r
94                 int len = buf.length();\r
95                 int n = len;\r
96                 while (--n >= 0 && buf.charAt(n) == '_') {\r
97                 }\r
98                 if (++n != len) {\r
99                     buf.delete(n, len);\r
100                 }\r
101                 id = buf.toString();\r
102             }\r
103         }\r
104         return id;\r
105     }*/\r
106 \r
107     /**\r
108      * Fallback from the given locale name by removing the rightmost _-delimited\r
109      * element. If there is none, return the root locale ("", "", ""). If this\r
110      * is the root locale, return null. NOTE: The string "root" is not\r
111      * recognized; do not use it.\r
112      * \r
113      * @return a new Locale that is a fallback from the given locale, or null.\r
114      */\r
115     public static Locale fallback(Locale loc) {\r
116 \r
117         // Split the locale into parts and remove the rightmost part\r
118         String[] parts = new String[]\r
119             { loc.getLanguage(), loc.getCountry(), loc.getVariant() };\r
120         int i;\r
121         for (i=2; i>=0; --i) {\r
122             if (parts[i].length() != 0) {\r
123                 parts[i] = "";\r
124                 break;\r
125             }\r
126         }\r
127         if (i<0) {\r
128             return null; // All parts were empty\r
129         }\r
130         return new Locale(parts[0], parts[1], parts[2]);\r
131     }\r
132 }\r