OLD | NEW |
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_ui.h> | 15 #include <libaddressinput/address_ui.h> |
16 | 16 |
17 #include <libaddressinput/address_field.h> | 17 #include <libaddressinput/address_field.h> |
18 #include <libaddressinput/address_ui_component.h> | 18 #include <libaddressinput/address_ui_component.h> |
19 | 19 |
| 20 #include <algorithm> |
| 21 #include <set> |
20 #include <string> | 22 #include <string> |
21 #include <vector> | 23 #include <vector> |
22 | 24 |
23 #include "grit.h" | 25 #include "grit.h" |
24 #include "grit/libaddressinput_strings.h" | 26 #include "grit/libaddressinput_strings.h" |
25 #include "region_data_constants.h" | 27 #include "region_data_constants.h" |
26 #include "rule.h" | 28 #include "rule.h" |
27 | 29 |
28 namespace i18n { | 30 namespace i18n { |
29 namespace addressinput { | 31 namespace addressinput { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 const std::string& region_code) { | 69 const std::string& region_code) { |
68 std::vector<AddressUiComponent> result; | 70 std::vector<AddressUiComponent> result; |
69 | 71 |
70 Rule rule; | 72 Rule rule; |
71 rule.CopyFrom(Rule::GetDefault()); | 73 rule.CopyFrom(Rule::GetDefault()); |
72 if (!rule.ParseSerializedRule( | 74 if (!rule.ParseSerializedRule( |
73 RegionDataConstants::GetRegionData(region_code))) { | 75 RegionDataConstants::GetRegionData(region_code))) { |
74 return result; | 76 return result; |
75 } | 77 } |
76 | 78 |
77 for (std::vector<std::vector<AddressField> >::const_iterator | 79 // For avoiding showing an input field twice, when the field is displayed |
78 line_it = rule.GetFormat().begin(); | 80 // twice on an envelope. |
| 81 std::set<AddressField> fields; |
| 82 |
| 83 for (std::vector<std::vector<FormatElement> >::const_iterator |
| 84 line_it = rule.GetFormat().begin(); |
79 line_it != rule.GetFormat().end(); | 85 line_it != rule.GetFormat().end(); |
80 ++line_it) { | 86 ++line_it) { |
81 for (std::vector<AddressField>::const_iterator field_it = line_it->begin(); | 87 int num_fields_this_row = 0; |
82 field_it != line_it->end(); ++field_it) { | 88 for (std::vector<FormatElement>::const_iterator element_it = |
| 89 line_it->begin(); |
| 90 element_it != line_it->end(); |
| 91 ++element_it) { |
| 92 if (element_it->IsField()) { |
| 93 ++num_fields_this_row; |
| 94 } |
| 95 } |
| 96 |
| 97 for (std::vector<FormatElement>::const_iterator element_it = |
| 98 line_it->begin(); |
| 99 element_it != line_it->end(); |
| 100 ++element_it) { |
| 101 AddressField field = element_it->field; |
| 102 if (!element_it->IsField() || fields.find(field) != fields.end()) { |
| 103 continue; |
| 104 } |
| 105 fields.insert(field); |
| 106 |
83 AddressUiComponent component; | 107 AddressUiComponent component; |
84 component.length_hint = | 108 component.length_hint = |
85 line_it->size() == 1 ? AddressUiComponent::HINT_LONG | 109 num_fields_this_row == 1 ? AddressUiComponent::HINT_LONG |
86 : AddressUiComponent::HINT_SHORT; | 110 : AddressUiComponent::HINT_SHORT; |
87 component.field = *field_it; | 111 component.field = field; |
88 component.name_id = | 112 component.name_id = |
89 GetMessageIdForField(*field_it, | 113 GetMessageIdForField(field, |
90 rule.GetAdminAreaNameMessageId(), | 114 rule.GetAdminAreaNameMessageId(), |
91 rule.GetPostalCodeNameMessageId()); | 115 rule.GetPostalCodeNameMessageId()); |
92 result.push_back(component); | 116 result.push_back(component); |
93 } | 117 } |
94 } | 118 } |
95 | 119 |
96 return result; | 120 return result; |
97 } | 121 } |
98 | 122 |
| 123 const std::string& GetCompactAddressLinesSeparator( |
| 124 const std::string& language_code, |
| 125 const std::string& country_code) { |
| 126 Rule rule; |
| 127 rule.CopyFrom(Rule::GetDefault()); |
| 128 if (!rule.ParseSerializedRule( |
| 129 RegionDataConstants::GetRegionData(country_code))) { |
| 130 return RegionDataConstants::GetLanguageCompactLineSeparator(language_code); |
| 131 } |
| 132 |
| 133 std::vector<std::string>::const_iterator lang_it = |
| 134 std::find(rule.GetLanguages().begin(), |
| 135 rule.GetLanguages().end(), |
| 136 language_code); |
| 137 if (lang_it != rule.GetLanguages().end()) { |
| 138 return RegionDataConstants::GetLanguageCompactLineSeparator(*lang_it); |
| 139 } |
| 140 |
| 141 if (!rule.GetLanguage().empty()) { |
| 142 return RegionDataConstants::GetLanguageCompactLineSeparator( |
| 143 rule.GetLanguage()); |
| 144 } |
| 145 |
| 146 return RegionDataConstants::GetCountryCompactLineSeparator(country_code); |
| 147 } |
| 148 |
99 } // namespace addressinput | 149 } // namespace addressinput |
100 } // namespace i18n | 150 } // namespace i18n |
OLD | NEW |