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

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: Add ctime include. 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 "region_data_constants.h" 25 #include "region_data_constants.h"
26 #include "rule.h" 26 #include "rule.h"
27 #include "util/string_util.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) {
35 switch (field) { 36 switch (field) {
36 case COUNTRY: 37 case COUNTRY:
(...skipping 21 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 // If latinized rules are available and the |language_code| of this address is
70 // not the primary language code for the region, then use the latinized
71 // formatting rules.
72 const std::vector<std::vector<FormatElement> >& format =
73 rule.GetLatinFormat().empty() ||
74 language_code.empty() ||
75 NormalizeLanguageCode(language_code) ==
76 NormalizeLanguageCode(rule.GetLanguage())
77 ? rule.GetFormat() : rule.GetLatinFormat();
78
69 for (size_t i = 0; i < format.size(); ++i) { 79 for (size_t i = 0; i < format.size(); ++i) {
70 std::string line; 80 std::string line;
71 for (size_t j = 0; j < format[i].size(); ++j) { 81 for (size_t j = 0; j < format[i].size(); ++j) {
72 const FormatElement& element = format[i][j]; 82 const FormatElement& element = format[i][j];
73 if (element.IsField()) { 83 if (element.IsField()) {
74 if (element.field == STREET_ADDRESS) { 84 if (element.field == STREET_ADDRESS) {
75 // Street address field can contain multiple values. 85 // Street address field can contain multiple values.
76 for (size_t k = 0; k < address_lines.size(); ++k) { 86 for (size_t k = 0; k < address_lines.size(); ++k) {
77 line += address_lines[k]; 87 line += address_lines[k];
78 if (k < address_lines.size() - 1) { 88 if (k < address_lines.size() - 1) {
(...skipping 21 matching lines...) Expand all
100 } 110 }
101 111
102 void AddressData::SetFieldValue(AddressField field, const std::string& value) { 112 void AddressData::SetFieldValue(AddressField field, const std::string& value) {
103 std::string* field_value = 113 std::string* field_value =
104 const_cast<std::string*>(GetMemberForField(*this, field)); 114 const_cast<std::string*>(GetMemberForField(*this, field));
105 if (field_value != NULL) { 115 if (field_value != NULL) {
106 *field_value = value; 116 *field_value = value;
107 } 117 }
108 } 118 }
109 119
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 120 } // namespace addressinput
134 } // namespace i18n 121 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698