Commit 7a24101a authored by Donn Denman's avatar Donn Denman Committed by Commit Bot

[TTS][Template URL] Add support for translations.

Adds support for new Contextual Search Resolve-request parameters
to provide translation hints from the client.  The server uses these
params as additional hints from the Chrome Language Model for the
user's preferred language and the language of the selection and
page content.

This means the returned Search Term may be translated or be accompanied
by a Translation Card.

The parameters we send in the Resolve request are the same parameters
we currently send to force a translation one-box in the Search request:
"&tlitesl=" for source language and "&tlitetl=" for the target language.

BUG=952401

Change-Id: Ia79d8c0cf3706009c2b6213895ad8f6df543c24c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2032058
Commit-Queue: Donn Denman <donnd@chromium.org>
Auto-Submit: Donn Denman <donnd@chromium.org>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#737387}
parent 90564151
...@@ -293,7 +293,8 @@ std::string ContextualSearchDelegate::BuildRequestUrl( ...@@ -293,7 +293,8 @@ std::string ContextualSearchDelegate::BuildRequestUrl(
TemplateURLRef::SearchTermsArgs::ContextualSearchParams params( TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
kContextualSearchRequestVersion, contextual_cards_version, kContextualSearchRequestVersion, contextual_cards_version,
context->GetHomeCountry(), context->GetPreviousEventId(), context->GetHomeCountry(), context->GetPreviousEventId(),
context->GetPreviousEventResults(), context->GetExactResolve()); context->GetPreviousEventResults(), context->GetExactResolve(),
std::string(), std::string());
search_terms_args.contextual_search_params = params; search_terms_args.contextual_search_params = params;
......
...@@ -221,16 +221,20 @@ TemplateURLRef::SearchTermsArgs::ContextualSearchParams:: ...@@ -221,16 +221,20 @@ TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
TemplateURLRef::SearchTermsArgs::ContextualSearchParams::ContextualSearchParams( TemplateURLRef::SearchTermsArgs::ContextualSearchParams::ContextualSearchParams(
int version, int version,
int contextual_cards_version, int contextual_cards_version,
const std::string& home_country, std::string home_country,
int64_t previous_event_id, int64_t previous_event_id,
int previous_event_results, int previous_event_results,
bool is_exact_search) bool is_exact_search,
std::string source_lang,
std::string target_lang)
: version(version), : version(version),
contextual_cards_version(contextual_cards_version), contextual_cards_version(contextual_cards_version),
home_country(home_country), home_country(home_country),
previous_event_id(previous_event_id), previous_event_id(previous_event_id),
previous_event_results(previous_event_results), previous_event_results(previous_event_results),
is_exact_search(is_exact_search) {} is_exact_search(is_exact_search),
source_lang(source_lang),
target_lang(target_lang) {}
TemplateURLRef::SearchTermsArgs::ContextualSearchParams::ContextualSearchParams( TemplateURLRef::SearchTermsArgs::ContextualSearchParams::ContextualSearchParams(
const ContextualSearchParams& other) = default; const ContextualSearchParams& other) = default;
...@@ -995,6 +999,10 @@ std::string TemplateURLRef::HandleReplacements( ...@@ -995,6 +999,10 @@ std::string TemplateURLRef::HandleReplacements(
} }
if (params.is_exact_search) if (params.is_exact_search)
args.push_back("ctxsl_exact=1"); args.push_back("ctxsl_exact=1");
if (!params.source_lang.empty())
args.push_back("tlitesl=" + params.source_lang);
if (!params.target_lang.empty())
args.push_back("tlitetl=" + params.target_lang);
HandleReplacement(std::string(), base::JoinString(args, "&"), *i, &url); HandleReplacement(std::string(), base::JoinString(args, "&"), *i, &url);
break; break;
......
...@@ -112,12 +112,20 @@ class TemplateURLRef { ...@@ -112,12 +112,20 @@ class TemplateURLRef {
// whatever else is in the context. The returned search term should not // whatever else is in the context. The returned search term should not
// be expanded, and the server will honor this along with creating a // be expanded, and the server will honor this along with creating a
// narrow Search Term. // narrow Search Term.
// The |source_lang| specifies a source language hint to apply for
// translation or to indicate that translation might be appropriate.
// This comes from CLD evaluating the selection and/or page content.
// The |target_lang| specifies the best language to translate into for
// the user, which also indicates when translation is appropriate or
// helpful. This comes from the Chrome Language Model.
ContextualSearchParams(int version, ContextualSearchParams(int version,
int contextual_cards_version, int contextual_cards_version,
const std::string& home_country, std::string home_country,
int64_t previous_event_id, int64_t previous_event_id,
int previous_event_results, int previous_event_results,
bool is_exact_search); bool is_exact_search,
std::string source_lang,
std::string target_lang);
ContextualSearchParams(const ContextualSearchParams& other); ContextualSearchParams(const ContextualSearchParams& other);
~ContextualSearchParams(); ~ContextualSearchParams();
...@@ -148,6 +156,12 @@ class TemplateURLRef { ...@@ -148,6 +156,12 @@ class TemplateURLRef {
// A flag that restricts the search to exactly match the selection rather // A flag that restricts the search to exactly match the selection rather
// than expanding the Search Term to include other words in the context. // than expanding the Search Term to include other words in the context.
bool is_exact_search = false; bool is_exact_search = false;
// Source language string to translate from.
std::string source_lang;
// Target language string to be translated into.
std::string target_lang;
}; };
// Estimates dynamic memory usage. // Estimates dynamic memory usage.
......
...@@ -1741,7 +1741,7 @@ TEST_F(TemplateURLTest, ContextualSearchParameters) { ...@@ -1741,7 +1741,7 @@ TEST_F(TemplateURLTest, ContextualSearchParameters) {
// Test the current common case, which uses no home country or previous // Test the current common case, which uses no home country or previous
// event. // event.
TemplateURLRef::SearchTermsArgs::ContextualSearchParams params( TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
2, 1, std::string(), 0, 0, false); 2, 1, std::string(), 0, 0, false, std::string(), std::string());
search_terms_args.contextual_search_params = params; search_terms_args.contextual_search_params = params;
result = url.url_ref().ReplaceSearchTerms(search_terms_args, result = url.url_ref().ReplaceSearchTerms(search_terms_args,
search_terms_data_); search_terms_data_);
...@@ -1754,7 +1754,7 @@ TEST_F(TemplateURLTest, ContextualSearchParameters) { ...@@ -1754,7 +1754,7 @@ TEST_F(TemplateURLTest, ContextualSearchParameters) {
// Test the home country and non-zero event data case. // Test the home country and non-zero event data case.
search_terms_args.contextual_search_params = search_terms_args.contextual_search_params =
TemplateURLRef::SearchTermsArgs::ContextualSearchParams( TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
2, 2, "CH", 1657713458, 5, false); 2, 2, "CH", 1657713458, 5, false, std::string(), std::string());
result = result =
url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
...@@ -1770,13 +1770,24 @@ TEST_F(TemplateURLTest, ContextualSearchParameters) { ...@@ -1770,13 +1770,24 @@ TEST_F(TemplateURLTest, ContextualSearchParameters) {
// Test exact-search. // Test exact-search.
search_terms_args.contextual_search_params = search_terms_args.contextual_search_params =
TemplateURLRef::SearchTermsArgs::ContextualSearchParams( TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
2, 1, std::string(), 0, 0, true); 2, 1, std::string(), 0, 0, true, std::string(), std::string());
result = result =
url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
// Find our param. // Find our param.
size_t found_pos = result.find("ctxsl_exact=1"); size_t found_pos = result.find("ctxsl_exact=1");
EXPECT_NE(found_pos, std::string::npos); EXPECT_NE(found_pos, std::string::npos);
// Test source and target languages.
search_terms_args.contextual_search_params =
TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
2, 1, std::string(), 0, 0, true, "es", "de");
result =
url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
// Find our params.
size_t source_pos = result.find("tlitesl=es");
EXPECT_NE(source_pos, std::string::npos);
size_t target_pos = result.find("tlitetl=de");
EXPECT_NE(target_pos, std::string::npos);
} }
TEST_F(TemplateURLTest, GenerateKeyword) { TEST_F(TemplateURLTest, GenerateKeyword) {
......
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