The language sub codes should be removed when it is added to the blocked list from Accept languages

BUG=140717
R=toyoshim@chromium.org

Review URL: https://chromiumcodereview.appspot.com/23522021

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221701 0039d316-1c4b-4281-b951-d872f2087c98
parent 09bdfa80
......@@ -44,6 +44,26 @@ void GetBlacklistedLanguages(const PrefService* prefs,
}
}
// Converts the language code for Translate. This removes the sub code (like
// -US) except for Chinese, and converts the synonyms.
// The same logic exists at language_options.js, and please keep consistensy
// with the JavaScript file.
std::string ConvertLangCodeForTranslation(const std::string &lang) {
std::vector<std::string> tokens;
base::SplitString(lang, '-', &tokens);
if (tokens.size() < 1)
return lang;
std::string main_part = tokens[0];
// Translate doesn't support General Chinese and the sub code is necessary.
if (main_part == "zh")
return lang;
TranslateUtil::ToTranslateLanguageSynonym(&main_part);
return main_part;
}
} // namespace
namespace {
......@@ -390,15 +410,14 @@ void TranslatePrefs::CreateBlockedLanguages(
for (std::vector<std::string>::const_iterator it = accept_languages.begin();
it != accept_languages.end(); ++it) {
std::string lang = *it;
TranslateUtil::ToTranslateLanguageSynonym(&lang);
std::string converted_lang = ConvertLangCodeForTranslation(*it);
// Regarding http://crbug.com/36182, even though English exists in Accept
// language list, English could be translated on non-English locale.
if (lang == "en" && !is_ui_english)
if (converted_lang == "en" && !is_ui_english)
continue;
result.insert(lang);
result.insert(converted_lang);
}
blocked_languages->insert(blocked_languages->begin(),
......
......@@ -22,12 +22,16 @@ TEST(TranslatePrefsTest, CreateBlockedLanguages) {
std::vector<std::string> accept_languages;
accept_languages.push_back("en");
// The subcode (IT) will be ignored when merging, except for Chinese.
accept_languages.push_back("it-IT");
accept_languages.push_back("ja");
// Filippino: synonym to 'tl'
accept_languages.push_back("fil");
// General Chinese is not used as Translate language, but not filtered
// when merging.
accept_languages.push_back("zh");
// Chinese with a sub code is acceptable for the blocked-language list.
accept_languages.push_back("zh-TW");
std::vector<std::string> blocked_languages;
......@@ -41,9 +45,11 @@ TEST(TranslatePrefsTest, CreateBlockedLanguages) {
expected.push_back("fr");
expected.push_back("iw");
expected.push_back("ht");
expected.push_back("it");
expected.push_back("ja");
expected.push_back("tl");
expected.push_back("zh");
expected.push_back("zh-TW");
EXPECT_EQ(expected.size(), blocked_languages.size());
for (std::vector<std::string>::const_iterator it = expected.begin();
......
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