0

[Translate] Apply language synonyms to the target language

Expose ConvertLangCodeForTranslation and use it when converting the UX language
for all platforms (the performance hit should be negligible and this way
it is more legible than adding a #ifdef Android)

Fix a few unrelated typos on comments while at it.

BUG=311624

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@234706 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
miguelg@chromium.org
2013-11-13 01:55:09 +00:00
parent 39dce5fd18
commit 2f90b3cfd8
5 changed files with 29 additions and 23 deletions

@ -285,7 +285,7 @@ void TranslateLanguageList::OnResourceRequestsAllowed() {
kAlphaLanguageQueryValue);
std::string message = base::StringPrintf(
"Language list includeing alpha languages fetch starts (URL: %s)",
"Language list including alpha languages fetch starts (URL: %s)",
url.spec().c_str());
NotifyEvent(__LINE__, message);

@ -726,7 +726,9 @@ void TranslateManager::ShowBubble(WebContents* web_contents,
// static
std::string TranslateManager::GetTargetLanguage(PrefService* prefs) {
std::string ui_lang =
GetLanguageCode(g_browser_process->GetApplicationLocale());
TranslatePrefs::ConvertLangCodeForTranslation(
GetLanguageCode(g_browser_process->GetApplicationLocale()));
if (IsSupportedLanguage(ui_lang))
return ui_lang;

@ -47,26 +47,6 @@ 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;
translate::ToTranslateLanguageSynonym(&main_part);
return main_part;
}
// Expands language codes to make these more suitable for Accept-Language.
// Example: ['en-US', 'ja', 'en-CA'] => ['en-US', 'en', 'ja', 'en-CA'].
// 'en' won't appear twice as this function eliminates duplicates.
@ -471,6 +451,24 @@ void TranslatePrefs::CreateBlockedLanguages(
result.begin(), result.end());
}
// static
std::string TranslatePrefs::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;
translate::ToTranslateLanguageSynonym(&main_part);
return main_part;
}
bool TranslatePrefs::IsValueInList(const ListValue* list,
const std::string& in_value) const {
for (size_t i = 0; i < list->GetSize(); ++i) {

@ -95,6 +95,12 @@ class TranslatePrefs {
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
static void MigrateUserPrefs(PrefService* user_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 consistency
// with the JavaScript file.
static std::string ConvertLangCodeForTranslation(const std::string &lang);
private:
friend class TranslatePrefsTest;
FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, CreateBlockedLanguages);

@ -77,7 +77,7 @@ void ToTranslateLanguageSynonym(std::string* language) {
if (main_part.empty())
return;
// Apply liner search here because number of items in the list is just four.
// Apply linear search here because number of items in the list is just four.
for (size_t i = 0; i < arraysize(kLanguageCodeSynonyms); ++i) {
if (main_part == kLanguageCodeSynonyms[i].chrome_language) {
main_part = std::string(kLanguageCodeSynonyms[i].translate_language);