OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 #include "chrome/browser/autofill/autofill_country.h" |
| 6 |
| 7 #include <map> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/singleton.h" |
| 11 #include "base/string_util.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/browser_process.h" |
| 14 #include "grit/generated_resources.h" |
| 15 #include "third_party/icu/public/common/unicode/locid.h" |
| 16 #include "third_party/icu/public/common/unicode/uloc.h" |
| 17 #include "third_party/icu/public/i18n/unicode/coll.h" |
| 18 #include "third_party/icu/public/i18n/unicode/ucol.h" |
| 19 #include "ui/base/l10n/l10n_util_collator.h" |
| 20 #include "ui/base/l10n/l10n_util.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 struct AutoFillCountryData { |
| 25 std::string country_code; |
| 26 int postal_code_label_id; |
| 27 int state_label_id; |
| 28 }; |
| 29 |
| 30 // The maximum capacity needed to store a locale up to the country code. |
| 31 const size_t kLocaleCapacity = |
| 32 ULOC_LANG_CAPACITY + ULOC_SCRIPT_CAPACITY + ULOC_COUNTRY_CAPACITY + 1; |
| 33 |
| 34 // Maps country codes to localized label string identifiers. |
| 35 const AutoFillCountryData kCountryData[] = { |
| 36 {"AD", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PARISH}, |
| 37 {"AE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_EMIRATE}, |
| 38 {"AF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 39 {"AG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 40 {"AI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 41 {"AL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 42 {"AM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 43 {"AN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 44 {"AO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 45 {"AQ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 46 {"AR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 47 {"AS", IDS_AUTOFILL_DIALOG_ZIP_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 48 {"AT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 49 {"AU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 50 {"AW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 51 {"AX", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 52 {"AZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 53 {"BA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 54 {"BB", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PARISH}, |
| 55 {"BD", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 56 {"BE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 57 {"BF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 58 {"BG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 59 {"BH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 60 {"BI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 61 {"BJ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 62 {"BL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 63 {"BM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 64 {"BN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 65 {"BO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 66 {"BR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 67 {"BS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND}, |
| 68 {"BT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 69 {"BV", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 70 {"BW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 71 {"BY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 72 {"BZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 73 {"CA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 74 {"CC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 75 {"CD", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 76 {"CF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 77 {"CG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 78 {"CH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 79 {"CI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 80 {"CK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 81 {"CL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 82 {"CM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 83 {"CN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 84 {"CO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 85 {"CR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 86 {"CV", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND}, |
| 87 {"CX", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 88 {"CY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 89 {"CZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 90 {"DE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 91 {"DJ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 92 {"DK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 93 {"DM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 94 {"DO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 95 {"DZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 96 {"EC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 97 {"EE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 98 {"EG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 99 {"EH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 100 {"ER", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 101 {"ES", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 102 {"ET", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 103 {"FI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 104 {"FJ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 105 {"FK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 106 {"FM", IDS_AUTOFILL_DIALOG_ZIP_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 107 {"FO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 108 {"FR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 109 {"GA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 110 {"GB", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_COUNTY}, |
| 111 {"GD", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 112 {"GE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 113 {"GF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 114 {"GG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 115 {"GH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 116 {"GI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 117 {"GL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 118 {"GM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 119 {"GN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 120 {"GP", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 121 {"GQ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 122 {"GR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 123 {"GS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 124 {"GT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 125 {"GU", IDS_AUTOFILL_DIALOG_ZIP_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 126 {"GW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 127 {"GY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 128 {"HK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_AREA}, |
| 129 {"HM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 130 {"HN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 131 {"HR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 132 {"HT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 133 {"HU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 134 {"ID", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 135 {"IE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_COUNTY}, |
| 136 {"IL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 137 {"IM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 138 {"IN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 139 {"IO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 140 {"IQ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 141 {"IS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 142 {"IT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 143 {"JE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 144 {"JM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PARISH}, |
| 145 {"JO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 146 {"JP", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PREFECTURE}, |
| 147 {"KE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 148 {"KG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 149 {"KH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 150 {"KI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND}, |
| 151 {"KM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 152 {"KN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND}, |
| 153 {"KP", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 154 {"KR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 155 {"KW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 156 {"KY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND}, |
| 157 {"KZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 158 {"LA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 159 {"LB", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 160 {"LC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 161 {"LI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 162 {"LK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 163 {"LR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 164 {"LS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 165 {"LT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 166 {"LU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 167 {"LV", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 168 {"LY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 169 {"MA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 170 {"MC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 171 {"MD", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 172 {"ME", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 173 {"MF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 174 {"MG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 175 {"MH", IDS_AUTOFILL_DIALOG_ZIP_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 176 {"MK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 177 {"ML", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 178 {"MN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 179 {"MO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 180 {"MP", IDS_AUTOFILL_DIALOG_ZIP_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 181 {"MQ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 182 {"MR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 183 {"MS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 184 {"MT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 185 {"MU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 186 {"MV", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 187 {"MW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 188 {"MX", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 189 {"MY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 190 {"MZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 191 {"NA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 192 {"NC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 193 {"NE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 194 {"NF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 195 {"NG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 196 {"NI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_DEPARTMENT}, |
| 197 {"NL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 198 {"NO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 199 {"NP", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 200 {"NR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_DISTRICT}, |
| 201 {"NU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 202 {"NZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 203 {"OM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 204 {"PA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 205 {"PE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 206 {"PF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND}, |
| 207 {"PG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 208 {"PH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 209 {"PK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 210 {"PL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 211 {"PM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 212 {"PN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 213 {"PR", IDS_AUTOFILL_DIALOG_ZIP_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 214 {"PS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 215 {"PT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 216 {"PW", IDS_AUTOFILL_DIALOG_ZIP_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 217 {"PY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 218 {"QA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 219 {"RE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 220 {"RO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 221 {"RS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 222 {"RU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 223 {"RW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 224 {"SA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 225 {"SB", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 226 {"SC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND}, |
| 227 {"SE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 228 {"SG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 229 {"SH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 230 {"SI", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 231 {"SJ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 232 {"SK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 233 {"SL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 234 {"SM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 235 {"SN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 236 {"SO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 237 {"SR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 238 {"ST", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 239 {"SV", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 240 {"SZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 241 {"TC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 242 {"TD", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 243 {"TF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 244 {"TG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 245 {"TH", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 246 {"TJ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 247 {"TK", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 248 {"TL", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 249 {"TM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 250 {"TN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 251 {"TO", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 252 {"TR", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 253 {"TT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 254 {"TV", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_ISLAND}, |
| 255 {"TW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_COUNTY}, |
| 256 {"TZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 257 {"UA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 258 {"UG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 259 {"UM", IDS_AUTOFILL_DIALOG_ZIP_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 260 {"US", IDS_AUTOFILL_DIALOG_ZIP_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 261 {"UY", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 262 {"UZ", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 263 {"VA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 264 {"VC", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 265 {"VE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 266 {"VG", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 267 {"VI", IDS_AUTOFILL_DIALOG_ZIP_CODE, IDS_AUTOFILL_DIALOG_STATE}, |
| 268 {"VN", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 269 {"VU", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 270 {"WF", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 271 {"WS", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 272 {"YE", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 273 {"YT", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 274 {"ZA", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 275 {"ZM", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 276 {"ZW", IDS_AUTOFILL_DIALOG_POSTAL_CODE, IDS_AUTOFILL_DIALOG_PROVINCE}, |
| 277 }; |
| 278 |
| 279 // A singleton class that encapsulates a map from country codes to country data. |
| 280 class AutoFillCountries { |
| 281 public: |
| 282 // Map type from country codes to country data. |
| 283 typedef std::map<std::string, AutoFillCountryData> MapType; |
| 284 |
| 285 static AutoFillCountries* GetInstance(); |
| 286 static const MapType& countries(); |
| 287 |
| 288 private: |
| 289 AutoFillCountries(); |
| 290 friend struct DefaultSingletonTraits<AutoFillCountries>; |
| 291 |
| 292 MapType countries_; |
| 293 |
| 294 DISALLOW_COPY_AND_ASSIGN(AutoFillCountries); |
| 295 }; |
| 296 |
| 297 // static |
| 298 AutoFillCountries* AutoFillCountries::GetInstance() { |
| 299 return Singleton<AutoFillCountries>::get(); |
| 300 } |
| 301 |
| 302 AutoFillCountries::AutoFillCountries() { |
| 303 // Add all the countries we have explicit data for. |
| 304 for (size_t i = 0; i < arraysize(kCountryData); ++i) { |
| 305 const AutoFillCountryData& data = kCountryData[i]; |
| 306 countries_.insert(std::make_pair(data.country_code, data)); |
| 307 } |
| 308 |
| 309 // Add any other countries that ICU knows about, falling back to default data |
| 310 // values. |
| 311 for (const char* const* country_pointer = Locale::getISOCountries(); |
| 312 *country_pointer; |
| 313 ++country_pointer) { |
| 314 std::string country_code = *country_pointer; |
| 315 if (!countries_.count(country_code)) { |
| 316 AutoFillCountryData data = { |
| 317 country_code, |
| 318 IDS_AUTOFILL_DIALOG_POSTAL_CODE, |
| 319 IDS_AUTOFILL_DIALOG_PROVINCE |
| 320 }; |
| 321 countries_.insert(std::make_pair(country_code, data)); |
| 322 } |
| 323 } |
| 324 } |
| 325 |
| 326 const AutoFillCountries::MapType& AutoFillCountries::countries() { |
| 327 return GetInstance()->countries_; |
| 328 } |
| 329 |
| 330 // Returns the country name corresponding to |country_code|, localized to the |
| 331 // |display_locale|. |
| 332 string16 GetDisplayName(const std::string& country_code, |
| 333 const icu::Locale& display_locale) { |
| 334 icu::Locale country_locale(NULL, country_code.c_str()); |
| 335 icu::UnicodeString name; |
| 336 country_locale.getDisplayName(display_locale, name); |
| 337 |
| 338 DCHECK_GT(name.length(), 0); |
| 339 return string16(name.getBuffer(), name.length()); |
| 340 } |
| 341 |
| 342 } // namespace |
| 343 |
| 344 AutoFillCountry::AutoFillCountry(const std::string& country_code, |
| 345 const std::string& locale) { |
| 346 const AutoFillCountries::MapType& countries = AutoFillCountries::countries(); |
| 347 DCHECK(countries.count(country_code)); |
| 348 const AutoFillCountryData& data = countries.find(country_code)->second; |
| 349 |
| 350 country_code_ = country_code; |
| 351 name_ = GetDisplayName(country_code, icu::Locale(locale.c_str())); |
| 352 postal_code_label_ = l10n_util::GetStringUTF16(data.postal_code_label_id); |
| 353 state_label_ = l10n_util::GetStringUTF16(data.state_label_id); |
| 354 } |
| 355 |
| 356 AutoFillCountry::~AutoFillCountry() { |
| 357 } |
| 358 |
| 359 // static |
| 360 void AutoFillCountry::GetAvailableCountries( |
| 361 std::vector<std::string>* country_codes) { |
| 362 DCHECK(country_codes); |
| 363 |
| 364 const AutoFillCountries::MapType& country_data = |
| 365 AutoFillCountries::countries(); |
| 366 for (AutoFillCountries::MapType::const_iterator it = country_data.begin(); |
| 367 it != country_data.end(); |
| 368 ++it) { |
| 369 country_codes->push_back(it->first); |
| 370 } |
| 371 } |
| 372 |
| 373 // static |
| 374 const std::string AutoFillCountry::CountryCodeForLocale( |
| 375 const std::string& locale) { |
| 376 // Add likely subtags to the locale. In particular, add any likely country |
| 377 // subtags -- e.g. for locales like "ru" that only include the language. |
| 378 std::string likely_locale; |
| 379 UErrorCode error_ignored = U_ZERO_ERROR; |
| 380 uloc_addLikelySubtags(locale.c_str(), |
| 381 WriteInto(&likely_locale, kLocaleCapacity), |
| 382 kLocaleCapacity, |
| 383 &error_ignored); |
| 384 |
| 385 // Extract the country code. |
| 386 std::string country_code = icu::Locale(likely_locale.c_str()).getCountry(); |
| 387 |
| 388 // Default to the United States if we have no better guess. |
| 389 return !country_code.empty() ? country_code : "US"; |
| 390 } |
| 391 |
| 392 // static |
| 393 const std::string AutoFillCountry::GetCountryCode( |
| 394 const string16& country, const std::string& locale) { |
| 395 // TODO(isherman): Add other synonyms |
| 396 if (country == ASCIIToUTF16("USA") || |
| 397 country == ASCIIToUTF16("United States of America")) |
| 398 return "US"; |
| 399 |
| 400 const AutoFillCountries::MapType& country_data = |
| 401 AutoFillCountries::countries(); |
| 402 |
| 403 // First, check to see if |country| is actually a country code, in which case |
| 404 // we can short-circuit a lot of the work. |
| 405 std::string country_utf8 = UTF16ToUTF8(StringToUpperASCII(country)); |
| 406 if (country_data.count(country_utf8)) |
| 407 return country_utf8; |
| 408 |
| 409 const icu::Locale icu_locale = icu::Locale(locale.c_str()); |
| 410 |
| 411 // Compare case-insensitively and ignoring punctuation. |
| 412 UErrorCode ignored = U_ZERO_ERROR; |
| 413 icu::Collator* collator = icu::Collator::createInstance(icu_locale, ignored); |
| 414 collator->setStrength(icu::Collator::SECONDARY); |
| 415 ignored = U_ZERO_ERROR; |
| 416 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored); |
| 417 |
| 418 for (AutoFillCountries::MapType::const_iterator it = country_data.begin(); |
| 419 it != country_data.end(); |
| 420 ++it) { |
| 421 std::string country_code = it->first; |
| 422 |
| 423 string16 name = GetDisplayName(country_code, icu_locale); |
| 424 if (l10n_util::CompareString16WithCollator(collator, |
| 425 country, |
| 426 name) == UCOL_EQUAL) { |
| 427 return country_code; |
| 428 } |
| 429 } |
| 430 |
| 431 // As a fallback, try assuming the country named is localized to US English. |
| 432 if (locale != "en_US") |
| 433 return GetCountryCode(country, "en_US"); |
| 434 |
| 435 return std::string(); |
| 436 } |
| 437 |
| 438 // static |
| 439 const std::string AutoFillCountry::ApplicationLocale() { |
| 440 return g_browser_process->GetApplicationLocale(); |
| 441 } |
| 442 |
| 443 AutoFillCountry::AutoFillCountry(const std::string& country_code, |
| 444 const string16& name, |
| 445 const string16& postal_code_label, |
| 446 const string16& state_label) |
| 447 : country_code_(country_code), |
| 448 name_(name), |
| 449 postal_code_label_(postal_code_label), |
| 450 state_label_(state_label) { |
| 451 } |
OLD | NEW |