Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions cpp/include/libaddressinput/localization.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,13 @@ class Localization {
// there's no message with this identifier.
std::string GetString(int message_id) const;

// Returns the error message. If |enable_examples| is false, then the error
// message will not contain examples of valid input. If |enable_links| is
// false, then the error message will not contain HTML links. (Some error
// messages contain postal code examples or link to post office websites to
// look up the postal code for an address). Vector field values (e.g. for
// street address) should not be empty if problem is UNKNOWN_VALUE. The
// POSTAL_CODE field should only be used with MISSING_REQUIRED_FIELD,
// INVALID_FORMAT, and MISMATCHING_VALUE problem codes. All other fields
// should only be used with MISSING_REQUIRED_FIELD, UNKNOWN_VALUE, and
// USES_P_O_BOX problem codes.
std::string GetErrorMessage(const AddressData& address,
AddressField field,
AddressProblem problem,
bool enable_examples,
bool enable_links) const;

// Sets the string getter that takes a message identifier and returns the
// corresponding localized string. For example, in Chromium there is
// l10n_util::GetStringUTF8 which always returns strings in the current
// application locale.
void SetGetter(std::string (*getter)(int));

private:
// Returns the error message where the address field is a postal code. Helper
// to |GetErrorMessage|. If |postal_code_example| is empty, then the error
// message will not contain examples of valid postal codes. If
// |post_service_url| is empty, then the error message will not contain a post
// service URL. The problem should only be one of MISSING_REQUIRED_FIELD,
// INVALID_FORMAT, or MISMATCHING_VALUE.
std::string GetErrorMessageForPostalCode(
AddressProblem problem,
bool uses_postal_code_as_label,
const std::string& postal_code_example,
const std::string& post_service_url) const;

// The string getter.
std::string (*get_string_)(int);
};
Expand Down
130 changes: 0 additions & 130 deletions cpp/res/messages.grdp
Original file line number Diff line number Diff line change
Expand Up @@ -167,134 +167,4 @@ limitations under the License.
desc="Label for a neighborhood, shown as part of an address input form.">
Neighborhood
</message>
<message
name="IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD"
desc="Error message shown with a UI field when it is a required field and
the user has not filled it out.">
You can't leave this empty.
</message>
<message
name="IDS_LIBADDRESSINPUT_MISSING_REQUIRED_POSTAL_CODE_EXAMPLE_AND_URL"
desc="Error message shown with the postal code field when it is a required
field and the user has not filled it out, providing an example
postal code and a link to this country's postal service that a user
can use to look up their postal code.">
You must provide a postal code, for example <ph name="EXAMPLE">$1<ex>90291</ex></ph>. Don't know your postal code? Find it out <ph name="BEGIN_LINK">$2</ph>here<ph name="END_LINK">$3</ph>.
</message>
<message
name="IDS_LIBADDRESSINPUT_MISSING_REQUIRED_POSTAL_CODE_EXAMPLE"
desc="Error message shown with the postal code field when it is a required
field and the user has not filled it out, providing an example
postal code.">
You must provide a postal code, for example <ph name="EXAMPLE">$1<ex>90291</ex></ph>.
</message>
<message
name="IDS_LIBADDRESSINPUT_MISSING_REQUIRED_ZIP_CODE_EXAMPLE_AND_URL"
desc="Error message shown with the ZIP code field when it is a required
field and the user has not filled it out, providing an example ZIP
code and a link to this country's postal service that a user can use
to look up their ZIP code. This is specifically for countries using
ZIP codes instead of Postal codes, such as America.">
You must provide a ZIP code, for example <ph name="EXAMPLE">$1<ex>90291</ex></ph>. Don't know your ZIP code? Find it out <ph name="BEGIN_LINK">$2</ph>here<ph name="END_LINK">$3</ph>.
</message>
<message
name="IDS_LIBADDRESSINPUT_MISSING_REQUIRED_ZIP_CODE_EXAMPLE"
desc="Error message shown with the ZIP code field when it is a required
field and the user has not filled it out, providing an example ZIP
code.">
You must provide a ZIP code, for example <ph name="EXAMPLE">$1<ex>90291</ex></ph>.
</message>
<message
name="IDS_LIBADDRESSINPUT_UNKNOWN_VALUE"
desc="Occurs when the user fills out the wrong value for an address field.
For example, this would be shown when putting 'Cupertino' in United
States' State field.">
<ph name="FIELD_VALUE">$1<ex>Cupertino</ex></ph> is not recognized as a known value for this field.
</message>
<message
name="IDS_LIBADDRESSINPUT_UNRECOGNIZED_FORMAT_POSTAL_CODE_EXAMPLE_AND_URL"
desc="Occurs when the user fills out a postal code that does not conform
to the country's postal code format. For example, this would be
shown when using '80' as a Swiss postal code, which is normally 4
digits long. Provides an example postal code and a link to this
country's postal service that a user can use to look up their postal
code.">
This postal code format is not recognized. Example of a valid postal code: <ph name="EXAMPLE">$1<ex>90291</ex></ph>. Don't know your postal code? Find it out <ph name="BEGIN_LINK">$2</ph>here<ph name="END_LINK">$3</ph>.
</message>
<message
name="IDS_LIBADDRESSINPUT_UNRECOGNIZED_FORMAT_POSTAL_CODE_EXAMPLE"
desc="Occurs when the user fills out a postal code that does not conform
to the country's postal code format. For example, this would be
shown when using '80' as a Swiss postal code, which is normally 4
digits long. Provides an example postal code.">
This postal code format is not recognized. Example of a valid postal code: <ph name="EXAMPLE">$1<ex>90291</ex></ph>.
</message>
<message
name="IDS_LIBADDRESSINPUT_UNRECOGNIZED_FORMAT_POSTAL_CODE"
desc="Occurs when the user fills out a postal code that does not conform
to the country's postal code format. For example, this would be
shown when using '80' as a Swiss postal code, which is normally 4
digits long.">
This postal code format is not recognized.
</message>
<message
name="IDS_LIBADDRESSINPUT_UNRECOGNIZED_FORMAT_ZIP_CODE_EXAMPLE_AND_URL"
desc="Occurs when the user fills out a ZIP code that does not conform to
the country's ZIP code format. For example, this would be shown when
using '901' as a ZIP code for the United States. Provides an example
ZIP code and a link to this country's postal service that a user can
use to look up their ZIP code.">
This ZIP code format is not recognized. Example of a valid ZIP code: <ph name="EXAMPLE">$1<ex>90291</ex></ph>. Don't know your ZIP code? Find it out <ph name="BEGIN_LINK">$2</ph>here<ph name="END_LINK">$3</ph>.
</message>
<message
name="IDS_LIBADDRESSINPUT_UNRECOGNIZED_FORMAT_ZIP_CODE_EXAMPLE"
desc="Occurs when the user fills out a ZIP code that does not conform to
the country's ZIP code format. For example, this would be shown when
using '901' as a ZIP code for the United States. Provides an example
ZIP code.">
This ZIP code format is not recognized. Example of a valid ZIP code: <ph name="EXAMPLE">$1<ex>90291</ex></ph>.
</message>
<message
name="IDS_LIBADDRESSINPUT_UNRECOGNIZED_FORMAT_ZIP"
desc="Occurs when the user fills out a ZIP code that does not conform to
the country's ZIP code format. For example, this would be shown when
using '901' as a ZIP code for the United States.">
This ZIP code format is not recognized.
</message>
<message
name="IDS_LIBADDRESSINPUT_MISMATCHING_VALUE_POSTAL_CODE_URL"
desc="Occurs when the user fills out the wrong postal code for a certain
location. For example, this would be shown when using Z3Z 2Y7 for
Alberta, Canada. Provides a link to this country's postal service
that a user can use to look up their postal code.">
This postal code does not appear to match the rest of this address. Don't know your postal code? Find it out <ph name="BEGIN_LINK">$1</ph>here<ph name="END_LINK">$2</ph>.
</message>
<message
name="IDS_LIBADDRESSINPUT_MISMATCHING_VALUE_POSTAL_CODE"
desc="Occurs when the user fills out the wrong postal code for a certain
location. For example, this would be shown when using Z3Z 2Y7 for
Alberta, Canada.">
This postal code does not appear to match the rest of this address.
</message>
<message
name="IDS_LIBADDRESSINPUT_MISMATCHING_VALUE_ZIP_URL"
desc="Occurs when the user fills out the wrong ZIP code for a certain
location. For example, this would be shown when using 10001 for
Arizona state. Provides a link to this country's postal service that
a user can use to look up their ZIP code.">
This ZIP code does not appear to match the rest of this address. Don't know your ZIP code? Find it out <ph name="BEGIN_LINK">$1</ph>here<ph name="END_LINK">$2</ph>.
</message>
<message
name="IDS_LIBADDRESSINPUT_MISMATCHING_VALUE_ZIP"
desc="Occurs when the user fills out the wrong ZIP code for a certain
location. For example, this would be shown when using 10001 for
Arizona state.">
This ZIP code does not appear to match the rest of this address.
</message>
<message
name="IDS_LIBADDRESSINPUT_PO_BOX_FORBIDDEN_VALUE"
desc="Occurs when the user fills out a P.O. box as part of a physical
address.">
This address line appears to contain a post office box. Please use a street or building address.
</message>
</grit-part>
138 changes: 0 additions & 138 deletions cpp/src/localization.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,10 @@

