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

Side by Side Diff: third_party/libaddressinput/chromium/cpp/src/address_ui.cc

Issue 389863002: Remove Chrome's own version of libaddressinput in favor of the upstream. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (C) 2013 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <libaddressinput/address_ui.h>
16
17 #include <libaddressinput/address_field.h>
18 #include <libaddressinput/address_ui_component.h>
19
20 #include <algorithm>
21 #include <cassert>
22 #include <cstddef>
23 #include <set>
24 #include <string>
25 #include <vector>
26
27 #include "grit.h"
28 #include "grit/libaddressinput_strings.h"
29 #include "region_data_constants.h"
30 #include "rule.h"
31 #include "util/string_util.h"
32
33 namespace i18n {
34 namespace addressinput {
35
36 namespace {
37
38 int GetMessageIdForField(AddressField field,
39 const std::string& admin_area_name_type,
40 const std::string& postal_code_name_type) {
41 switch (field) {
42 case COUNTRY:
43 return IDS_LIBADDRESSINPUT_I18N_COUNTRY_LABEL;
44 case LOCALITY:
45 return IDS_LIBADDRESSINPUT_I18N_LOCALITY_LABEL;
46 case DEPENDENT_LOCALITY:
47 return IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL;
48 case SORTING_CODE:
49 return IDS_LIBADDRESSINPUT_I18N_CEDEX_LABEL;
50 case STREET_ADDRESS:
51 return IDS_LIBADDRESSINPUT_I18N_ADDRESS_LINE1_LABEL;
52 case RECIPIENT:
53 return IDS_LIBADDRESSINPUT_I18N_RECIPIENT_LABEL;
54
55 case ADMIN_AREA:
56 if (admin_area_name_type == "area") {
57 return IDS_LIBADDRESSINPUT_I18N_AREA;
58 }
59 if (admin_area_name_type == "county") {
60 return IDS_LIBADDRESSINPUT_I18N_COUNTY_LABEL;
61 }
62 if (admin_area_name_type == "department") {
63 return IDS_LIBADDRESSINPUT_I18N_DEPARTMENT;
64 }
65 if (admin_area_name_type == "district") {
66 return IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL;
67 }
68 if (admin_area_name_type == "do_si") {
69 return IDS_LIBADDRESSINPUT_I18N_DO_SI;
70 }
71 if (admin_area_name_type == "emirate") {
72 return IDS_LIBADDRESSINPUT_I18N_EMIRATE;
73 }
74 if (admin_area_name_type == "island") {
75 return IDS_LIBADDRESSINPUT_I18N_ISLAND;
76 }
77 if (admin_area_name_type == "parish") {
78 return IDS_LIBADDRESSINPUT_I18N_PARISH;
79 }
80 if (admin_area_name_type == "prefecture") {
81 return IDS_LIBADDRESSINPUT_I18N_PREFECTURE;
82 }
83 if (admin_area_name_type == "province") {
84 return IDS_LIBADDRESSINPUT_I18N_PROVINCE;
85 }
86 if (admin_area_name_type == "state") {
87 return IDS_LIBADDRESSINPUT_I18N_STATE_LABEL;
88 }
89 break;
90
91 case POSTAL_CODE:
92 if (postal_code_name_type == "postal") {
93 return IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL;
94 }
95 if (postal_code_name_type == "zip") {
96 return IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL;
97 }
98 break;
99 }
100
101 return INVALID_MESSAGE_ID;
102 }
103
104 // Returns the BCP 47 language code that should be used to format the address
105 // that the user entered.
106 //
107 // If the rule does not contain information about languages, then returns the
108 // UI language.
109 //
110 // If the UI language is either the default language for the country, one of the
111 // languages for rules, or one of the languages for address input, then returns
112 // this UI language. If there're no matches, then picks one of the languages in
113 // the rule and returns it.
114 //
115 // If latinized rules are available and the UI language code is not the primary
116 // language code for this region, then returns the primary language with "-latn"
117 // appended.
118 std::string GetComponentsLanguageCode(const Rule& rule,
119 const std::string& ui_language_code) {
120 // Select the default language code for the region.
121 std::string default_language_code;
122 if (!rule.GetLanguage().empty()) {
123 default_language_code = rule.GetLanguage();
124 } else if (!rule.GetLanguages().empty()) {
125 default_language_code = rule.GetLanguages()[0];
126 } else if (!rule.GetInputLanguages().empty()) {
127 default_language_code = rule.GetInputLanguages()[0];
128 } else {
129 // Region does not have any language information (e.g. Antarctica). Use the
130 // UI language code as is.
131 return ui_language_code;
132 }
133
134 // If the UI language code is not set, then use default language code.
135 if (ui_language_code.empty()) {
136 return default_language_code;
137 }
138
139 const std::string& normalized_ui_language_code =
140 NormalizeLanguageCode(ui_language_code);
141 const std::string& normalized_default_language_code =
142 NormalizeLanguageCode(default_language_code);
143
144 // Check whether UI language code matches any language codes in the rule,
145 // normalized or as is.
146 if (normalized_default_language_code == normalized_ui_language_code ||
147 std::find(
148 rule.GetLanguages().begin(),
149 rule.GetLanguages().end(),
150 ui_language_code) != rule.GetLanguages().end() ||
151 std::find(
152 rule.GetLanguages().begin(),
153 rule.GetLanguages().end(),
154 normalized_ui_language_code) != rule.GetLanguages().end() ||
155 std::find(
156 rule.GetInputLanguages().begin(),
157 rule.GetInputLanguages().end(),
158 ui_language_code) != rule.GetInputLanguages().end() ||
159 std::find(
160 rule.GetInputLanguages().begin(),
161 rule.GetInputLanguages().end(),
162 normalized_ui_language_code) != rule.GetInputLanguages().end()) {
163 return ui_language_code;
164 }
165
166 // The UI language code does not match any language information in the rule.
167 return rule.GetLatinFormat().empty()
168 ? default_language_code
169 : normalized_default_language_code + "-latn";
170 }
171
172 } // namespace
173
174 const std::vector<std::string>& GetRegionCodes() {
175 return RegionDataConstants::GetRegionCodes();
176 }
177
178 std::vector<AddressUiComponent> BuildComponents(
179 const std::string& region_code,
180 const std::string& ui_language_code,
181 std::string* components_language_code) {
182 std::vector<AddressUiComponent> result;
183
184 Rule rule;
185 rule.CopyFrom(Rule::GetDefault());
186 if (!rule.ParseSerializedRule(
187 RegionDataConstants::GetRegionData(region_code))) {
188 return result;
189 }
190
191 if (components_language_code != NULL) {
192 *components_language_code =
193 GetComponentsLanguageCode(rule, ui_language_code);
194 }
195
196 // For avoiding showing an input field twice, when the field is displayed
197 // twice on an envelope.
198 std::set<AddressField> fields;
199
200 // If latinized rules are available and the |ui_language_code| is not the
201 // primary language code for the region, then use the latinized formatting
202 // rules.
203 const std::vector<std::vector<FormatElement> >& format =
204 rule.GetLatinFormat().empty() ||
205 ui_language_code.empty() ||
206 NormalizeLanguageCode(ui_language_code) ==
207 NormalizeLanguageCode(rule.GetLanguage())
208 ? rule.GetFormat() : rule.GetLatinFormat();
209
210 for (std::vector<std::vector<FormatElement> >::const_iterator
211 line_it = format.begin();
212 line_it != format.end();
213 ++line_it) {
214 int num_fields_this_row = 0;
215 for (std::vector<FormatElement>::const_iterator element_it =
216 line_it->begin();
217 element_it != line_it->end();
218 ++element_it) {
219 if (element_it->IsField()) {
220 ++num_fields_this_row;
221 }
222 }
223
224 for (std::vector<FormatElement>::const_iterator element_it =
225 line_it->begin();
226 element_it != line_it->end();
227 ++element_it) {
228 AddressField field = element_it->field;
229 if (!element_it->IsField() || fields.find(field) != fields.end()) {
230 continue;
231 }
232 fields.insert(field);
233
234 AddressUiComponent component;
235 component.length_hint =
236 num_fields_this_row == 1 ? AddressUiComponent::HINT_LONG
237 : AddressUiComponent::HINT_SHORT;
238 component.field = field;
239 component.name_id =
240 GetMessageIdForField(field,
241 rule.GetAdminAreaNameType(),
242 rule.GetPostalCodeNameType());
243 result.push_back(component);
244 }
245 }
246
247 return result;
248 }
249
250 const std::string& GetCompactAddressLinesSeparator(
251 const std::string& language_code) {
252 return RegionDataConstants::GetLanguageCompactLineSeparator(language_code);
253 }
254
255 } // namespace addressinput
256 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698