| 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_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" | |
| 28 | 27 |
| 29 namespace i18n { | 28 namespace i18n { |
| 30 namespace addressinput { | 29 namespace addressinput { |
| 31 | 30 |
| 32 namespace { | 31 namespace { |
| 33 | 32 |
| 34 const std::string* GetMemberForField(const AddressData& address, | 33 const std::string* GetMemberForField(const AddressData& address, |
| 35 AddressField field) { | 34 AddressField field) { |
| 36 switch (field) { | 35 switch (field) { |
| 37 case COUNTRY: | 36 case COUNTRY: |
| (...skipping 21 matching lines...) Expand all Loading... |
| 59 } // namespace | 58 } // namespace |
| 60 | 59 |
| 61 void AddressData::FormatForDisplay(std::vector<std::string>* lines) const { | 60 void AddressData::FormatForDisplay(std::vector<std::string>* lines) const { |
| 62 assert(lines != NULL); | 61 assert(lines != NULL); |
| 63 lines->clear(); | 62 lines->clear(); |
| 64 | 63 |
| 65 Rule rule; | 64 Rule rule; |
| 66 rule.CopyFrom(Rule::GetDefault()); | 65 rule.CopyFrom(Rule::GetDefault()); |
| 67 rule.ParseSerializedRule(RegionDataConstants::GetRegionData(country_code)); | 66 rule.ParseSerializedRule(RegionDataConstants::GetRegionData(country_code)); |
| 68 | 67 |
| 69 // If latinized rules are available and the |language_code| of this address is | 68 const std::vector<std::vector<FormatElement> >& format = rule.GetFormat(); |
| 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 | |
| 79 for (size_t i = 0; i < format.size(); ++i) { | 69 for (size_t i = 0; i < format.size(); ++i) { |
| 80 std::string line; | 70 std::string line; |
| 81 for (size_t j = 0; j < format[i].size(); ++j) { | 71 for (size_t j = 0; j < format[i].size(); ++j) { |
| 82 const FormatElement& element = format[i][j]; | 72 const FormatElement& element = format[i][j]; |
| 83 if (element.IsField()) { | 73 if (element.IsField()) { |
| 84 if (element.field == STREET_ADDRESS) { | 74 if (element.field == STREET_ADDRESS) { |
| 85 // Street address field can contain multiple values. | 75 // Street address field can contain multiple values. |
| 86 for (size_t k = 0; k < address_lines.size(); ++k) { | 76 for (size_t k = 0; k < address_lines.size(); ++k) { |
| 87 line += address_lines[k]; | 77 line += address_lines[k]; |
| 88 if (k < address_lines.size() - 1) { | 78 if (k < address_lines.size() - 1) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 110 } | 100 } |
| 111 | 101 |
| 112 void AddressData::SetFieldValue(AddressField field, const std::string& value) { | 102 void AddressData::SetFieldValue(AddressField field, const std::string& value) { |
| 113 std::string* field_value = | 103 std::string* field_value = |
| 114 const_cast<std::string*>(GetMemberForField(*this, field)); | 104 const_cast<std::string*>(GetMemberForField(*this, field)); |
| 115 if (field_value != NULL) { | 105 if (field_value != NULL) { |
| 116 *field_value = value; | 106 *field_value = value; |
| 117 } | 107 } |
| 118 } | 108 } |
| 119 | 109 |
| 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 |
| 120 } // namespace addressinput | 133 } // namespace addressinput |
| 121 } // namespace i18n | 134 } // namespace i18n |
| OLD | NEW |