#include <libaddressinput/localization.h>

#include <libaddressinput/address_data.h>
#include <libaddressinput/address_field.h>
#include <libaddressinput/address_problem.h>

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

#include "messages.h"
#include "region_data_constants.h"
#include "rule.h"
#include "util/string_split.h"
#include "util/string_util.h"

namespace {

void PushBackUrl(const std::string& url, std::vector<std::string>* parameters) {
assert(parameters != nullptr);
// TODO: HTML-escape the "url".
parameters->push_back("<a href=\"" + url + "\">");
parameters->emplace_back("</a>");
}

} // namespace

namespace i18n {
namespace addressinput {
Expand All @@ -60,127 +39,10 @@ std::string Localization::GetString(int message_id) const {
return get_string_(message_id);
}

std::string Localization::GetErrorMessage(const AddressData& address,
AddressField field,
AddressProblem problem,
bool enable_examples,
bool enable_links) const {
if (field == POSTAL_CODE) {
Rule rule;
rule.CopyFrom(Rule::GetDefault());
std::string postal_code_example, post_service_url;
if (rule.ParseSerializedRule(
RegionDataConstants::GetRegionData(address.region_code))) {
if (enable_examples) {
std::vector<std::string> examples_list;
SplitString(rule.GetPostalCodeExample(), ',', &examples_list);
if (!examples_list.empty()) {
postal_code_example = examples_list.front();
}
}
if (enable_links) {
post_service_url = rule.GetPostServiceUrl();
}
} else {
assert(false);
}
// If we can't parse the serialized rule |uses_postal_code_as_label| will be
// determined from the default rule.
bool uses_postal_code_as_label =
rule.GetPostalCodeNameMessageId() ==
IDS_LIBADDRESSINPUT_POSTAL_CODE_LABEL;
return GetErrorMessageForPostalCode(problem, uses_postal_code_as_label,
postal_code_example, post_service_url);
} else {
if (problem == MISSING_REQUIRED_FIELD) {
return get_string_(IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD);
} else if (problem == UNKNOWN_VALUE) {
std::vector<std::string> parameters;
if (AddressData::IsRepeatedFieldValue(field)) {
const auto& values = address.GetRepeatedFieldValue(field);
assert(!values.empty());
parameters.push_back(values.front());
} else {
parameters.push_back(address.GetFieldValue(field));
}
return DoReplaceStringPlaceholders(
get_string_(IDS_LIBADDRESSINPUT_UNKNOWN_VALUE), parameters);
} else if (problem == USES_P_O_BOX) {
return get_string_(IDS_LIBADDRESSINPUT_PO_BOX_FORBIDDEN_VALUE);
} else {
// Keep the default under "else" so the compiler helps us check that all
// handled cases return and don't fall through.
assert(false);
return "";
}
}
}

void Localization::SetGetter(std::string (*getter)(int)) {
assert(getter != nullptr);
get_string_ = getter;
}

std::string Localization::GetErrorMessageForPostalCode(
AddressProblem problem,
bool uses_postal_code_as_label,
const std::string& postal_code_example,
const std::string& post_service_url) const {
int message_id;
std::vector<std::string> parameters;
if (problem == MISSING_REQUIRED_FIELD) {
if (!postal_code_example.empty() && !post_service_url.empty()) {
message_id = uses_postal_code_as_label ?
IDS_LIBADDRESSINPUT_MISSING_REQUIRED_POSTAL_CODE_EXAMPLE_AND_URL :
IDS_LIBADDRESSINPUT_MISSING_REQUIRED_ZIP_CODE_EXAMPLE_AND_URL;
parameters.push_back(postal_code_example);
PushBackUrl(post_service_url, &parameters);
} else if (!postal_code_example.empty()) {
message_id = uses_postal_code_as_label ?
IDS_LIBADDRESSINPUT_MISSING_REQUIRED_POSTAL_CODE_EXAMPLE :
IDS_LIBADDRESSINPUT_MISSING_REQUIRED_ZIP_CODE_EXAMPLE;
parameters.push_back(postal_code_example);
} else {
message_id = IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD;
}
return DoReplaceStringPlaceholders(get_string_(message_id), parameters);
} else if (problem == INVALID_FORMAT) {
if (!postal_code_example.empty() && !post_service_url.empty()) {
message_id = uses_postal_code_as_label ?
IDS_LIBADDRESSINPUT_UNRECOGNIZED_FORMAT_POSTAL_CODE_EXAMPLE_AND_URL :
IDS_LIBADDRESSINPUT_UNRECOGNIZED_FORMAT_ZIP_CODE_EXAMPLE_AND_URL;
parameters.push_back(postal_code_example);
PushBackUrl(post_service_url, &parameters);
} else if (!postal_code_example.empty()) {
message_id = uses_postal_code_as_label ?
IDS_LIBADDRESSINPUT_UNRECOGNIZED_FORMAT_POSTAL_CODE_EXAMPLE :
IDS_LIBADDRESSINPUT_UNRECOGNIZED_FORMAT_ZIP_CODE_EXAMPLE;
parameters.push_back(postal_code_example);
} else {
message_id = uses_postal_code_as_label ?
IDS_LIBADDRESSINPUT_UNRECOGNIZED_FORMAT_POSTAL_CODE :
IDS_LIBADDRESSINPUT_UNRECOGNIZED_FORMAT_ZIP;
}
return DoReplaceStringPlaceholders(get_string_(message_id), parameters);
} else if (problem == MISMATCHING_VALUE) {
if (!post_service_url.empty()) {
message_id = uses_postal_code_as_label ?
IDS_LIBADDRESSINPUT_MISMATCHING_VALUE_POSTAL_CODE_URL :
IDS_LIBADDRESSINPUT_MISMATCHING_VALUE_ZIP_URL;
PushBackUrl(post_service_url, &parameters);
} else {
message_id = uses_postal_code_as_label ?
IDS_LIBADDRESSINPUT_MISMATCHING_VALUE_POSTAL_CODE :
IDS_LIBADDRESSINPUT_MISMATCHING_VALUE_ZIP;
}
return DoReplaceStringPlaceholders(get_string_(message_id), parameters);
} else {
// Keep the default under "else" so the compiler helps us check that all
// handled cases return and don't fall through.
assert(false);
return "";
}
}

} // namespace addressinput
} // namespace i18n
Loading