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