1 // Copyright (C) 2013 Google Inc.
3 // Licensed under the Apache License, Version 2.0 (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
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include <libaddressinput/address_ui.h>
17 #include <libaddressinput/address_field.h>
18 #include <libaddressinput/address_ui_component.h>
19 #include <libaddressinput/localization.h>
27 #include "format_element.h"
31 #include "region_data_constants.h"
35 namespace addressinput {
39 std::string GetLabelForField(const Localization& localization,
41 int admin_area_name_message_id,
42 int postal_code_name_message_id) {
46 // This needs no translation as it's used only in one locale.
49 messageId = IDS_LIBADDRESSINPUT_COUNTRY_OR_REGION_LABEL;
52 messageId = admin_area_name_message_id;
55 messageId = IDS_LIBADDRESSINPUT_LOCALITY_LABEL;
57 case DEPENDENT_LOCALITY:
58 messageId = IDS_LIBADDRESSINPUT_DISTRICT;
61 messageId = postal_code_name_message_id;
64 messageId = IDS_LIBADDRESSINPUT_ADDRESS_LINE_1_LABEL;
67 messageId = IDS_LIBADDRESSINPUT_ORGANIZATION_LABEL;
70 messageId = IDS_LIBADDRESSINPUT_RECIPIENT_LABEL;
73 messageId = INVALID_MESSAGE_ID;
75 return localization.GetString(messageId);
80 const std::vector<std::string>& GetRegionCodes() {
81 return RegionDataConstants::GetRegionCodes();
84 std::vector<AddressUiComponent> BuildComponents(
85 const std::string& region_code,
86 const Localization& localization,
87 const std::string& ui_language_tag,
88 std::string* best_address_language_tag) {
89 assert(best_address_language_tag != NULL);
90 std::vector<AddressUiComponent> result;
93 rule.CopyFrom(Rule::GetDefault());
94 if (!rule.ParseSerializedRule(
95 RegionDataConstants::GetRegionData(region_code))) {
99 const Language& best_address_language =
100 ChooseBestAddressLanguage(rule, Language(ui_language_tag));
101 *best_address_language_tag = best_address_language.tag;
103 const std::vector<FormatElement>& format =
104 !rule.GetLatinFormat().empty() && best_address_language.has_latin_script
105 ? rule.GetLatinFormat()
108 // For avoiding showing an input field twice, when the field is displayed
109 // twice on an envelope.
110 std::set<AddressField> fields;
112 bool preceded_by_newline = true;
113 bool followed_by_newline = true;
114 for (std::vector<FormatElement>::const_iterator format_it = format.begin();
115 format_it != format.end(); ++format_it) {
116 if (format_it->IsNewline()) {
117 preceded_by_newline = true;
119 } else if (!format_it->IsField() ||
120 !fields.insert(format_it->GetField()).second) {
123 AddressUiComponent component;
124 std::vector<FormatElement>::const_iterator next_format_it = format_it + 1;
125 followed_by_newline =
126 next_format_it == format.end() || next_format_it->IsNewline();
127 component.length_hint = preceded_by_newline && followed_by_newline
128 ? AddressUiComponent::HINT_LONG
129 : AddressUiComponent::HINT_SHORT;
130 preceded_by_newline = false;
131 component.field = format_it->GetField();
132 component.name = GetLabelForField(localization,
133 format_it->GetField(),
134 rule.GetAdminAreaNameMessageId(),
135 rule.GetPostalCodeNameMessageId());
136 result.push_back(component);
142 } // namespace addressinput