1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.base; 6 7 import org.chromium.base.annotations.CalledByNative; 8 9 import java.util.Locale; 10 11 /** 12 * This class provides the locale related methods. 13 */ 14 public class LocaleUtils { 15 /** 16 * Guards this class from being instantiated. 17 */ LocaleUtils()18 private LocaleUtils() { 19 } 20 21 /** 22 * @return the string for the given locale, translating 23 * Android deprecated language codes into the modern ones 24 * used by Chromium. 25 */ getLocale(Locale locale)26 public static String getLocale(Locale locale) { 27 String language = locale.getLanguage(); 28 String country = locale.getCountry(); 29 30 // Android uses deprecated lanuages codes for Hebrew and Indonesian but Chromium uses the 31 // updated codes. Also, Android uses "tl" while Chromium uses "fil" for Tagalog/Filipino. 32 // So apply a mapping. 33 // See http://developer.android.com/reference/java/util/Locale.html 34 if ("iw".equals(language)) { 35 language = "he"; 36 } else if ("in".equals(language)) { 37 language = "id"; 38 } else if ("tl".equals(language)) { 39 language = "fil"; 40 } 41 return country.isEmpty() ? language : language + "-" + country; 42 } 43 44 /** 45 * @return the default locale, translating Android deprecated 46 * language codes into the modern ones used by Chromium. 47 */ 48 @CalledByNative getDefaultLocale()49 public static String getDefaultLocale() { 50 return getLocale(Locale.getDefault()); 51 } 52 53 /** 54 * Get the default country code set during install. 55 * @return country code. 56 */ 57 @CalledByNative getDefaultCountryCode()58 private static String getDefaultCountryCode() { 59 CommandLine commandLine = CommandLine.getInstance(); 60 return commandLine.hasSwitch(BaseSwitches.DEFAULT_COUNTRY_CODE_AT_INSTALL) 61 ? commandLine.getSwitchValue(BaseSwitches.DEFAULT_COUNTRY_CODE_AT_INSTALL) 62 : Locale.getDefault().getCountry(); 63 } 64 65 } 66