1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/translate/core/common/translate_util.h"
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/strings/string_split.h"
11 #include "components/translate/core/common/translate_switches.h"
16 // Split the |language| into two parts. For example, if |language| is 'en-US',
17 // this will be split into the main part 'en' and the tail part '-US'.
18 void SplitIntoMainAndTail(const std::string
& language
,
19 std::string
* main_part
,
20 std::string
* tail_part
) {
24 std::vector
<std::string
> chunks
;
25 base::SplitString(language
, '-', &chunks
);
26 if (chunks
.size() == 0u)
29 *main_part
= chunks
[0];
30 *tail_part
= language
.substr(main_part
->size());
37 struct LanguageCodePair
{
38 // Code used in supporting list of Translate.
39 const char* const translate_language
;
41 // Code used in Chrome internal.
42 const char* const chrome_language
;
45 // Some languages are treated as same languages in Translate even though they
46 // are different to be exact.
48 // If this table is updated, please sync this with the synonym table in
49 // chrome/browser/resources/options/language_options.js
50 const LanguageCodePair kLanguageCodeSimilitudes
[] = {
55 // Some languages have changed codes over the years and sometimes the older
56 // codes are used, so we must see them as synonyms.
58 // If this table is updated, please sync this with the synonym table in
59 // chrome/browser/resources/options/language_options.js
60 const LanguageCodePair kLanguageCodeSynonyms
[] = {
65 // Some Chinese language codes are compatible with zh-TW or zh-CN in terms of
68 // If this table is updated, please sync this with the synonym table in
69 // chrome/browser/resources/options/language_options.js
70 const LanguageCodePair kLanguageCodeChineseCompatiblePairs
[] = {
76 const char kSecurityOrigin
[] = "https://translate.googleapis.com/";
78 void ToTranslateLanguageSynonym(std::string
* language
) {
79 for (size_t i
= 0; i
< arraysize(kLanguageCodeSimilitudes
); ++i
) {
80 if (*language
== kLanguageCodeSimilitudes
[i
].chrome_language
) {
81 *language
= kLanguageCodeSimilitudes
[i
].translate_language
;
86 for (size_t i
= 0; i
< arraysize(kLanguageCodeChineseCompatiblePairs
); ++i
) {
87 if (*language
== kLanguageCodeChineseCompatiblePairs
[i
].chrome_language
) {
88 *language
= kLanguageCodeChineseCompatiblePairs
[i
].translate_language
;
93 std::string main_part
, tail_part
;
94 SplitIntoMainAndTail(*language
, &main_part
, &tail_part
);
95 if (main_part
.empty())
98 // Apply linear search here because number of items in the list is just four.
99 for (size_t i
= 0; i
< arraysize(kLanguageCodeSynonyms
); ++i
) {
100 if (main_part
== kLanguageCodeSynonyms
[i
].chrome_language
) {
101 main_part
= std::string(kLanguageCodeSynonyms
[i
].translate_language
);
106 if (main_part
== "zh") {
107 *language
= main_part
+ tail_part
;
111 *language
= main_part
;
114 void ToChromeLanguageSynonym(std::string
* language
) {
115 for (size_t i
= 0; i
< arraysize(kLanguageCodeSimilitudes
); ++i
) {
116 if (*language
== kLanguageCodeSimilitudes
[i
].translate_language
) {
117 *language
= kLanguageCodeSimilitudes
[i
].chrome_language
;
122 std::string main_part
, tail_part
;
123 SplitIntoMainAndTail(*language
, &main_part
, &tail_part
);
124 if (main_part
.empty())
127 // Apply liner search here because number of items in the list is just four.
128 for (size_t i
= 0; i
< arraysize(kLanguageCodeSynonyms
); ++i
) {
129 if (main_part
== kLanguageCodeSynonyms
[i
].translate_language
) {
130 main_part
= std::string(kLanguageCodeSynonyms
[i
].chrome_language
);
135 *language
= main_part
+ tail_part
;
138 GURL
GetTranslateSecurityOrigin() {
139 std::string
security_origin(kSecurityOrigin
);
140 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
141 if (command_line
->HasSwitch(switches::kTranslateSecurityOrigin
)) {
143 command_line
->GetSwitchValueASCII(switches::kTranslateSecurityOrigin
);
145 return GURL(security_origin
);
148 } // namespace translate