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> | |
23 #include <set> | 20 #include <set> |
24 #include <string> | 21 #include <string> |
25 #include <vector> | 22 #include <vector> |
26 | 23 |
27 #include "grit.h" | 24 #include "grit.h" |
28 #include "grit/libaddressinput_strings.h" | 25 #include "grit/libaddressinput_strings.h" |
29 #include "region_data_constants.h" | 26 #include "region_data_constants.h" |
30 #include "rule.h" | 27 #include "rule.h" |
31 #include "util/string_util.h" | |
32 | 28 |
33 namespace i18n { | 29 namespace i18n { |
34 namespace addressinput { | 30 namespace addressinput { |
35 | 31 |
36 namespace { | 32 namespace { |
37 | 33 |
38 int GetMessageIdForField(AddressField field, | 34 int GetMessageIdForField(AddressField field, |
39 int admin_area_name_message_id, | 35 int admin_area_name_message_id, |
40 int postal_code_name_message_id) { | 36 int postal_code_name_message_id) { |
41 switch (field) { | 37 switch (field) { |
(...skipping 13 matching lines...) Expand all Loading... |
55 return IDS_LIBADDRESSINPUT_I18N_ADDRESS_LINE1_LABEL; | 51 return IDS_LIBADDRESSINPUT_I18N_ADDRESS_LINE1_LABEL; |
56 case ORGANIZATION: | 52 case ORGANIZATION: |
57 return IDS_LIBADDRESSINPUT_I18N_ORGANIZATION_LABEL; | 53 return IDS_LIBADDRESSINPUT_I18N_ORGANIZATION_LABEL; |
58 case RECIPIENT: | 54 case RECIPIENT: |
59 return IDS_LIBADDRESSINPUT_I18N_RECIPIENT_LABEL; | 55 return IDS_LIBADDRESSINPUT_I18N_RECIPIENT_LABEL; |
60 default: | 56 default: |
61 return INVALID_MESSAGE_ID; | 57 return INVALID_MESSAGE_ID; |
62 } | 58 } |
63 } | 59 } |
64 | 60 |
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 | |
133 } // namespace | 61 } // namespace |
134 | 62 |
135 const std::vector<std::string>& GetRegionCodes() { | 63 const std::vector<std::string>& GetRegionCodes() { |
136 return RegionDataConstants::GetRegionCodes(); | 64 return RegionDataConstants::GetRegionCodes(); |
137 } | 65 } |
138 | 66 |
139 std::vector<AddressUiComponent> BuildComponents( | 67 std::vector<AddressUiComponent> BuildComponents( |
140 const std::string& region_code, | 68 const std::string& region_code) { |
141 const std::string& ui_language_code, | |
142 std::string* components_language_code) { | |
143 std::vector<AddressUiComponent> result; | 69 std::vector<AddressUiComponent> result; |
144 | 70 |
145 Rule rule; | 71 Rule rule; |
146 rule.CopyFrom(Rule::GetDefault()); | 72 rule.CopyFrom(Rule::GetDefault()); |
147 if (!rule.ParseSerializedRule( | 73 if (!rule.ParseSerializedRule( |
148 RegionDataConstants::GetRegionData(region_code))) { | 74 RegionDataConstants::GetRegionData(region_code))) { |
149 return result; | 75 return result; |
150 } | 76 } |
151 | 77 |
152 if (components_language_code != NULL) { | |
153 *components_language_code = | |
154 GetComponentsLanguageCode(rule, ui_language_code); | |
155 } | |
156 | |
157 // For avoiding showing an input field twice, when the field is displayed | 78 // For avoiding showing an input field twice, when the field is displayed |
158 // twice on an envelope. | 79 // twice on an envelope. |
159 std::set<AddressField> fields; | 80 std::set<AddressField> fields; |
160 | 81 |
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 | |
171 for (std::vector<std::vector<FormatElement> >::const_iterator | 82 for (std::vector<std::vector<FormatElement> >::const_iterator |
172 line_it = format.begin(); | 83 line_it = rule.GetFormat().begin(); |
173 line_it != format.end(); | 84 line_it != rule.GetFormat().end(); |
174 ++line_it) { | 85 ++line_it) { |
175 int num_fields_this_row = 0; | 86 int num_fields_this_row = 0; |
176 for (std::vector<FormatElement>::const_iterator element_it = | 87 for (std::vector<FormatElement>::const_iterator element_it = |
177 line_it->begin(); | 88 line_it->begin(); |
178 element_it != line_it->end(); | 89 element_it != line_it->end(); |
179 ++element_it) { | 90 ++element_it) { |
180 if (element_it->IsField()) { | 91 if (element_it->IsField()) { |
181 ++num_fields_this_row; | 92 ++num_fields_this_row; |
182 } | 93 } |
183 } | 94 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 return rule.GetRequired(); | 130 return rule.GetRequired(); |
220 } | 131 } |
221 | 132 |
222 const std::string& GetCompactAddressLinesSeparator( | 133 const std::string& GetCompactAddressLinesSeparator( |
223 const std::string& language_code) { | 134 const std::string& language_code) { |
224 return RegionDataConstants::GetLanguageCompactLineSeparator(language_code); | 135 return RegionDataConstants::GetLanguageCompactLineSeparator(language_code); |
225 } | 136 } |
226 | 137 |
227 } // namespace addressinput | 138 } // namespace addressinput |
228 } // namespace i18n | 139 } // namespace i18n |
OLD | NEW |