2 ******************************************************************************
\r
3 * Copyright (C) 1996-2007, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 ******************************************************************************
\r
7 ******************************************************************************
\r
10 package com.ibm.icu.impl;
\r
12 import java.util.Locale;
\r
15 * A class to hold utility functions missing from java.util.Locale.
\r
17 public class LocaleUtility {
\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
23 public static Locale getLocaleFromName(String name) {
\r
24 String language = "";
\r
25 String country = "";
\r
26 String variant = "";
\r
28 int i1 = name.indexOf('_');
\r
32 language = name.substring(0, i1);
\r
34 int i2 = name.indexOf('_', i1);
\r
36 country = name.substring(i1);
\r
38 country = name.substring(i1, i2);
\r
39 variant = name.substring(i2+1);
\r
43 return new Locale(language, country, variant);
\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
51 public static boolean isFallbackOf(String parent, String child) {
\r
52 if (!child.startsWith(parent)) {
\r
55 int i = parent.length();
\r
56 return (i == child.length() ||
\r
57 child.charAt(i) == '_');
\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
65 public static boolean isFallbackOf(Locale parent, Locale child) {
\r
66 return isFallbackOf(parent.toString(), child.toString());
\r
71 * Convenience method that calls canonicalLocaleString(String) with
\r
72 * locale.toString();
\r
74 /*public static String canonicalLocaleString(Locale locale) {
\r
75 return canonicalLocaleString(locale.toString());
\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
84 /*public static String canonicalLocaleString(String id) {
\r
86 int x = id.indexOf("_");
\r
88 id = id.toLowerCase(Locale.ENGLISH);
\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
94 int len = buf.length();
\r
96 while (--n >= 0 && buf.charAt(n) == '_') {
\r
101 id = buf.toString();
\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
113 * @return a new Locale that is a fallback from the given locale, or null.
\r
115 public static Locale fallback(Locale loc) {
\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
121 for (i=2; i>=0; --i) {
\r
122 if (parts[i].length() != 0) {
\r
128 return null; // All parts were empty
\r
130 return new Locale(parts[0], parts[1], parts[2]);
\r