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

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

Issue 208243005: Determine language code and type of format for address. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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
1 // Copyright (C) 2013 Google Inc. 1 // Copyright (C) 2013 Google Inc.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with 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 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include <libaddressinput/address_data.h> 15 #include <libaddressinput/address_data.h>
16 16
17 #include <libaddressinput/address_field.h> 17 #include <libaddressinput/address_field.h>
18 18
19 #include <algorithm> 19 #include <algorithm>
20 #include <cassert> 20 #include <cassert>
21 #include <cstddef> 21 #include <cstddef>
22 #include <string> 22 #include <string>
23 #include <vector> 23 #include <vector>
24 24
25 #include "normalize_language_code.h"
25 #include "region_data_constants.h" 26 #include "region_data_constants.h"
26 #include "rule.h" 27 #include "rule.h"
27 28
28 namespace i18n { 29 namespace i18n {
29 namespace addressinput { 30 namespace addressinput {
30 31
31 namespace { 32 namespace {
32 33
33 const std::string* GetMemberForField(const AddressData& address, 34 const std::string* GetMemberForField(const AddressData& address,
34 AddressField field) { 35 AddressField field) {
(...skipping 23 matching lines...) Expand all
58 } // namespace 59 } // namespace
59 60
60 void AddressData::FormatForDisplay(std::vector<std::string>* lines) const { 61 void AddressData::FormatForDisplay(std::vector<std::string>* lines) const {
61 assert(lines != NULL); 62 assert(lines != NULL);
62 lines->clear(); 63 lines->clear();
63 64
64 Rule rule; 65 Rule rule;
65 rule.CopyFrom(Rule::GetDefault()); 66 rule.CopyFrom(Rule::GetDefault());
66 rule.ParseSerializedRule(RegionDataConstants::GetRegionData(country_code)); 67 rule.ParseSerializedRule(RegionDataConstants::GetRegionData(country_code));
67 68
68 const std::vector<std::vector<FormatElement> >& format = rule.GetFormat(); 69 std::string norm_address_lang = NormalizeLanguageCode(language_code);
70 std::string norm_rule_lang = NormalizeLanguageCode(rule.GetLanguage());
Evan Stade 2014/03/21 23:44:54 nit: I would delete the norm_ part of each of thes
please use gerrit instead 2014/03/22 01:34:30 Done.
71
72 const std::vector<std::vector<FormatElement> >& latin_format =
73 rule.GetLatinFormat();
74 const std::vector<std::vector<FormatElement> >& format =
75 norm_address_lang != norm_rule_lang && !latin_format.empty()
76 ? latin_format : rule.GetFormat();
77
69 for (size_t i = 0; i < format.size(); ++i) { 78 for (size_t i = 0; i < format.size(); ++i) {
70 std::string line; 79 std::string line;
71 for (size_t j = 0; j < format[i].size(); ++j) { 80 for (size_t j = 0; j < format[i].size(); ++j) {
72 const FormatElement& element = format[i][j]; 81 const FormatElement& element = format[i][j];
73 if (element.IsField()) { 82 if (element.IsField()) {
74 if (element.field == STREET_ADDRESS) { 83 if (element.field == STREET_ADDRESS) {
75 // Street address field can contain multiple values. 84 // Street address field can contain multiple values.
76 for (size_t k = 0; k < address_lines.size(); ++k) { 85 for (size_t k = 0; k < address_lines.size(); ++k) {
77 line += address_lines[k]; 86 line += address_lines[k];
78 if (k < address_lines.size() - 1) { 87 if (k < address_lines.size() - 1) {
(...skipping 21 matching lines...) Expand all
100 } 109 }
101 110
102 void AddressData::SetFieldValue(AddressField field, const std::string& value) { 111 void AddressData::SetFieldValue(AddressField field, const std::string& value) {
103 std::string* field_value = 112 std::string* field_value =
104 const_cast<std::string*>(GetMemberForField(*this, field)); 113 const_cast<std::string*>(GetMemberForField(*this, field));
105 if (field_value != NULL) { 114 if (field_value != NULL) {
106 *field_value = value; 115 *field_value = value;
107 } 116 }
108 } 117 }
109 118
110 const std::string& AddressData::GuessLanguageCode() const {
111 Rule rule;
112 rule.CopyFrom(Rule::GetDefault());
113 if (!rule.ParseSerializedRule(
114 RegionDataConstants::GetRegionData(country_code))) {
115 return language_code;
116 }
117
118 std::vector<std::string>::const_iterator lang_it =
119 std::find(rule.GetLanguages().begin(),
120 rule.GetLanguages().end(),
121 language_code);
122 if (lang_it != rule.GetLanguages().end()) {
123 return *lang_it;
124 }
125
126 if (!rule.GetLanguage().empty()) {
127 return rule.GetLanguage();
128 }
129
130 return language_code;
131 }
132
133 } // namespace addressinput 119 } // namespace addressinput
134 } // namespace i18n 120 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698