Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: base/android/java/src/org/chromium/base/LocaleUtils.java

Issue 2406203002: Use BCP47 compliant format for locale representation (Closed)
Patch Set: format return in description Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.base; 5 package org.chromium.base;
6 6
7 import android.os.Build;
8
7 import org.chromium.base.annotations.CalledByNative; 9 import org.chromium.base.annotations.CalledByNative;
8 10
9 import java.util.Locale; 11 import java.util.Locale;
10 12
11 /** 13 /**
12 * This class provides the locale related methods. 14 * This class provides the locale related methods.
13 */ 15 */
14 public class LocaleUtils { 16 public class LocaleUtils {
15 /** 17 /**
16 * Guards this class from being instantiated. 18 * Guards this class from being instantiated.
17 */ 19 */
18 private LocaleUtils() { 20 private LocaleUtils() {
19 } 21 }
20 22
21 /** 23 /**
22 * @return The string for the given locale, translating Android deprecated l anguage codes 24 * @return the string for the given locale with language and/or country code , translating
23 * into the modern ones used by Chromium. 25 * Android deprecated language codes into the modern ones used by Ch romium.
24 */ 26 */
25 public static String getLocale(Locale locale) { 27 public static String getLocale(Locale locale) {
26 String language = getLanguage(locale); 28 String language = languageAdjust(locale.getLanguage());
27 String country = locale.getCountry(); 29 String country = locale.getCountry();
28 30
29 return country.isEmpty() ? language : language + "-" + country; 31 return country.isEmpty() ? language : language + "-" + country;
30 } 32 }
31 33
32 /** 34 /**
33 * @return The language for the given locale, translating Android deprecated languages codes 35 * Handle the special cases in converting a language code/region code pair i nto an ISO-639-1
34 * into modern ones used by Chromium. 36 * language tag.
35 */ 37 * @return a updated language code if necessary.
36 public static String getLanguage(Locale locale) { 38 **/
37 String language = locale.getLanguage(); 39 private static String languageAdjust(String language) {
38 40 // Android uses deprecated lanuages codes for Hebrew, Yiddish and Indone sian but
39 // Android uses deprecated lanuages codes for Hebrew and Indonesian but Chromium uses the 41 // Chromium uses the updated codes. Also, Android uses "tl" while Chromi um uses "fil"
40 // updated codes. Also, Android uses "tl" while Chromium uses "fil" for Tagalog/Filipino. 42 // for Tagalog/Filipino. So apply a mapping.
41 // So apply a mapping.
42 // See http://developer.android.com/reference/java/util/Locale.html 43 // See http://developer.android.com/reference/java/util/Locale.html
43 if ("iw".equals(language)) { 44 if ("iw".equals(language)) {
44 language = "he"; 45 return "he";
46 } else if ("ji".equals(language)) {
47 return "yi";
45 } else if ("in".equals(language)) { 48 } else if ("in".equals(language)) {
46 language = "id"; 49 return "id";
47 } else if ("tl".equals(language)) { 50 } else if ("tl".equals(language)) {
48 language = "fil"; 51 return "fil";
52 } else {
53 return language;
49 } 54 }
50 return language;
51 } 55 }
52 56
53 /** 57 /**
58 * This function does the same as Locale.forLanguageTag() when API level is lower than 21.
59 * @return the best representative locale with the language tag that has lan guage and/or
60 * country code.
61 */
62 public static Locale forLanguageTagCompat(String languageTag) {
63 if (languageTag.length() == 0) {
64 return new Locale("");
65 }
66 String[] tag = languageTag.split("-");
67 String language = tag[0];
68 if (language.length() != 2 && language.length() != 3) {
69 return new Locale("");
70 }
71 if (tag.length == 1) {
72 language = languageAdjust(languageTag);
73 return new Locale(language);
74 }
75 String country = tag[1];
76 if (country.length() != 2 && country.length() != 3) {
77 return new Locale(language);
78 }
79 return new Locale(language, country);
80 }
81
82 /**
83 * @return the locale that best represents the language tag that has languag e and/or
84 * country code.
85 */
86 public static Locale forLanguageTag(String languageTag) {
87 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
88 return Locale.forLanguageTag(languageTag);
89 }
90 return LocaleUtils.forLanguageTagCompat(languageTag);
91 }
92
93 /**
94 * @return a well-formed IETF BCP 47 language tag with language and country code that
95 * represents this locale.
96 */
97 public static String toLanguageTag(Locale locale) {
98 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
99 return locale.toLanguageTag();
100 }
101 return getLocale(locale);
102 }
103
104 /**
54 * @return The default locale, translating Android deprecated language codes into the modern 105 * @return The default locale, translating Android deprecated language codes into the modern
55 * ones used by Chromium. 106 * ones used by Chromium.
56 */ 107 */
57 @CalledByNative 108 @CalledByNative
58 public static String getDefaultLocale() { 109 public static String getDefaultLocale() {
59 return getLocale(Locale.getDefault()); 110 return getLocale(Locale.getDefault());
60 } 111 }
61 112
62 /** 113 /**
63 * @return The default country code set during install. 114 * @return The default country code set during install.
64 */ 115 */
65 @CalledByNative 116 @CalledByNative
66 private static String getDefaultCountryCode() { 117 private static String getDefaultCountryCode() {
67 CommandLine commandLine = CommandLine.getInstance(); 118 CommandLine commandLine = CommandLine.getInstance();
68 return commandLine.hasSwitch(BaseSwitches.DEFAULT_COUNTRY_CODE_AT_INSTAL L) 119 return commandLine.hasSwitch(BaseSwitches.DEFAULT_COUNTRY_CODE_AT_INSTAL L)
69 ? commandLine.getSwitchValue(BaseSwitches.DEFAULT_COUNTRY_CODE_A T_INSTALL) 120 ? commandLine.getSwitchValue(BaseSwitches.DEFAULT_COUNTRY_CODE_A T_INSTALL)
70 : Locale.getDefault().getCountry(); 121 : Locale.getDefault().getCountry();
71 } 122 }
72 123
73 } 124 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698