Commit d3792b1f authored by estade@chromium.org's avatar estade@chromium.org

libaddressinput string translations

- make string translations work
- break libaddressinput standalone build

BUG=333405

Review URL: https://codereview.chromium.org/134903003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@245290 0039d316-1c4b-4281-b951-d872f2087c98
parent 2513dd6b
include_rules = [ include_rules = [
'+third_party/libaddressinput/src/cpp/include', '+third_party/libaddressinput/chromium/cpp/include/libaddressinput',
] ]
...@@ -9,10 +9,9 @@ ...@@ -9,10 +9,9 @@
#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_switches.h"
#include "components/autofill/core/browser/field_types.h" #include "components/autofill/core/browser/field_types.h"
#include "grit/component_strings.h" #include "grit/component_strings.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h" #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_field.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h" #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_ui.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui_component.h" #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_ui_component.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/localization.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
namespace autofill { namespace autofill {
...@@ -66,11 +65,8 @@ bool Enabled() { ...@@ -66,11 +65,8 @@ bool Enabled() {
void BuildAddressInputs(common::AddressType address_type, void BuildAddressInputs(common::AddressType address_type,
const std::string& country_code, const std::string& country_code,
DetailInputs* inputs) { DetailInputs* inputs) {
i18n::addressinput::Localization localization;
// TODO(dbeam): figure out how to include libaddressinput's translations into
// some .pak file so I can call |SetGetter(&l10n_util::GetStringUTF8)| here.
std::vector<AddressUiComponent> components( std::vector<AddressUiComponent> components(
i18n::addressinput::BuildComponents(country_code, localization)); i18n::addressinput::BuildComponents(country_code));
const bool billing = address_type == common::ADDRESS_TYPE_BILLING; const bool billing = address_type == common::ADDRESS_TYPE_BILLING;
...@@ -83,7 +79,7 @@ void BuildAddressInputs(common::AddressType address_type, ...@@ -83,7 +79,7 @@ void BuildAddressInputs(common::AddressType address_type,
ServerFieldType server_type = GetServerType(component.field, billing); ServerFieldType server_type = GetServerType(component.field, billing);
DetailInput::Length length = LengthFromHint(component.length_hint); DetailInput::Length length = LengthFromHint(component.length_hint);
base::string16 placeholder = base::UTF8ToUTF16(component.name); base::string16 placeholder = l10n_util::GetStringUTF16(component.name_id);
DetailInput input = { length, server_type, placeholder }; DetailInput input = { length, server_type, placeholder };
inputs->push_back(input); inputs->push_back(input);
...@@ -92,7 +88,7 @@ void BuildAddressInputs(common::AddressType address_type, ...@@ -92,7 +88,7 @@ void BuildAddressInputs(common::AddressType address_type,
// TODO(dbeam): support more than 2 address lines. http://crbug.com/324889 // TODO(dbeam): support more than 2 address lines. http://crbug.com/324889
ServerFieldType server_type = ServerFieldType server_type =
billing ? ADDRESS_BILLING_LINE2 : ADDRESS_HOME_LINE2; billing ? ADDRESS_BILLING_LINE2 : ADDRESS_HOME_LINE2;
base::string16 placeholder = base::UTF8ToUTF16(component.name); base::string16 placeholder = l10n_util::GetStringUTF16(component.name_id);
DetailInput input = { length, server_type, placeholder }; DetailInput input = { length, server_type, placeholder };
inputs->push_back(input); inputs->push_back(input);
} }
......
...@@ -3338,6 +3338,7 @@ ...@@ -3338,6 +3338,7 @@
['enable_autofill_dialog==1 and OS!="android"', { ['enable_autofill_dialog==1 and OS!="android"', {
'dependencies': [ 'dependencies': [
'../third_party/libaddressinput/libaddressinput.gyp:libaddressinput', '../third_party/libaddressinput/libaddressinput.gyp:libaddressinput',
'../third_party/libaddressinput/libaddressinput.gyp:libaddressinput_strings',
], ],
}, { # enable_autofill_dialog!=1 or OS=="android" }, { # enable_autofill_dialog!=1 or OS=="android"
'sources!': [ 'sources!': [
......
...@@ -394,6 +394,11 @@ ...@@ -394,6 +394,11 @@
'<(DEPTH)/ash/ash.gyp:ash_resources', '<(DEPTH)/ash/ash.gyp:ash_resources',
], ],
}], }],
['enable_autofill_dialog==1 and OS!="android"', {
'dependencies': [
'<(DEPTH)/third_party/libaddressinput/libaddressinput.gyp:libaddressinput_strings',
],
}],
['OS != "mac" and OS != "ios"', { ['OS != "mac" and OS != "ios"', {
# Copy pak files to the product directory. These files will be picked # Copy pak files to the product directory. These files will be picked
# up by the following installer scripts: # up by the following installer scripts:
......
...@@ -99,6 +99,13 @@ def calc_inputs(locale): ...@@ -99,6 +99,13 @@ def calc_inputs(locale):
inputs.append(os.path.join(SHARE_INT_DIR, 'ui', 'app_locale_settings', inputs.append(os.path.join(SHARE_INT_DIR, 'ui', 'app_locale_settings',
'app_locale_settings_%s.pak' % locale)) 'app_locale_settings_%s.pak' % locale))
if OS != 'ios' and OS != 'android':
#e.g. '<(SHARED_INTERMEDIATE_DIR)/third_party/libaddressinput/
# libaddressinput_strings_da.pak',
inputs.append(os.path.join(SHARE_INT_DIR, 'third_party', 'libaddressinput',
'libaddressinput_strings_%s.pak' % locale))
#e.g. '<(grit_out_dir)/google_chrome_strings_da.pak' #e.g. '<(grit_out_dir)/google_chrome_strings_da.pak'
# or # or
# '<(grit_out_dir)/chromium_strings_da.pak' # '<(grit_out_dir)/chromium_strings_da.pak'
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
#include <libaddressinput/address_field.h> #include <libaddressinput/address_field.h>
#include <iosfwd> #include <iosfwd>
#include <string>
namespace i18n { namespace i18n {
namespace addressinput { namespace addressinput {
...@@ -59,7 +58,7 @@ struct AddressProblem { ...@@ -59,7 +58,7 @@ struct AddressProblem {
MISMATCHING_VALUE MISMATCHING_VALUE
}; };
AddressProblem(AddressField field, Type type, const std::string& description); AddressProblem(AddressField field, Type type, int description_id);
~AddressProblem(); ~AddressProblem();
// The address field that has the problem. // The address field that has the problem.
...@@ -68,8 +67,9 @@ struct AddressProblem { ...@@ -68,8 +67,9 @@ struct AddressProblem {
// The type of problem. // The type of problem.
Type type; Type type;
// The human readable description of the problem. // The ID for the string that is the human readable description of the
std::string description; // problem.
int description_id;
}; };
// Produces human-readable output in logging, for example in unit tests. // Produces human-readable output in logging, for example in unit tests.
......
...@@ -21,17 +21,14 @@ ...@@ -21,17 +21,14 @@
namespace i18n { namespace i18n {
namespace addressinput { namespace addressinput {
class Localization;
struct AddressUiComponent; struct AddressUiComponent;
// Returns the list of supported CLDR region codes. // Returns the list of supported CLDR region codes.
const std::vector<std::string>& GetRegionCodes(); const std::vector<std::string>& GetRegionCodes();
// Returns the UI components for the CLDR |region_code|. Uses the strings from // Returns the UI components for the CLDR |region_code|. Returns an empty vector
// |localization|. Returns an empty vector on error. // on error.
std::vector<AddressUiComponent> BuildComponents( std::vector<AddressUiComponent> BuildComponents(const std::string& region_code);
const std::string& region_code,
const Localization& localization);
} // namespace addressinput } // namespace addressinput
} // namespace i18n } // namespace i18n
......
...@@ -17,8 +17,6 @@ ...@@ -17,8 +17,6 @@
#include <libaddressinput/address_field.h> #include <libaddressinput/address_field.h>
#include <string>
namespace i18n { namespace i18n {
namespace addressinput { namespace addressinput {
...@@ -35,8 +33,9 @@ struct AddressUiComponent { ...@@ -35,8 +33,9 @@ struct AddressUiComponent {
// The address field type for this UI component, for example LOCALITY. // The address field type for this UI component, for example LOCALITY.
AddressField field; AddressField field;
// The name of the field, for example "City". // The ID of the string for the name of the field, for example
std::string name; // IDS_LIBADDRESSINPUT_I18N_LOCALITY_LABEL.
int name_id;
// The hint for how large the input field should be in a multiline address // The hint for how large the input field should be in a multiline address
// form. // form.
......
...@@ -28,7 +28,6 @@ namespace addressinput { ...@@ -28,7 +28,6 @@ namespace addressinput {
class Downloader; class Downloader;
class LoadRulesDelegate; class LoadRulesDelegate;
class Localization;
class Storage; class Storage;
struct AddressData; struct AddressData;
...@@ -116,7 +115,6 @@ class AddressValidator { ...@@ -116,7 +115,6 @@ class AddressValidator {
// rules are available, but does not validate the |address|. // rules are available, but does not validate the |address|.
virtual Status ValidateAddress(const AddressData& address, virtual Status ValidateAddress(const AddressData& address,
const AddressProblemFilter& filter, const AddressProblemFilter& filter,
const Localization& localization,
AddressProblems* problems) const = 0; AddressProblems* problems) const = 0;
}; };
......
// 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.
#ifndef I18N_ADDRESSINPUT_LOCALIZATION_H_
#define I18N_ADDRESSINPUT_LOCALIZATION_H_
#include <string>
namespace i18n {
namespace addressinput {
// The object to retrieve localized strings based on message IDs. Sample usage:
// Localization localization;
// localization.SetLanguage("en");
// Process(BuildComponents("CA", localization));
//
// Alternative usage:
// Localization localization;
// localization.SetGetter(&MyStringGetter);
// Process(BuildComponents("CA", localization));
class Localization {
public:
// Initializes with English messages by default.
Localization();
~Localization();
// Returns the localized string for |message_id|. Returns an empty string if
// there's no message with this identifier.
std::string GetString(int message_id) const;
// Sets the language for the strings. The only supported language is "en"
// until we have translations.
void SetLanguage(const std::string& language_code);
// Sets the string getter that takes a message identifier and returns the
// corresponding localized string.
void SetGetter(std::string (*getter)(int));
private:
// The string getter.
std::string (*get_string_)(int);
};
} // namespace addressinput
} // namespace i18n
#endif // I18N_ADDRESSINPUT_LOCALIZATION_H_
...@@ -37,7 +37,6 @@ ...@@ -37,7 +37,6 @@
'src/address_ui.cc', 'src/address_ui.cc',
'src/address_validator.cc', 'src/address_validator.cc',
'src/country_rules_aggregator.cc', 'src/country_rules_aggregator.cc',
'src/localization.cc',
'src/region_data_constants.cc', 'src/region_data_constants.cc',
'src/retriever.cc', 'src/retriever.cc',
'src/rule.cc', 'src/rule.cc',
...@@ -65,7 +64,6 @@ ...@@ -65,7 +64,6 @@
'test/fake_downloader_test.cc', 'test/fake_downloader_test.cc',
'test/fake_storage.cc', 'test/fake_storage.cc',
'test/fake_storage_test.cc', 'test/fake_storage_test.cc',
'test/localization_test.cc',
'test/region_data_constants_test.cc', 'test/region_data_constants_test.cc',
'test/retriever_test.cc', 'test/retriever_test.cc',
'test/rule_test.cc', 'test/rule_test.cc',
......
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="am">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="ar">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="bg">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="bn">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="ca">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="cs">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="da">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="de">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="el">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="en-GB">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="es-419">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="es">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="et">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="fa">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="fi">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="fil">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="fr">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="gu">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="hi">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="hr">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="hu">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="id">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="it">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="iw">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="ja">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="kn">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="ko">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="lt">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="lv">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="ml">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="mr">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="ms">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="nl">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="no">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="pl">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="pt-BR">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="pt-PT">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="ro">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="ru">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="sk">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="sl">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="sr">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="sv">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="sw">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="ta">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="te">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="th">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="tr">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="uk">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="vi">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="zh-CN">
</translationbundle>
<?xml version="1.0" ?>
<!DOCTYPE translationbundle>
<translationbundle lang="zh-TW">
</translationbundle>
...@@ -22,8 +22,8 @@ namespace addressinput { ...@@ -22,8 +22,8 @@ namespace addressinput {
AddressProblem::AddressProblem(AddressField field, AddressProblem::AddressProblem(AddressField field,
Type type, Type type,
const std::string& description) int description_id)
: field(field), type(type), description(description) {} : field(field), type(type), description_id(description_id) {}
AddressProblem::~AddressProblem() {} AddressProblem::~AddressProblem() {}
...@@ -51,7 +51,7 @@ std::ostream& operator<<(std::ostream& o, AddressProblem::Type problem_type) { ...@@ -51,7 +51,7 @@ std::ostream& operator<<(std::ostream& o, AddressProblem::Type problem_type) {
std::ostream& operator<<(std::ostream& o, const AddressProblem& problem) { std::ostream& operator<<(std::ostream& o, const AddressProblem& problem) {
o << "[" << problem.field << ", " o << "[" << problem.field << ", "
<< problem.type << ", \"" << problem.type << ", \""
<< problem.description << "\"]"; << problem.description_id << "\"]";
return o; return o;
} }
......
...@@ -16,13 +16,12 @@ ...@@ -16,13 +16,12 @@
#include <libaddressinput/address_field.h> #include <libaddressinput/address_field.h>
#include <libaddressinput/address_ui_component.h> #include <libaddressinput/address_ui_component.h>
#include <libaddressinput/localization.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include "grit.h" #include "grit.h"
#include "messages.h" #include "grit/libaddressinput_strings.h"
#include "region_data_constants.h" #include "region_data_constants.h"
#include "rule.h" #include "rule.h"
...@@ -65,8 +64,7 @@ const std::vector<std::string>& GetRegionCodes() { ...@@ -65,8 +64,7 @@ const std::vector<std::string>& GetRegionCodes() {
} }
std::vector<AddressUiComponent> BuildComponents( std::vector<AddressUiComponent> BuildComponents(
const std::string& region_code, const std::string& region_code) {
const Localization& localization) {
std::vector<AddressUiComponent> result; std::vector<AddressUiComponent> result;
Rule rule; Rule rule;
...@@ -87,9 +85,10 @@ std::vector<AddressUiComponent> BuildComponents( ...@@ -87,9 +85,10 @@ std::vector<AddressUiComponent> BuildComponents(
line_it->size() == 1 ? AddressUiComponent::HINT_LONG line_it->size() == 1 ? AddressUiComponent::HINT_LONG
: AddressUiComponent::HINT_SHORT; : AddressUiComponent::HINT_SHORT;
component.field = *field_it; component.field = *field_it;
component.name = localization.GetString( component.name_id =
GetMessageIdForField(*field_it, rule.GetAdminAreaNameMessageId(), GetMessageIdForField(*field_it,
rule.GetPostalCodeNameMessageId())); rule.GetAdminAreaNameMessageId(),
rule.GetPostalCodeNameMessageId());
result.push_back(component); result.push_back(component);
} }
} }
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include <libaddressinput/address_data.h> #include <libaddressinput/address_data.h>
#include <libaddressinput/downloader.h> #include <libaddressinput/downloader.h>
#include <libaddressinput/load_rules_delegate.h> #include <libaddressinput/load_rules_delegate.h>
#include <libaddressinput/localization.h>
#include <libaddressinput/storage.h> #include <libaddressinput/storage.h>
#include <libaddressinput/util/basictypes.h> #include <libaddressinput/util/basictypes.h>
#include <libaddressinput/util/scoped_ptr.h> #include <libaddressinput/util/scoped_ptr.h>
...@@ -32,7 +31,7 @@ ...@@ -32,7 +31,7 @@
#include <re2/re2.h> #include <re2/re2.h>
#include "country_rules_aggregator.h" #include "country_rules_aggregator.h"
#include "messages.h" #include "grit/libaddressinput_strings.h"
#include "retriever.h" #include "retriever.h"
#include "rule.h" #include "rule.h"
#include "ruleset.h" #include "ruleset.h"
...@@ -97,7 +96,6 @@ class AddressValidatorImpl : public AddressValidator { ...@@ -97,7 +96,6 @@ class AddressValidatorImpl : public AddressValidator {
virtual Status ValidateAddress( virtual Status ValidateAddress(
const AddressData& address, const AddressData& address,
const AddressProblemFilter& filter, const AddressProblemFilter& filter,
const Localization& localization,
AddressProblems* problems) const { AddressProblems* problems) const {
std::map<std::string, const Ruleset*>::const_iterator ruleset_it = std::map<std::string, const Ruleset*>::const_iterator ruleset_it =
rules_.find(address.country_code); rules_.find(address.country_code);
...@@ -123,8 +121,7 @@ class AddressValidatorImpl : public AddressValidator { ...@@ -123,8 +121,7 @@ class AddressValidatorImpl : public AddressValidator {
problems->push_back(AddressProblem( problems->push_back(AddressProblem(
*field_it, *field_it,
AddressProblem::MISSING_REQUIRED_FIELD, AddressProblem::MISSING_REQUIRED_FIELD,
localization.GetString( IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD));
IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD)));
} }
} }
...@@ -140,8 +137,7 @@ class AddressValidatorImpl : public AddressValidator { ...@@ -140,8 +137,7 @@ class AddressValidatorImpl : public AddressValidator {
problems->push_back(AddressProblem( problems->push_back(AddressProblem(
POSTAL_CODE, POSTAL_CODE,
AddressProblem::UNRECOGNIZED_FORMAT, AddressProblem::UNRECOGNIZED_FORMAT,
localization.GetString( country_rule.GetInvalidPostalCodeMessageId()));
country_rule.GetInvalidPostalCodeMessageId())));
} }
while (ruleset != NULL) { while (ruleset != NULL) {
...@@ -160,8 +156,7 @@ class AddressValidatorImpl : public AddressValidator { ...@@ -160,8 +156,7 @@ class AddressValidatorImpl : public AddressValidator {
problems->push_back(AddressProblem( problems->push_back(AddressProblem(
sub_field_type, sub_field_type,
AddressProblem::UNKNOWN_VALUE, AddressProblem::UNKNOWN_VALUE,
localization.GetString( country_rule.GetInvalidFieldMessageId(sub_field_type)));
country_rule.GetInvalidFieldMessageId(sub_field_type))));
} }
// Validate sub-region specific postal code format. A sub-region specifies // Validate sub-region specific postal code format. A sub-region specifies
...@@ -180,8 +175,7 @@ class AddressValidatorImpl : public AddressValidator { ...@@ -180,8 +175,7 @@ class AddressValidatorImpl : public AddressValidator {
problems->push_back(AddressProblem( problems->push_back(AddressProblem(
POSTAL_CODE, POSTAL_CODE,
AddressProblem::MISMATCHING_VALUE, AddressProblem::MISMATCHING_VALUE,
localization.GetString( country_rule.GetInvalidPostalCodeMessageId()));
country_rule.GetInvalidPostalCodeMessageId())));
} }
ruleset = ruleset->GetSubRegionRuleset(sub_field); ruleset = ruleset->GetSubRegionRuleset(sub_field);
......
// 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/localization.h>
#include <cassert>
#include <cstddef>
#include <string>
namespace i18n {
namespace addressinput {
namespace {
// For each language code XX with translations:
// (1) Add a namespace XX here with an include of "XX_messages.cc".
// (2) Add a wrapper that converts the char pointer to std::string. (GRIT
// generated functions return char pointers.)
// (2) Use the XX::GetStdString in the SetLanguage() method below.
namespace en {
#include "en_messages.cc"
std::string GetStdString(int message_id) {
const char* str = GetString(message_id);
return str != NULL ? std::string(str) : std::string();
}
} // namespace en
} // namespace
Localization::Localization() : get_string_(&en::GetStdString) {}
Localization::~Localization() {}
std::string Localization::GetString(int message_id) const {
return get_string_(message_id);
}
void Localization::SetLanguage(const std::string& language_code) {
if (language_code == "en") {
get_string_ = &en::GetStdString;
} else {
assert(false);
}
}
void Localization::SetGetter(std::string (*getter)(int)) {
assert(getter != NULL);
get_string_ = getter;
}
} // namespace addressinput
} // namespace i18n
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
#include <vector> #include <vector>
#include "grit.h" #include "grit.h"
#include "messages.h" #include "grit/libaddressinput_strings.h"
#include "region_data_constants.h" #include "region_data_constants.h"
#include "util/json.h" #include "util/json.h"
#include "util/string_split.h" #include "util/string_split.h"
......
...@@ -16,22 +16,18 @@ ...@@ -16,22 +16,18 @@
#include <libaddressinput/address_field.h> #include <libaddressinput/address_field.h>
#include <libaddressinput/address_ui_component.h> #include <libaddressinput/address_ui_component.h>
#include <libaddressinput/localization.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace { #include "grit.h"
namespace i18n {
namespace addressinput {
using i18n::addressinput::AddressField; namespace {
using i18n::addressinput::AddressUiComponent;
using i18n::addressinput::BuildComponents;
using i18n::addressinput::COUNTRY;
using i18n::addressinput::GetRegionCodes;
using i18n::addressinput::Localization;
using i18n::addressinput::RECIPIENT;
// Returns testing::AssertionSuccess if the |components| are valid. Uses // Returns testing::AssertionSuccess if the |components| are valid. Uses
// |region_code| in test failure messages. // |region_code| in test failure messages.
...@@ -52,8 +48,8 @@ testing::AssertionResult ComponentsAreValid( ...@@ -52,8 +48,8 @@ testing::AssertionResult ComponentsAreValid(
<< component_it->field; << component_it->field;
} }
if (component_it->name.empty()) { if (component_it->name_id == INVALID_MESSAGE_ID) {
return testing::AssertionFailure() << "empty field name for field " return testing::AssertionFailure() << "invalid field name_id for field "
<< component_it->field; << component_it->field;
} }
} }
...@@ -62,10 +58,7 @@ testing::AssertionResult ComponentsAreValid( ...@@ -62,10 +58,7 @@ testing::AssertionResult ComponentsAreValid(
} }
// Tests for address UI functions. // Tests for address UI functions.
class AddressUiTest : public testing::TestWithParam<std::string> { class AddressUiTest : public testing::TestWithParam<std::string> {};
protected:
Localization localization_;
};
// Verifies that a region code consists of two characters, for example "TW". // Verifies that a region code consists of two characters, for example "TW".
TEST_P(AddressUiTest, RegionCodeHasTwoCharacters) { TEST_P(AddressUiTest, RegionCodeHasTwoCharacters) {
...@@ -75,7 +68,7 @@ TEST_P(AddressUiTest, RegionCodeHasTwoCharacters) { ...@@ -75,7 +68,7 @@ TEST_P(AddressUiTest, RegionCodeHasTwoCharacters) {
// Verifies that BuildComponents() returns valid UI components for a region // Verifies that BuildComponents() returns valid UI components for a region
// code. // code.
TEST_P(AddressUiTest, ComponentsAreValid) { TEST_P(AddressUiTest, ComponentsAreValid) {
EXPECT_TRUE(ComponentsAreValid(BuildComponents(GetParam(), localization_))); EXPECT_TRUE(ComponentsAreValid(BuildComponents(GetParam())));
} }
// Test all regions codes. // Test all regions codes.
...@@ -86,7 +79,10 @@ INSTANTIATE_TEST_CASE_P( ...@@ -86,7 +79,10 @@ INSTANTIATE_TEST_CASE_P(
// Verifies that BuildComponents() returns an empty vector for an invalid region // Verifies that BuildComponents() returns an empty vector for an invalid region
// code. // code.
TEST_F(AddressUiTest, InvalidRegionCodeReturnsEmptyVector) { TEST_F(AddressUiTest, InvalidRegionCodeReturnsEmptyVector) {
EXPECT_TRUE(BuildComponents("INVALID-REGION-CODE", localization_).empty()); EXPECT_TRUE(BuildComponents("INVALID-REGION-CODE").empty());
} }
} // namespace } // namespace
} // namespace addressinput
} // namespace i18n
// 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/localization.h>
#include <string>
#include <gtest/gtest.h>
#include "grit.h"
#include "messages.h"
namespace {
using i18n::addressinput::INVALID_MESSAGE_ID;
using i18n::addressinput::Localization;
// Tests for Localization object.
class LocalizationTest : public testing::TestWithParam<int> {
protected:
Localization localization_;
};
// Verifies that a custom message getter can be used.
const char kValidMessage[] = "Data";
std::string GetValidMessage(int message_id) { return kValidMessage; }
TEST_P(LocalizationTest, ValidStringGetterCanBeUsed) {
localization_.SetGetter(&GetValidMessage);
EXPECT_EQ(kValidMessage, localization_.GetString(GetParam()));
}
// Verifies that the default language for messages does not have empty strings.
TEST_P(LocalizationTest, DefaultStringIsNotEmpty) {
EXPECT_FALSE(localization_.GetString(GetParam()).empty());
}
// Verifies that English is the default language.
TEST_P(LocalizationTest, EnglishIsDefaultLanguage) {
std::string default_string = localization_.GetString(GetParam());
localization_.SetLanguage("en");
EXPECT_EQ(default_string, localization_.GetString(GetParam()));
}
// Tests all message identifiers.
INSTANTIATE_TEST_CASE_P(
AllMessages, LocalizationTest,
testing::Values(IDS_LIBADDRESSINPUT_I18N_COUNTRY_LABEL,
IDS_LIBADDRESSINPUT_I18N_LOCALITY_LABEL,
IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL,
IDS_LIBADDRESSINPUT_I18N_ORGANIZATION_LABEL,
IDS_LIBADDRESSINPUT_I18N_RECIPIENT_LABEL,
IDS_LIBADDRESSINPUT_I18N_ADDRESS_LINE1_LABEL,
IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL,
IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL,
IDS_LIBADDRESSINPUT_I18N_CEDEX_LABEL,
IDS_LIBADDRESSINPUT_I18N_AREA,
IDS_LIBADDRESSINPUT_I18N_COUNTY_LABEL,
IDS_LIBADDRESSINPUT_I18N_DEPARTMENT,
IDS_LIBADDRESSINPUT_I18N_DO_SI,
IDS_LIBADDRESSINPUT_I18N_EMIRATE,
IDS_LIBADDRESSINPUT_I18N_ISLAND,
IDS_LIBADDRESSINPUT_I18N_PARISH,
IDS_LIBADDRESSINPUT_I18N_PREFECTURE,
IDS_LIBADDRESSINPUT_I18N_PROVINCE,
IDS_LIBADDRESSINPUT_I18N_STATE_LABEL));
// Verifies that an invalid message identifier results in an empty string in the
// default configuration.
TEST_F(LocalizationTest, InvalidMessageIsEmptyString) {
EXPECT_TRUE(localization_.GetString(INVALID_MESSAGE_ID).empty());
}
} // namespace
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "messages.h" #include "grit/libaddressinput_strings.h"
#include "region_data_constants.h" #include "region_data_constants.h"
namespace { namespace {
......
...@@ -21,14 +21,14 @@ ...@@ -21,14 +21,14 @@
}, },
'targets': [ 'targets': [
{ {
'target_name': 'generated_messages', 'target_name': 'libaddressinput_strings',
'type': 'none', 'type': 'none',
'variables': { 'variables': {
'grit_out_dir': '<(SHARED_INTERMEDIATE_DIR)/libaddressinput/', 'grit_out_dir': '<(SHARED_INTERMEDIATE_DIR)/third_party/libaddressinput/',
}, },
'actions': [ 'actions': [
{ {
'action_name': 'generate_messages', 'action_name': 'libaddressinput_strings',
'variables': { 'variables': {
'grit_grd_file': '<(libaddressinput_dir)/cpp/res/messages.grd', 'grit_grd_file': '<(libaddressinput_dir)/cpp/res/messages.grd',
}, },
...@@ -61,7 +61,6 @@ ...@@ -61,7 +61,6 @@
'<(libaddressinput_dir)/cpp/include/libaddressinput/address_ui.h', '<(libaddressinput_dir)/cpp/include/libaddressinput/address_ui.h',
'<(libaddressinput_dir)/cpp/include/libaddressinput/address_validator.h', '<(libaddressinput_dir)/cpp/include/libaddressinput/address_validator.h',
'<(libaddressinput_dir)/cpp/include/libaddressinput/load_rules_delegate.h', '<(libaddressinput_dir)/cpp/include/libaddressinput/load_rules_delegate.h',
'<(libaddressinput_dir)/cpp/include/libaddressinput/localization.h',
'<(libaddressinput_dir)/cpp/include/libaddressinput/util/basictypes.h', '<(libaddressinput_dir)/cpp/include/libaddressinput/util/basictypes.h',
'<(libaddressinput_dir)/cpp/include/libaddressinput/util/internal/basictypes.h', '<(libaddressinput_dir)/cpp/include/libaddressinput/util/internal/basictypes.h',
'<(libaddressinput_dir)/cpp/include/libaddressinput/util/internal/move.h', '<(libaddressinput_dir)/cpp/include/libaddressinput/util/internal/move.h',
...@@ -76,7 +75,6 @@ ...@@ -76,7 +75,6 @@
'<(libaddressinput_dir)/cpp/src/country_rules_aggregator.cc', '<(libaddressinput_dir)/cpp/src/country_rules_aggregator.cc',
'<(libaddressinput_dir)/cpp/src/country_rules_aggregator.h', '<(libaddressinput_dir)/cpp/src/country_rules_aggregator.h',
'<(libaddressinput_dir)/cpp/src/grit.h', '<(libaddressinput_dir)/cpp/src/grit.h',
'<(libaddressinput_dir)/cpp/src/localization.cc',
'<(libaddressinput_dir)/cpp/src/region_data_constants.cc', '<(libaddressinput_dir)/cpp/src/region_data_constants.cc',
'<(libaddressinput_dir)/cpp/src/region_data_constants.h', '<(libaddressinput_dir)/cpp/src/region_data_constants.h',
'<(libaddressinput_dir)/cpp/src/retriever.cc', '<(libaddressinput_dir)/cpp/src/retriever.cc',
...@@ -98,7 +96,7 @@ ...@@ -98,7 +96,7 @@
'VALIDATION_DATA_URL="https://i18napis.appspot.com/ssl-address/"', 'VALIDATION_DATA_URL="https://i18napis.appspot.com/ssl-address/"',
], ],
'dependencies': [ 'dependencies': [
'generated_messages', 'libaddressinput_strings',
'<(DEPTH)/base/base.gyp:base', '<(DEPTH)/base/base.gyp:base',
'<(DEPTH)/third_party/re2/re2.gyp:re2', '<(DEPTH)/third_party/re2/re2.gyp:re2',
], ],
...@@ -127,7 +125,6 @@ ...@@ -127,7 +125,6 @@
'<(libaddressinput_dir)/cpp/test/fake_storage.cc', '<(libaddressinput_dir)/cpp/test/fake_storage.cc',
'<(libaddressinput_dir)/cpp/test/fake_storage.h', '<(libaddressinput_dir)/cpp/test/fake_storage.h',
'<(libaddressinput_dir)/cpp/test/fake_storage_test.cc', '<(libaddressinput_dir)/cpp/test/fake_storage_test.cc',
'<(libaddressinput_dir)/cpp/test/localization_test.cc',
'<(libaddressinput_dir)/cpp/test/region_data_constants_test.cc', '<(libaddressinput_dir)/cpp/test/region_data_constants_test.cc',
'<(libaddressinput_dir)/cpp/test/retriever_test.cc', '<(libaddressinput_dir)/cpp/test/retriever_test.cc',
'<(libaddressinput_dir)/cpp/test/rule_test.cc', '<(libaddressinput_dir)/cpp/test/rule_test.cc',
...@@ -145,6 +142,7 @@ ...@@ -145,6 +142,7 @@
], ],
'dependencies': [ 'dependencies': [
'libaddressinput', 'libaddressinput',
'libaddressinput_strings',
'<(DEPTH)/base/base.gyp:base_prefs', '<(DEPTH)/base/base.gyp:base_prefs',
'<(DEPTH)/base/base.gyp:run_all_unittests', '<(DEPTH)/base/base.gyp:run_all_unittests',
'<(DEPTH)/net/net.gyp:net_test_support', '<(DEPTH)/net/net.gyp:net_test_support',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment