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