This source file includes following definitions.
- SplitIntoMainAndTail
- ToTranslateLanguageSynonym
- ToChromeLanguageSynonym
- GetTranslateSecurityOrigin
#include "components/translate/core/common/translate_util.h"
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/strings/string_split.h"
#include "components/translate/core/common/translate_switches.h"
#include "url/gurl.h"
namespace {
void SplitIntoMainAndTail(const std::string& language,
std::string* main_part,
std::string* tail_part) {
DCHECK(main_part);
DCHECK(tail_part);
std::vector<std::string> chunks;
base::SplitString(language, '-', &chunks);
if (chunks.size() == 0u)
return;
*main_part = chunks[0];
*tail_part = language.substr(main_part->size());
}
}
namespace translate {
struct LanguageCodePair {
const char* const translate_language;
const char* const chrome_language;
};
const LanguageCodePair kLanguageCodeSimilitudes[] = {
{"no", "nb"},
{"tl", "fil"},
};
const LanguageCodePair kLanguageCodeSynonyms[] = {
{"iw", "he"},
{"jw", "jv"},
};
const char kSecurityOrigin[] = "https://translate.googleapis.com/";
void ToTranslateLanguageSynonym(std::string* language) {
for (size_t i = 0; i < arraysize(kLanguageCodeSimilitudes); ++i) {
if (*language == kLanguageCodeSimilitudes[i].chrome_language) {
*language = kLanguageCodeSimilitudes[i].translate_language;
return;
}
}
std::string main_part, tail_part;
SplitIntoMainAndTail(*language, &main_part, &tail_part);
if (main_part.empty())
return;
for (size_t i = 0; i < arraysize(kLanguageCodeSynonyms); ++i) {
if (main_part == kLanguageCodeSynonyms[i].chrome_language) {
main_part = std::string(kLanguageCodeSynonyms[i].translate_language);
break;
}
}
*language = main_part + tail_part;
}
void ToChromeLanguageSynonym(std::string* language) {
for (size_t i = 0; i < arraysize(kLanguageCodeSimilitudes); ++i) {
if (*language == kLanguageCodeSimilitudes[i].translate_language) {
*language = kLanguageCodeSimilitudes[i].chrome_language;
return;
}
}
std::string main_part, tail_part;
SplitIntoMainAndTail(*language, &main_part, &tail_part);
if (main_part.empty())
return;
for (size_t i = 0; i < arraysize(kLanguageCodeSynonyms); ++i) {
if (main_part == kLanguageCodeSynonyms[i].translate_language) {
main_part = std::string(kLanguageCodeSynonyms[i].chrome_language);
break;
}
}
*language = main_part + tail_part;
}
GURL GetTranslateSecurityOrigin() {
std::string security_origin(kSecurityOrigin);
CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kTranslateSecurityOrigin)) {
security_origin =
command_line->GetSwitchValueASCII(switches::kTranslateSecurityOrigin);
}
return GURL(security_origin);
}
}