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