aboutsummaryrefslogtreecommitdiff
path: root/cpp/src/address_ui.cc
blob: a0993352e232a87d1d653c289aa8638730b8d2e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (C) 2013 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <libaddressinput/address_ui.h>

#include <libaddressinput/address_field.h>
#include <libaddressinput/address_ui_component.h>
#include <libaddressinput/localization.h>

#include <cassert>
#include <cstddef>
#include <set>
#include <string>
#include <vector>

#include "address_field_util.h"
#include "format_element.h"
#include "grit.h"
#include "language.h"
#include "messages.h"
#include "region_data_constants.h"
#include "rule.h"

namespace i18n {
namespace addressinput {

namespace {

std::string GetLabelForField(const Localization& localization,
                             AddressField field,
                             int admin_area_name_message_id,
                             int postal_code_name_message_id) {
  int messageId;
  switch (field) {
    case SORTING_CODE:
      // This needs no translation as it's used only in one locale.
      return "CEDEX";
    case COUNTRY:
      messageId = IDS_LIBADDRESSINPUT_COUNTRY_OR_REGION_LABEL;
      break;
    case ADMIN_AREA:
      messageId = admin_area_name_message_id;
      break;
    case LOCALITY:
      messageId = IDS_LIBADDRESSINPUT_LOCALITY_LABEL;
      break;
    case DEPENDENT_LOCALITY:
      messageId = IDS_LIBADDRESSINPUT_DISTRICT;
      break;
    case POSTAL_CODE:
      messageId = postal_code_name_message_id;
      break;
    case STREET_ADDRESS:
      messageId = IDS_LIBADDRESSINPUT_ADDRESS_LINE_1_LABEL;
      break;
    case RECIPIENT:
      messageId = IDS_LIBADDRESSINPUT_RECIPIENT_LABEL;
      break;
    default:
      messageId = INVALID_MESSAGE_ID;
  }
  return localization.GetString(messageId);
}

}  // namespace

const std::vector<std::string>& GetRegionCodes() {
  return RegionDataConstants::GetRegionCodes();
}

std::vector<AddressUiComponent> BuildComponents(
    const std::string& region_code,
    const Localization& localization,
    const std::string& ui_language_tag,
    std::string* best_address_language_tag) {
  assert(best_address_language_tag != NULL);
  std::vector<AddressUiComponent> result;

  Rule rule;
  rule.CopyFrom(Rule::GetDefault());
  if (!rule.ParseSerializedRule(
          RegionDataConstants::GetRegionData(region_code))) {
    return result;
  }

  const Language& best_address_language =
      ChooseBestAddressLanguage(rule, Language(ui_language_tag));
  *best_address_language_tag = best_address_language.tag;

  const std::vector<FormatElement>& format =
      !rule.GetLatinFormat().empty() &&
      best_address_language.has_latin_script
          ? rule.GetLatinFormat() : rule.GetFormat();

  // For avoiding showing an input field twice, when the field is displayed
  // twice on an envelope.
  std::set<AddressField> fields;

  bool preceded_by_newline = true;
  bool followed_by_newline = true;
  for (std::vector<FormatElement>::const_iterator format_it = format.begin();
       format_it != format.end(); ++format_it) {
    if (format_it->IsNewline()) {
      preceded_by_newline = true;
      continue;
    } else if (!format_it->IsField() ||
               !fields.insert(format_it->GetField()).second) {
      continue;
    }
    AddressUiComponent component;
    std::vector<FormatElement>::const_iterator next_format_it = format_it + 1;
    followed_by_newline =
        next_format_it == format.end() || next_format_it->IsNewline();
    component.length_hint = preceded_by_newline && followed_by_newline
                                ? AddressUiComponent::HINT_LONG
                                : AddressUiComponent::HINT_SHORT;
    preceded_by_newline = false;
    component.field = format_it->GetField();
    component.name = GetLabelForField(localization, format_it->GetField(),
        rule.GetAdminAreaNameMessageId(), rule.GetPostalCodeNameMessageId());
    result.push_back(component);
  }

  return result;
}

}  // namespace addressinput
}  // namespace i18n