Commit 3a532d27 authored by toyoshim@chromium.org's avatar toyoshim@chromium.org

Translate: split language code typo correction to apply unit tests.

Split language code type correction logic to an independent static
function. Then, apply unit tests to confirm that the logic correct
well-known mal-formed language code.

BUG=175673
TEST=unit_tests --gtest_filter'TranslateHelperTest.LanguageCodeTypoCorrection'

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@182459 0039d316-1c4b-4281-b951-d872f2087c98
parent 648121d0
......@@ -93,26 +93,7 @@ void TranslateHelper::PageCaptured(const string16& contents) {
// relevant for things like langauge textbooks). This distinction
// shouldn't affect translation.
std::string language = document.contentLanguage().utf8();
size_t coma_index = language.find(',');
if (coma_index != std::string::npos) {
// There are more than 1 language specified, just keep the first one.
language = language.substr(0, coma_index);
}
TrimWhitespaceASCII(language, TRIM_ALL, &language);
// An underscore instead of a dash is a frequent mistake.
size_t underscore_index = language.find('_');
if (underscore_index != std::string::npos)
language[underscore_index] = '-';
// Change everything up to a dash to lower-case and everything after to upper.
size_t dash_index = language.find('-');
if (dash_index != std::string::npos) {
language = StringToLowerASCII(language.substr(0, dash_index)) +
StringToUpperASCII(language.substr(dash_index));
} else {
language = StringToLowerASCII(language);
}
CorrectLanguageCodeTypo(&language);
#if defined(ENABLE_LANGUAGE_DETECTION)
if (language.empty()) {
......@@ -241,8 +222,36 @@ bool TranslateHelper::DontDelayTasks() {
////////////////////////////////////////////////////////////////////////////////
// TranslateHelper, private:
//
// static
void TranslateHelper::CorrectLanguageCodeTypo(std::string* code) {
DCHECK(code);
size_t coma_index = code->find(',');
if (coma_index != std::string::npos) {
// There are more than 1 language specified, just keep the first one.
*code = code->substr(0, coma_index);
}
TrimWhitespaceASCII(*code, TRIM_ALL, code);
// An underscore instead of a dash is a frequent mistake.
size_t underscore_index = code->find('_');
if (underscore_index != std::string::npos)
(*code)[underscore_index] = '-';
// Change everything up to a dash to lower-case and everything after to upper.
size_t dash_index = code->find('-');
if (dash_index != std::string::npos) {
*code = StringToLowerASCII(code->substr(0, dash_index)) +
StringToUpperASCII(code->substr(dash_index));
} else {
*code = StringToLowerASCII(*code);
}
}
// static
void TranslateHelper::ConvertLanguageCodeSynonym(std::string* code) {
DCHECK(code);
// Apply liner search here because number of items in the list is just four.
for (size_t i = 0; i < arraysize(kLanguageCodeSynonyms); ++i) {
if (code->compare(kLanguageCodeSynonyms[i].from) == 0) {
......
......@@ -66,8 +66,12 @@ class TranslateHelper : public content::RenderViewObserver {
virtual bool DontDelayTasks();
private:
FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, LanguageCodeTypoCorrection);
FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, LanguageCodeSynonyms);
// Correct language code if it contains well-known mistakes.
static void CorrectLanguageCodeTypo(std::string* code);
// Convert language code to the one used in server supporting list.
static void ConvertLanguageCodeSynonym(std::string* code);
......
......@@ -8,6 +8,26 @@
typedef testing::Test TranslateHelperTest;
// Tests that well-known language code typos are fixed.
TEST_F(TranslateHelperTest, LanguageCodeTypoCorrection) {
std::string language;
// Strip the second and later codes.
language = std::string("ja,en");
TranslateHelper::CorrectLanguageCodeTypo(&language);
EXPECT_EQ("ja", language);
// Replace dash with hyphen.
language = std::string("ja_JP");
TranslateHelper::CorrectLanguageCodeTypo(&language);
EXPECT_EQ("ja-JP", language);
// Correct wrong cases.
language = std::string("JA-jp");
TranslateHelper::CorrectLanguageCodeTypo(&language);
EXPECT_EQ("ja-JP", language);
}
// Tests that synonym language code is converted to one used in supporting list.
TEST_F(TranslateHelperTest, LanguageCodeSynonyms) {
std::string language;
......
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