Chromium Code Reviews| 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 <cstddef> | |
| 20 #include <set> | 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 "grit/libaddressinput_strings.h" | 27 #include "grit/libaddressinput_strings.h" |
| 28 #include "normalize_language_code.h" | |
| 26 #include "region_data_constants.h" | 29 #include "region_data_constants.h" |
| 27 #include "rule.h" | 30 #include "rule.h" |
| 28 | 31 |
| 29 namespace i18n { | 32 namespace i18n { |
| 30 namespace addressinput { | 33 namespace addressinput { |
| 31 | 34 |
| 32 namespace { | 35 namespace { |
| 33 | 36 |
| 34 int GetMessageIdForField(AddressField field, | 37 int GetMessageIdForField(AddressField field, |
| 35 int admin_area_name_message_id, | 38 int admin_area_name_message_id, |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 51 return IDS_LIBADDRESSINPUT_I18N_ADDRESS_LINE1_LABEL; | 54 return IDS_LIBADDRESSINPUT_I18N_ADDRESS_LINE1_LABEL; |
| 52 case ORGANIZATION: | 55 case ORGANIZATION: |
| 53 return IDS_LIBADDRESSINPUT_I18N_ORGANIZATION_LABEL; | 56 return IDS_LIBADDRESSINPUT_I18N_ORGANIZATION_LABEL; |
| 54 case RECIPIENT: | 57 case RECIPIENT: |
| 55 return IDS_LIBADDRESSINPUT_I18N_RECIPIENT_LABEL; | 58 return IDS_LIBADDRESSINPUT_I18N_RECIPIENT_LABEL; |
| 56 default: | 59 default: |
| 57 return INVALID_MESSAGE_ID; | 60 return INVALID_MESSAGE_ID; |
| 58 } | 61 } |
| 59 } | 62 } |
| 60 | 63 |
| 64 // Returns the BCP 47 language code that should be used to format the address | |
| 65 // that the user entered. | |
| 66 // | |
| 67 // If the rule does not contain information about languages, then returns the | |
| 68 // UI language. | |
| 69 // | |
| 70 // If the UI language is either the default language for the country, one of the | |
| 71 // languages for rules, or one of the languages for address input, then returns | |
| 72 // this UI language. If there're no matches, then picks one of the languages in | |
| 73 // the rule and returns it. | |
| 74 // | |
| 75 // If |using_latin_format| is true and the UI language does not match any | |
| 76 // languages in the rule, then appends "-latn" to the normalized version of the | |
| 77 // language for the rule. | |
| 78 std::string GetComponentsLanguageCode( | |
| 79 const std::vector<std::string>& rule_languages, | |
| 80 const std::vector<std::string>& input_languages, | |
| 81 const std::string& norm_default_lang, | |
|
Evan Stade
2014/03/21 23:44:54
ditto
please use gerrit instead
2014/03/22 01:34:30
How would I distinguish normalized UI language fro
Evan Stade
2014/03/24 19:04:15
maybe it's confusing because there's too many para
please use gerrit instead
2014/03/24 22:56:29
Made the function less confusing by passing in onl
| |
| 82 const std::string& norm_ui_lang, | |
| 83 const std::string& ui_lang, | |
| 84 bool using_latin_format) { | |
| 85 if (std::find(rule_languages.begin(), rule_languages.end(), norm_ui_lang) != | |
| 86 rule_languages.end() || | |
| 87 std::find(rule_languages.begin(), rule_languages.end(), ui_lang) != | |
| 88 rule_languages.end() || | |
| 89 std::find(input_languages.begin(), input_languages.end(), norm_ui_lang) != | |
| 90 input_languages.end() || | |
| 91 std::find(input_languages.begin(), input_languages.end(), ui_lang) != | |
| 92 input_languages.end() || | |
| 93 norm_default_lang == norm_ui_lang) { | |
| 94 return ui_lang; | |
| 95 } | |
| 96 | |
| 97 std::string rule_lang; | |
| 98 if (!norm_default_lang.empty()) { | |
| 99 rule_lang = norm_default_lang; | |
| 100 } else if (!rule_languages.empty()) { | |
| 101 rule_lang = rule_languages[0]; | |
| 102 } else if (!input_languages.empty()) { | |
| 103 rule_lang = input_languages[0]; | |
| 104 } else { | |
| 105 return ui_lang; | |
| 106 } | |
| 107 | |
| 108 return using_latin_format ? NormalizeLanguageCode(rule_lang) + "-latn" | |
| 109 : rule_lang; | |
| 110 } | |
| 111 | |
| 61 } // namespace | 112 } // namespace |
| 62 | 113 |
| 63 const std::vector<std::string>& GetRegionCodes() { | 114 const std::vector<std::string>& GetRegionCodes() { |
| 64 return RegionDataConstants::GetRegionCodes(); | 115 return RegionDataConstants::GetRegionCodes(); |
| 65 } | 116 } |
| 66 | 117 |
| 67 std::vector<AddressUiComponent> BuildComponents( | 118 std::vector<AddressUiComponent> BuildComponents( |
| 68 const std::string& region_code) { | 119 const std::string& region_code, |
| 120 const std::string& ui_language_code, | |
| 121 std::string* components_language_code) { | |
| 69 std::vector<AddressUiComponent> result; | 122 std::vector<AddressUiComponent> result; |
| 70 | 123 |
| 71 Rule rule; | 124 Rule rule; |
| 72 rule.CopyFrom(Rule::GetDefault()); | 125 rule.CopyFrom(Rule::GetDefault()); |
| 73 if (!rule.ParseSerializedRule( | 126 if (!rule.ParseSerializedRule( |
| 74 RegionDataConstants::GetRegionData(region_code))) { | 127 RegionDataConstants::GetRegionData(region_code))) { |
| 75 return result; | 128 return result; |
| 76 } | 129 } |
| 77 | 130 |
| 131 std::string norm_ui_lang = NormalizeLanguageCode(ui_language_code); | |
| 132 std::string norm_region_lang = NormalizeLanguageCode(rule.GetLanguage()); | |
| 133 | |
| 134 const std::vector<std::vector<FormatElement> >& latin_format = | |
| 135 rule.GetLatinFormat(); | |
| 136 bool use_latin_format = | |
| 137 norm_ui_lang != norm_region_lang && !latin_format.empty(); | |
| 138 const std::vector<std::vector<FormatElement> >& format = use_latin_format | |
| 139 ? latin_format : rule.GetFormat(); | |
| 140 if (components_language_code != NULL) { | |
| 141 *components_language_code = GetComponentsLanguageCode( | |
| 142 rule.GetLanguages(), rule.GetInputLanguages(), norm_region_lang, | |
| 143 norm_ui_lang, ui_language_code, use_latin_format); | |
| 144 } | |
| 145 | |
| 78 // For avoiding showing an input field twice, when the field is displayed | 146 // For avoiding showing an input field twice, when the field is displayed |
| 79 // twice on an envelope. | 147 // twice on an envelope. |
| 80 std::set<AddressField> fields; | 148 std::set<AddressField> fields; |
| 81 | 149 |
| 82 for (std::vector<std::vector<FormatElement> >::const_iterator | 150 for (std::vector<std::vector<FormatElement> >::const_iterator |
| 83 line_it = rule.GetFormat().begin(); | 151 line_it = format.begin(); |
| 84 line_it != rule.GetFormat().end(); | 152 line_it != format.end(); |
| 85 ++line_it) { | 153 ++line_it) { |
| 86 int num_fields_this_row = 0; | 154 int num_fields_this_row = 0; |
| 87 for (std::vector<FormatElement>::const_iterator element_it = | 155 for (std::vector<FormatElement>::const_iterator element_it = |
| 88 line_it->begin(); | 156 line_it->begin(); |
| 89 element_it != line_it->end(); | 157 element_it != line_it->end(); |
| 90 ++element_it) { | 158 ++element_it) { |
| 91 if (element_it->IsField()) { | 159 if (element_it->IsField()) { |
| 92 ++num_fields_this_row; | 160 ++num_fields_this_row; |
| 93 } | 161 } |
| 94 } | 162 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 return rule.GetRequired(); | 198 return rule.GetRequired(); |
| 131 } | 199 } |
| 132 | 200 |
| 133 const std::string& GetCompactAddressLinesSeparator( | 201 const std::string& GetCompactAddressLinesSeparator( |
| 134 const std::string& language_code) { | 202 const std::string& language_code) { |
| 135 return RegionDataConstants::GetLanguageCompactLineSeparator(language_code); | 203 return RegionDataConstants::GetLanguageCompactLineSeparator(language_code); |
| 136 } | 204 } |
| 137 | 205 |
| 138 } // namespace addressinput | 206 } // namespace addressinput |
| 139 } // namespace i18n | 207 } // namespace i18n |
| OLD | NEW |