Commit 53d0c401 authored by Yue Li's avatar Yue Li Committed by Commit Bot

Quick Answers: Show detected language name for translation query

Bug: b/170436934
Test: Manual Test
Change-Id: I81605b7aff2f0a93a36369a8f46778c3f36178c9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2491123Reviewed-by: default avatarJeroen Dhollander <jeroendh@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Commit-Queue: Yue Li <updowndota@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821368}
parent 90716fd7
...@@ -565,6 +565,11 @@ Try tapping the mic to ask me anything. ...@@ -565,6 +565,11 @@ Try tapping the mic to ask me anything.
Used memory Used memory
</message> </message>
<!-- Quick Answers -->
<message name="IDS_QUICK_ANSWERS_TRANSLATION_TITLE_TEXT" desc="The title text format string used for Quick Answers translation result card. The first placeholder contains the source text in foreign language and the second placeholder contains the detected source language name displayed in system locale.">
<ph name="QUERY_TEXT">$1<ex>prodotto</ex></ph> · <ph name="SOURCE_LANGUAGE_NAME">$2<ex>Italian</ex></ph>
</message>
</messages> </messages>
</release> </release>
</grit> </grit>
a6fc28576542213526c211c12ae26cd94e8c7710
\ No newline at end of file
...@@ -48,6 +48,7 @@ source_set("quick_answers") { ...@@ -48,6 +48,7 @@ source_set("quick_answers") {
"//chromeos/services/assistant/public/shared", "//chromeos/services/assistant/public/shared",
"//chromeos/services/machine_learning/public/cpp", "//chromeos/services/machine_learning/public/cpp",
"//chromeos/services/machine_learning/public/mojom", "//chromeos/services/machine_learning/public/mojom",
"//chromeos/strings:strings_grit",
"//components/prefs:prefs", "//components/prefs:prefs",
"//net:net", "//net:net",
"//services/data_decoder/public/cpp", "//services/data_decoder/public/cpp",
......
...@@ -2,5 +2,7 @@ include_rules = [ ...@@ -2,5 +2,7 @@ include_rules = [
"+ash/public", "+ash/public",
"+services/data_decoder/public", "+services/data_decoder/public",
"+third_party/cld_3/src/src/nnet_language_identifier.h", "+third_party/cld_3/src/src/nnet_language_identifier.h",
"+ui/base/l10n",
"+ui/base/resource/resource_bundle.h",
"+ui/gfx", "+ui/gfx",
] ]
...@@ -37,7 +37,8 @@ class TestResultLoader : public ResultLoader { ...@@ -37,7 +37,8 @@ class TestResultLoader : public ResultLoader {
return std::move(callback).Run(std::make_unique<network::ResourceRequest>(), return std::move(callback).Run(std::make_unique<network::ResourceRequest>(),
std::string()); std::string());
} }
void ProcessResponse(std::unique_ptr<std::string> response_body, void ProcessResponse(const PreprocessedOutput& preprocessed_output,
std::unique_ptr<std::string> response_body,
ResponseParserCallback complete_callback) override {} ResponseParserCallback complete_callback) override {}
}; };
......
...@@ -66,10 +66,11 @@ void ResultLoader::Fetch(const PreprocessedOutput& preprocessed_output) { ...@@ -66,10 +66,11 @@ void ResultLoader::Fetch(const PreprocessedOutput& preprocessed_output) {
// Load the resource. // Load the resource.
BuildRequest(preprocessed_output, BuildRequest(preprocessed_output,
base::BindOnce(&ResultLoader::OnBuildRequestComplete, base::BindOnce(&ResultLoader::OnBuildRequestComplete,
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr(), preprocessed_output));
} }
void ResultLoader::OnBuildRequestComplete( void ResultLoader::OnBuildRequestComplete(
const PreprocessedOutput& preprocessed_output,
std::unique_ptr<network::ResourceRequest> resource_request, std::unique_ptr<network::ResourceRequest> resource_request,
const std::string& request_body) { const std::string& request_body) {
loader_ = network::SimpleURLLoader::Create(std::move(resource_request), loader_ = network::SimpleURLLoader::Create(std::move(resource_request),
...@@ -81,10 +82,11 @@ void ResultLoader::OnBuildRequestComplete( ...@@ -81,10 +82,11 @@ void ResultLoader::OnBuildRequestComplete(
loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie( loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
network_loader_factory_, network_loader_factory_,
base::BindOnce(&ResultLoader::OnSimpleURLLoaderComplete, base::BindOnce(&ResultLoader::OnSimpleURLLoaderComplete,
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr(), preprocessed_output));
} }
void ResultLoader::OnSimpleURLLoaderComplete( void ResultLoader::OnSimpleURLLoaderComplete(
const PreprocessedOutput& preprocessed_output,
std::unique_ptr<std::string> response_body) { std::unique_ptr<std::string> response_body) {
base::TimeDelta duration = base::TimeTicks::Now() - fetch_start_time_; base::TimeDelta duration = base::TimeTicks::Now() - fetch_start_time_;
...@@ -95,7 +97,7 @@ void ResultLoader::OnSimpleURLLoaderComplete( ...@@ -95,7 +97,7 @@ void ResultLoader::OnSimpleURLLoaderComplete(
return; return;
} }
ProcessResponse(std::move(response_body), ProcessResponse(preprocessed_output, std::move(response_body),
base::BindOnce(&ResultLoader::OnResultParserComplete, base::BindOnce(&ResultLoader::OnResultParserComplete,
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr()));
} }
......
...@@ -83,7 +83,8 @@ class ResultLoader { ...@@ -83,7 +83,8 @@ class ResultLoader {
BuildRequestCallback callback) const = 0; BuildRequestCallback callback) const = 0;
// Process the |response_body| and invoked the callback with |QuickAnswer|. // Process the |response_body| and invoked the callback with |QuickAnswer|.
virtual void ProcessResponse(std::unique_ptr<std::string> response_body, virtual void ProcessResponse(const PreprocessedOutput& preprocessed_output,
std::unique_ptr<std::string> response_body,
ResponseParserCallback complete_callback) = 0; ResponseParserCallback complete_callback) = 0;
private: private:
...@@ -92,9 +93,11 @@ class ResultLoader { ...@@ -92,9 +93,11 @@ class ResultLoader {
ResultLoaderDelegate* const delegate_; ResultLoaderDelegate* const delegate_;
void OnBuildRequestComplete( void OnBuildRequestComplete(
const PreprocessedOutput& preprocessed_output,
std::unique_ptr<network::ResourceRequest> resource_request, std::unique_ptr<network::ResourceRequest> resource_request,
const std::string& request_body); const std::string& request_body);
void OnSimpleURLLoaderComplete(std::unique_ptr<std::string> response_body); void OnSimpleURLLoaderComplete(const PreprocessedOutput& preprocessed_output,
std::unique_ptr<std::string> response_body);
void OnResultParserComplete(std::unique_ptr<QuickAnswer> quick_answer); void OnResultParserComplete(std::unique_ptr<QuickAnswer> quick_answer);
// Time when the query is issued. // Time when the query is issued.
......
...@@ -87,6 +87,7 @@ void SearchResultLoader::BuildRequest( ...@@ -87,6 +87,7 @@ void SearchResultLoader::BuildRequest(
} }
void SearchResultLoader::ProcessResponse( void SearchResultLoader::ProcessResponse(
const PreprocessedOutput& preprocessed_output,
std::unique_ptr<std::string> response_body, std::unique_ptr<std::string> response_body,
ResponseParserCallback complete_callback) { ResponseParserCallback complete_callback) {
search_response_parser_ = search_response_parser_ =
......
...@@ -33,7 +33,8 @@ class SearchResultLoader : public ResultLoader { ...@@ -33,7 +33,8 @@ class SearchResultLoader : public ResultLoader {
// ResultLoader: // ResultLoader:
void BuildRequest(const PreprocessedOutput& preprocessed_output, void BuildRequest(const PreprocessedOutput& preprocessed_output,
BuildRequestCallback callback) const override; BuildRequestCallback callback) const override;
void ProcessResponse(std::unique_ptr<std::string> response_body, void ProcessResponse(const PreprocessedOutput& preprocessed_output,
std::unique_ptr<std::string> response_body,
ResponseParserCallback complete_callback) override; ResponseParserCallback complete_callback) override;
private: private:
......
...@@ -23,14 +23,16 @@ TranslationResponseParser::~TranslationResponseParser() { ...@@ -23,14 +23,16 @@ TranslationResponseParser::~TranslationResponseParser() {
} }
void TranslationResponseParser::ProcessResponse( void TranslationResponseParser::ProcessResponse(
std::unique_ptr<std::string> response_body) { std::unique_ptr<std::string> response_body,
const std::string& title_text) {
data_decoder::DataDecoder::ParseJsonIsolated( data_decoder::DataDecoder::ParseJsonIsolated(
response_body->c_str(), response_body->c_str(),
base::BindOnce(&TranslationResponseParser::OnJsonParsed, base::BindOnce(&TranslationResponseParser::OnJsonParsed,
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr(), title_text));
} }
void TranslationResponseParser::OnJsonParsed( void TranslationResponseParser::OnJsonParsed(
const std::string& title_text,
data_decoder::DataDecoder::ValueOrError result) { data_decoder::DataDecoder::ValueOrError result) {
DCHECK(complete_callback_); DCHECK(complete_callback_);
...@@ -60,6 +62,8 @@ void TranslationResponseParser::OnJsonParsed( ...@@ -60,6 +62,8 @@ void TranslationResponseParser::OnJsonParsed(
auto quick_answer = std::make_unique<QuickAnswer>(); auto quick_answer = std::make_unique<QuickAnswer>();
quick_answer->result_type = ResultType::kTranslationResult; quick_answer->result_type = ResultType::kTranslationResult;
quick_answer->primary_answer = *translated_text; quick_answer->primary_answer = *translated_text;
quick_answer->secondary_answer = title_text;
quick_answer->title.push_back(std::make_unique<QuickAnswerText>(title_text));
quick_answer->first_answer_row.push_back( quick_answer->first_answer_row.push_back(
std::make_unique<QuickAnswerResultText>(*translated_text)); std::make_unique<QuickAnswerResultText>(*translated_text));
......
...@@ -35,10 +35,12 @@ class TranslationResponseParser { ...@@ -35,10 +35,12 @@ class TranslationResponseParser {
delete; delete;
// Starts processing the search response. // Starts processing the search response.
void ProcessResponse(std::unique_ptr<std::string> response_body); void ProcessResponse(std::unique_ptr<std::string> response_body,
const std::string& title_text);
private: private:
void OnJsonParsed(data_decoder::DataDecoder::ValueOrError result); void OnJsonParsed(const std::string& title_text,
data_decoder::DataDecoder::ValueOrError result);
TranslationResponseParserCallback complete_callback_; TranslationResponseParserCallback complete_callback_;
......
...@@ -64,11 +64,13 @@ TEST_F(TranslationResponseParserTest, ProcessResponseSuccess) { ...@@ -64,11 +64,13 @@ TEST_F(TranslationResponseParserTest, ProcessResponseSuccess) {
} }
} }
)"; )";
constexpr char kTranslationTitle[] = "testo tradotto · Italian";
translation_response_parser_->ProcessResponse( translation_response_parser_->ProcessResponse(
std::make_unique<std::string>(kTranslationResponse)); std::make_unique<std::string>(kTranslationResponse), kTranslationTitle);
WaitForResponse(); WaitForResponse();
EXPECT_TRUE(quick_answer_); EXPECT_TRUE(quick_answer_);
EXPECT_EQ("translated text", quick_answer_->primary_answer); EXPECT_EQ("translated text", quick_answer_->primary_answer);
EXPECT_EQ(kTranslationTitle, quick_answer_->secondary_answer);
EXPECT_EQ(ResultType::kTranslationResult, quick_answer_->result_type); EXPECT_EQ(ResultType::kTranslationResult, quick_answer_->result_type);
} }
...@@ -77,14 +79,14 @@ TEST_F(TranslationResponseParserTest, ProcessResponseNoResults) { ...@@ -77,14 +79,14 @@ TEST_F(TranslationResponseParserTest, ProcessResponseNoResults) {
{} {}
)"; )";
translation_response_parser_->ProcessResponse( translation_response_parser_->ProcessResponse(
std::make_unique<std::string>(kTranslationResponse)); std::make_unique<std::string>(kTranslationResponse), std::string());
WaitForResponse(); WaitForResponse();
EXPECT_FALSE(quick_answer_); EXPECT_FALSE(quick_answer_);
} }
TEST_F(TranslationResponseParserTest, ProcessResponseInvalidResponse) { TEST_F(TranslationResponseParserTest, ProcessResponseInvalidResponse) {
translation_response_parser_->ProcessResponse( translation_response_parser_->ProcessResponse(
std::make_unique<std::string>("results {}")); std::make_unique<std::string>("results {}"), std::string());
WaitForResponse(); WaitForResponse();
EXPECT_FALSE(quick_answer_); EXPECT_FALSE(quick_answer_);
} }
......
...@@ -10,10 +10,13 @@ ...@@ -10,10 +10,13 @@
#include "base/json/json_writer.h" #include "base/json/json_writer.h"
#include "chromeos/components/quick_answers/quick_answers_model.h" #include "chromeos/components/quick_answers/quick_answers_model.h"
#include "chromeos/services/assistant/public/shared/constants.h" #include "chromeos/services/assistant/public/shared/constants.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "net/base/escape.h" #include "net/base/escape.h"
#include "net/base/url_util.h" #include "net/base/url_util.h"
#include "services/network/public/cpp/resource_request.h" #include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h" #include "services/network/public/cpp/simple_url_loader.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace chromeos { namespace chromeos {
...@@ -40,6 +43,18 @@ constexpr base::StringPiece kQueryKey = "q"; ...@@ -40,6 +43,18 @@ constexpr base::StringPiece kQueryKey = "q";
constexpr base::StringPiece kSourceLanguageKey = "source"; constexpr base::StringPiece kSourceLanguageKey = "source";
constexpr base::StringPiece kTargetLanguageKey = "target"; constexpr base::StringPiece kTargetLanguageKey = "target";
std::string BuildTitleText(const IntentInfo& intent_info) {
// TODO(b/169453041): Add test support for localized strings.
if (!ui::ResourceBundle::HasSharedInstance())
return std::string();
auto locale_name = l10n_util::GetDisplayNameForLocale(
intent_info.source_language, intent_info.target_language, true);
return l10n_util::GetStringFUTF8(IDS_QUICK_ANSWERS_TRANSLATION_TITLE_TEXT,
base::UTF8ToUTF16(intent_info.intent_text),
locale_name);
}
std::string BuildTranslationRequestBody(const IntentInfo& intent_info) { std::string BuildTranslationRequestBody(const IntentInfo& intent_info) {
Value payload(Value::Type::DICTIONARY); Value payload(Value::Type::DICTIONARY);
...@@ -74,11 +89,14 @@ void TranslationResultLoader::BuildRequest( ...@@ -74,11 +89,14 @@ void TranslationResultLoader::BuildRequest(
} }
void TranslationResultLoader::ProcessResponse( void TranslationResultLoader::ProcessResponse(
const PreprocessedOutput& preprocessed_output,
std::unique_ptr<std::string> response_body, std::unique_ptr<std::string> response_body,
ResponseParserCallback complete_callback) { ResponseParserCallback complete_callback) {
translation_response_parser_ = translation_response_parser_ =
std::make_unique<TranslationResponseParser>(std::move(complete_callback)); std::make_unique<TranslationResponseParser>(std::move(complete_callback));
translation_response_parser_->ProcessResponse(std::move(response_body)); translation_response_parser_->ProcessResponse(
std::move(response_body),
BuildTitleText(preprocessed_output.intent_info));
} }
void TranslationResultLoader::OnRequestAccessTokenComplete( void TranslationResultLoader::OnRequestAccessTokenComplete(
......
...@@ -33,7 +33,8 @@ class TranslationResultLoader : public ResultLoader { ...@@ -33,7 +33,8 @@ class TranslationResultLoader : public ResultLoader {
// ResultLoader: // ResultLoader:
void BuildRequest(const PreprocessedOutput& preprocessed_output, void BuildRequest(const PreprocessedOutput& preprocessed_output,
BuildRequestCallback callback) const override; BuildRequestCallback callback) const override;
void ProcessResponse(std::unique_ptr<std::string> response_body, void ProcessResponse(const PreprocessedOutput& preprocessed_output,
std::unique_ptr<std::string> response_body,
ResponseParserCallback complete_callback) override; ResponseParserCallback complete_callback) override;
private: private:
......
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