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

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

Issue 298863012: Use upstream libaddressinput in Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Initial. Created 6 years, 7 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 ORGANIZATION:
53 return IDS_LIBADDRESSINPUT_I18N_ORGANIZATION_LABEL;
54 case RECIPIENT:
55 return IDS_LIBADDRESSINPUT_I18N_RECIPIENT_LABEL;
56
57 case ADMIN_AREA:
58 if (admin_area_name_type == "area") {
59 return IDS_LIBADDRESSINPUT_I18N_AREA;
60 }
61 if (admin_area_name_type == "county") {
62 return IDS_LIBADDRESSINPUT_I18N_COUNTY_LABEL;
63 }
64 if (admin_area_name_type == "department") {
65 return IDS_LIBADDRESSINPUT_I18N_DEPARTMENT;
66 }
67 if (admin_area_name_type == "district") {
68 return IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL;
69 }
70 if (admin_area_name_type == "do_si") {
71 return IDS_LIBADDRESSINPUT_I18N_DO_SI;
72 }
73 if (admin_area_name_type == "emirate") {
74 return IDS_LIBADDRESSINPUT_I18N_EMIRATE;
75 }
76 if (admin_area_name_type == "island") {
77 return IDS_LIBADDRESSINPUT_I18N_ISLAND;
78 }
79 if (admin_area_name_type == "parish") {
80 return IDS_LIBADDRESSINPUT_I18N_PARISH;
81 }
82 if (admin_area_name_type == "prefecture") {
83 return IDS_LIBADDRESSINPUT_I18N_PREFECTURE;
84 }
85 if (admin_area_name_type == "province") {
86 return IDS_LIBADDRESSINPUT_I18N_PROVINCE;
87 }
88 if (admin_area_name_type == "state") {
89 return IDS_LIBADDRESSINPUT_I18N_STATE_LABEL;
90 }
91 break;
92
93 case POSTAL_CODE:
94 if (postal_code_name_type == "postal") {
95 return IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL;
96 }
97 if (postal_code_name_type == "zip") {
98 return IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL;
99 }
100 break;
101 }
102
103 return INVALID_MESSAGE_ID;
104 }
105
106 // Returns the BCP 47 language code that should be used to format the address
107 // that the user entered.
108 //
109 // If the rule does not contain information about languages, then returns the
110 // UI language.
111 //
112 // If the UI language is either the default language for the country, one of the
113 // languages for rules, or one of the languages for address input, then returns
114 // this UI language. If there're no matches, then picks one of the languages in
115 // the rule and returns it.
116 //
117 // If latinized rules are available and the UI language code is not the primary
118 // language code for this region, then returns the primary language with "-latn"
119 // appended.
120 std::string GetComponentsLanguageCode(const Rule& rule,
121 const std::string& ui_language_code) {
122 // Select the default language code for the region.
123 std::string default_language_code;
124 if (!rule.GetLanguage().empty()) {
125 default_language_code = rule.GetLanguage();
126 } else if (!rule.GetLanguages().empty()) {
127 default_language_code = rule.GetLanguages()[0];
128 } else if (!rule.GetInputLanguages().empty()) {
129 default_language_code = rule.GetInputLanguages()[0];
130 } else {
131 // Region does not have any language information (e.g. Antarctica). Use the
132 // UI language code as is.
133 return ui_language_code;
134 }
135
136 // If the UI language code is not set, then use default language code.
137 if (ui_language_code.empty()) {
138 return default_language_code;
139 }
140
141 const std::string& normalized_ui_language_code =
142 NormalizeLanguageCode(ui_language_code);
143 const std::string& normalized_default_language_code =
144 NormalizeLanguageCode(default_language_code);
145
146 // Check whether UI language code matches any language codes in the rule,
147 // normalized or as is.
148 if (normalized_default_language_code == normalized_ui_language_code ||
149 std::find(
150 rule.GetLanguages().begin(),
151 rule.GetLanguages().end(),
152 ui_language_code) != rule.GetLanguages().end() ||
153 std::find(
154 rule.GetLanguages().begin(),
155 rule.GetLanguages().end(),
156 normalized_ui_language_code) != rule.GetLanguages().end() ||
157 std::find(
158 rule.GetInputLanguages().begin(),
159 rule.GetInputLanguages().end(),
160 ui_language_code) != rule.GetInputLanguages().end() ||
161 std::find(
162 rule.GetInputLanguages().begin(),
163 rule.GetInputLanguages().end(),
164 normalized_ui_language_code) != rule.GetInputLanguages().end()) {
165 return ui_language_code;
166 }
167
168 // The UI language code does not match any language information in the rule.
169 return rule.GetLatinFormat().empty()
170 ? default_language_code
171 : normalized_default_language_code + "-latn";
172 }
173
174 } // namespace
175
176 const std::vector<std::string>& GetRegionCodes() {
177 return RegionDataConstants::GetRegionCodes();
178 }
179
180 std::vector<AddressUiComponent> BuildComponents(
181 const std::string& region_code,
182 const std::string& ui_language_code,
183 std::string* components_language_code) {
184 std::vector<AddressUiComponent> result;
185
186 Rule rule;
187 rule.CopyFrom(Rule::GetDefault());
188 if (!rule.ParseSerializedRule(
189 RegionDataConstants::GetRegionData(region_code))) {
190 return result;
191 }
192
193 if (components_language_code != NULL) {
194 *components_language_code =
195 GetComponentsLanguageCode(rule, ui_language_code);
196 }
197
198 // For avoiding showing an input field twice, when the field is displayed
199 // twice on an envelope.
200 std::set<AddressField> fields;
201
202 // If latinized rules are available and the |ui_language_code| is not the
203 // primary language code for the region, then use the latinized formatting
204 // rules.
205 const std::vector<std::vector<FormatElement> >& format =
206 rule.GetLatinFormat().empty() ||
207 ui_language_code.empty() ||
208 NormalizeLanguageCode(ui_language_code) ==
209 NormalizeLanguageCode(rule.GetLanguage())
210 ? rule.GetFormat() : rule.GetLatinFormat();
211
212 for (std::vector<std::vector<FormatElement> >::const_iterator
213 line_it = format.begin();
214 line_it != format.end();
215 ++line_it) {
216 int num_fields_this_row = 0;
217 for (std::vector<FormatElement>::const_iterator element_it =
218 line_it->begin();
219 element_it != line_it->end();
220 ++element_it) {
221 if (element_it->IsField()) {
222 ++num_fields_this_row;
223 }
224 }
225
226 for (std::vector<FormatElement>::const_iterator element_it =
227 line_it->begin();
228 element_it != line_it->end();
229 ++element_it) {
230 AddressField field = element_it->field;
231 if (!element_it->IsField() || fields.find(field) != fields.end()) {
232 continue;
233 }
234 fields.insert(field);
235
236 AddressUiComponent component;
237 component.length_hint =
238 num_fields_this_row == 1 ? AddressUiComponent::HINT_LONG
239 : AddressUiComponent::HINT_SHORT;
240 component.field = field;
241 component.name_id =
242 GetMessageIdForField(field,
243 rule.GetAdminAreaNameType(),
244 rule.GetPostalCodeNameType());
245 result.push_back(component);
246 }
247 }
248
249 return result;
250 }
251
252 const std::string& GetCompactAddressLinesSeparator(
253 const std::string& language_code) {
254 return RegionDataConstants::GetLanguageCompactLineSeparator(language_code);
255 }
256
257 } // namespace addressinput
258 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698