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 <cassert> |
| 22 #include <cstddef> |
20 #include <set> | 23 #include <set> |
21 #include <string> | 24 #include <string> |
22 #include <vector> | 25 #include <vector> |
23 | 26 |
24 #include "grit.h" | 27 #include "grit.h" |
25 #include "grit/libaddressinput_strings.h" | 28 #include "grit/libaddressinput_strings.h" |
26 #include "region_data_constants.h" | 29 #include "region_data_constants.h" |
27 #include "rule.h" | 30 #include "rule.h" |
| 31 #include "util/string_util.h" |
28 | 32 |
29 namespace i18n { | 33 namespace i18n { |
30 namespace addressinput { | 34 namespace addressinput { |
31 | 35 |
32 namespace { | 36 namespace { |
33 | 37 |
34 int GetMessageIdForField(AddressField field, | 38 int GetMessageIdForField(AddressField field, |
35 int admin_area_name_message_id, | 39 int admin_area_name_message_id, |
36 int postal_code_name_message_id) { | 40 int postal_code_name_message_id) { |
37 switch (field) { | 41 switch (field) { |
(...skipping 13 matching lines...) Expand all Loading... |
51 return IDS_LIBADDRESSINPUT_I18N_ADDRESS_LINE1_LABEL; | 55 return IDS_LIBADDRESSINPUT_I18N_ADDRESS_LINE1_LABEL; |
52 case ORGANIZATION: | 56 case ORGANIZATION: |
53 return IDS_LIBADDRESSINPUT_I18N_ORGANIZATION_LABEL; | 57 return IDS_LIBADDRESSINPUT_I18N_ORGANIZATION_LABEL; |
54 case RECIPIENT: | 58 case RECIPIENT: |
55 return IDS_LIBADDRESSINPUT_I18N_RECIPIENT_LABEL; | 59 return IDS_LIBADDRESSINPUT_I18N_RECIPIENT_LABEL; |
56 default: | 60 default: |
57 return INVALID_MESSAGE_ID; | 61 return INVALID_MESSAGE_ID; |
58 } | 62 } |
59 } | 63 } |
60 | 64 |
| 65 // Returns the BCP 47 language code that should be used to format the address |
| 66 // that the user entered. |
| 67 // |
| 68 // If the rule does not contain information about languages, then returns the |
| 69 // UI language. |
| 70 // |
| 71 // If the UI language is either the default language for the country, one of the |
| 72 // languages for rules, or one of the languages for address input, then returns |
| 73 // this UI language. If there're no matches, then picks one of the languages in |
| 74 // the rule and returns it. |
| 75 // |
| 76 // If latinized rules are available and the UI language code is not the primary |
| 77 // language code for this region, then returns the primary language with "-latn" |
| 78 // appended. |
| 79 std::string GetComponentsLanguageCode(const Rule& rule, |
| 80 const std::string& ui_language_code) { |
| 81 // Select the default language code for the region. |
| 82 std::string default_language_code; |
| 83 if (!rule.GetLanguage().empty()) { |
| 84 default_language_code = rule.GetLanguage(); |
| 85 } else if (!rule.GetLanguages().empty()) { |
| 86 default_language_code = rule.GetLanguages()[0]; |
| 87 } else if (!rule.GetInputLanguages().empty()) { |
| 88 default_language_code = rule.GetInputLanguages()[0]; |
| 89 } else { |
| 90 // Region does not have any language information (e.g. Antarctica). Use the |
| 91 // UI language code as is. |
| 92 return ui_language_code; |
| 93 } |
| 94 |
| 95 // If the UI language code is not set, then use default language code. |
| 96 if (ui_language_code.empty()) { |
| 97 return default_language_code; |
| 98 } |
| 99 |
| 100 const std::string& normalized_ui_language_code = |
| 101 NormalizeLanguageCode(ui_language_code); |
| 102 const std::string& normalized_default_language_code = |
| 103 NormalizeLanguageCode(default_language_code); |
| 104 |
| 105 // Check whether UI language code matches any language codes in the rule, |
| 106 // normalized or as is. |
| 107 if (normalized_default_language_code == normalized_ui_language_code || |
| 108 std::find( |
| 109 rule.GetLanguages().begin(), |
| 110 rule.GetLanguages().end(), |
| 111 ui_language_code) != rule.GetLanguages().end() || |
| 112 std::find( |
| 113 rule.GetLanguages().begin(), |
| 114 rule.GetLanguages().end(), |
| 115 normalized_ui_language_code) != rule.GetLanguages().end() || |
| 116 std::find( |
| 117 rule.GetInputLanguages().begin(), |
| 118 rule.GetInputLanguages().end(), |
| 119 ui_language_code) != rule.GetInputLanguages().end() || |
| 120 std::find( |
| 121 rule.GetInputLanguages().begin(), |
| 122 rule.GetInputLanguages().end(), |
| 123 normalized_ui_language_code) != rule.GetInputLanguages().end()) { |
| 124 return ui_language_code; |
| 125 } |
| 126 |
| 127 // The UI language code does not match any language information in the rule. |
| 128 return rule.GetLatinFormat().empty() |
| 129 ? default_language_code |
| 130 : normalized_default_language_code + "-latn"; |
| 131 } |
| 132 |
61 } // namespace | 133 } // namespace |
62 | 134 |
63 const std::vector<std::string>& GetRegionCodes() { | 135 const std::vector<std::string>& GetRegionCodes() { |
64 return RegionDataConstants::GetRegionCodes(); | 136 return RegionDataConstants::GetRegionCodes(); |
65 } | 137 } |
66 | 138 |
67 std::vector<AddressUiComponent> BuildComponents( | 139 std::vector<AddressUiComponent> BuildComponents( |
68 const std::string& region_code) { | 140 const std::string& region_code, |
| 141 const std::string& ui_language_code, |
| 142 std::string* components_language_code) { |
69 std::vector<AddressUiComponent> result; | 143 std::vector<AddressUiComponent> result; |
70 | 144 |
71 Rule rule; | 145 Rule rule; |
72 rule.CopyFrom(Rule::GetDefault()); | 146 rule.CopyFrom(Rule::GetDefault()); |
73 if (!rule.ParseSerializedRule( | 147 if (!rule.ParseSerializedRule( |
74 RegionDataConstants::GetRegionData(region_code))) { | 148 RegionDataConstants::GetRegionData(region_code))) { |
75 return result; | 149 return result; |
76 } | 150 } |
77 | 151 |
| 152 if (components_language_code != NULL) { |
| 153 *components_language_code = |
| 154 GetComponentsLanguageCode(rule, ui_language_code); |
| 155 } |
| 156 |
78 // For avoiding showing an input field twice, when the field is displayed | 157 // For avoiding showing an input field twice, when the field is displayed |
79 // twice on an envelope. | 158 // twice on an envelope. |
80 std::set<AddressField> fields; | 159 std::set<AddressField> fields; |
81 | 160 |
| 161 // If latinized rules are available and the |ui_language_code| is not the |
| 162 // primary language code for the region, then use the latinized formatting |
| 163 // rules. |
| 164 const std::vector<std::vector<FormatElement> >& format = |
| 165 rule.GetLatinFormat().empty() || |
| 166 ui_language_code.empty() || |
| 167 NormalizeLanguageCode(ui_language_code) == |
| 168 NormalizeLanguageCode(rule.GetLanguage()) |
| 169 ? rule.GetFormat() : rule.GetLatinFormat(); |
| 170 |
82 for (std::vector<std::vector<FormatElement> >::const_iterator | 171 for (std::vector<std::vector<FormatElement> >::const_iterator |
83 line_it = rule.GetFormat().begin(); | 172 line_it = format.begin(); |
84 line_it != rule.GetFormat().end(); | 173 line_it != format.end(); |
85 ++line_it) { | 174 ++line_it) { |
86 int num_fields_this_row = 0; | 175 int num_fields_this_row = 0; |
87 for (std::vector<FormatElement>::const_iterator element_it = | 176 for (std::vector<FormatElement>::const_iterator element_it = |
88 line_it->begin(); | 177 line_it->begin(); |
89 element_it != line_it->end(); | 178 element_it != line_it->end(); |
90 ++element_it) { | 179 ++element_it) { |
91 if (element_it->IsField()) { | 180 if (element_it->IsField()) { |
92 ++num_fields_this_row; | 181 ++num_fields_this_row; |
93 } | 182 } |
94 } | 183 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 return rule.GetRequired(); | 219 return rule.GetRequired(); |
131 } | 220 } |
132 | 221 |
133 const std::string& GetCompactAddressLinesSeparator( | 222 const std::string& GetCompactAddressLinesSeparator( |
134 const std::string& language_code) { | 223 const std::string& language_code) { |
135 return RegionDataConstants::GetLanguageCompactLineSeparator(language_code); | 224 return RegionDataConstants::GetLanguageCompactLineSeparator(language_code); |
136 } | 225 } |
137 | 226 |
138 } // namespace addressinput | 227 } // namespace addressinput |
139 } // namespace i18n | 228 } // namespace i18n |
OLD | NEW |