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, |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "grit.h" | 24 #include "grit.h" |
25 #include "messages.h" | 25 #include "messages.h" |
26 #include "region_data_constants.h" | 26 #include "region_data_constants.h" |
27 #include "rule.h" | 27 #include "rule.h" |
28 | 28 |
29 namespace i18n { | 29 namespace i18n { |
30 namespace addressinput { | 30 namespace addressinput { |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 // Parses the default region data into the static Rule object and returns a | |
35 // constant reference to this object. Cannot return a copy of the object, | |
36 // because Rule objects are not copyable. | |
37 const Rule& InitDefaultRule() { | |
38 static Rule rule; | |
39 rule.ParseSerializedRule(RegionDataConstants::GetDefaultRegionData()); | |
40 return rule; | |
41 } | |
42 | |
43 // Returns the constant reference to the Rule object from InitDefaultRule(). The | |
44 // static object is in InitDefaultRule(), but this function maintains a constant | |
45 // static reference to it. The constant static reference avoids re-parsing the | |
46 // default region data. | |
47 const Rule& GetDefaultRule() { | |
48 static const Rule& kDefaultRule(InitDefaultRule()); | |
49 return kDefaultRule; | |
50 } | |
51 | |
52 int GetMessageIdForField(AddressField field, | 34 int GetMessageIdForField(AddressField field, |
53 int admin_area_name_message_id, | 35 int admin_area_name_message_id, |
54 int postal_code_name_message_id) { | 36 int postal_code_name_message_id) { |
55 switch (field) { | 37 switch (field) { |
56 case COUNTRY: | 38 case COUNTRY: |
57 return IDS_LIBADDRESSINPUT_I18N_COUNTRY_LABEL; | 39 return IDS_LIBADDRESSINPUT_I18N_COUNTRY_LABEL; |
58 case ADMIN_AREA: | 40 case ADMIN_AREA: |
59 return admin_area_name_message_id; | 41 return admin_area_name_message_id; |
60 case LOCALITY: | 42 case LOCALITY: |
61 return IDS_LIBADDRESSINPUT_I18N_LOCALITY_LABEL; | 43 return IDS_LIBADDRESSINPUT_I18N_LOCALITY_LABEL; |
(...skipping 19 matching lines...) Expand all Loading... |
81 const std::vector<std::string>& GetRegionCodes() { | 63 const std::vector<std::string>& GetRegionCodes() { |
82 return RegionDataConstants::GetRegionCodes(); | 64 return RegionDataConstants::GetRegionCodes(); |
83 } | 65 } |
84 | 66 |
85 std::vector<AddressUiComponent> BuildComponents( | 67 std::vector<AddressUiComponent> BuildComponents( |
86 const std::string& region_code, | 68 const std::string& region_code, |
87 const Localization& localization) { | 69 const Localization& localization) { |
88 std::vector<AddressUiComponent> result; | 70 std::vector<AddressUiComponent> result; |
89 | 71 |
90 Rule rule; | 72 Rule rule; |
91 rule.CopyFrom(GetDefaultRule()); | 73 rule.CopyFrom(Rule::GetDefault()); |
92 if (!rule.ParseSerializedRule( | 74 if (!rule.ParseSerializedRule( |
93 RegionDataConstants::GetRegionData(region_code))) { | 75 RegionDataConstants::GetRegionData(region_code))) { |
94 return result; | 76 return result; |
95 } | 77 } |
96 | 78 |
97 for (std::vector<std::vector<AddressField> >::const_iterator | 79 for (std::vector<std::vector<AddressField> >::const_iterator |
98 line_it = rule.GetFormat().begin(); | 80 line_it = rule.GetFormat().begin(); |
99 line_it != rule.GetFormat().end(); | 81 line_it != rule.GetFormat().end(); |
100 ++line_it) { | 82 ++line_it) { |
101 for (std::vector<AddressField>::const_iterator field_it = line_it->begin(); | 83 for (std::vector<AddressField>::const_iterator field_it = line_it->begin(); |
102 field_it != line_it->end(); ++field_it) { | 84 field_it != line_it->end(); ++field_it) { |
103 AddressUiComponent component; | 85 AddressUiComponent component; |
104 component.length_hint = | 86 component.length_hint = |
105 line_it->size() == 1 ? AddressUiComponent::HINT_LONG | 87 line_it->size() == 1 ? AddressUiComponent::HINT_LONG |
106 : AddressUiComponent::HINT_SHORT; | 88 : AddressUiComponent::HINT_SHORT; |
107 component.field = *field_it; | 89 component.field = *field_it; |
108 component.name = localization.GetString( | 90 component.name = localization.GetString( |
109 GetMessageIdForField(*field_it, rule.GetAdminAreaNameMessageId(), | 91 GetMessageIdForField(*field_it, rule.GetAdminAreaNameMessageId(), |
110 rule.GetPostalCodeNameMessageId())); | 92 rule.GetPostalCodeNameMessageId())); |
111 result.push_back(component); | 93 result.push_back(component); |
112 } | 94 } |
113 } | 95 } |
114 | 96 |
115 return result; | 97 return result; |
116 } | 98 } |
117 | 99 |
118 } // namespace addressinput | 100 } // namespace addressinput |
119 } // namespace i18n | 101 } // namespace i18n |
OLD | NEW |