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.
Used memory
</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>
</release>
</grit>
a6fc28576542213526c211c12ae26cd94e8c7710
\ No newline at end of file
......@@ -48,6 +48,7 @@ source_set("quick_answers") {
"//chromeos/services/assistant/public/shared",
"//chromeos/services/machine_learning/public/cpp",
"//chromeos/services/machine_learning/public/mojom",
"//chromeos/strings:strings_grit",
"//components/prefs:prefs",
"//net:net",
"//services/data_decoder/public/cpp",
......
......@@ -2,5 +2,7 @@ include_rules = [
"+ash/public",
"+services/data_decoder/public",
"+third_party/cld_3/src/src/nnet_language_identifier.h",
"+ui/base/l10n",
"+ui/base/resource/resource_bundle.h",
"+ui/gfx",
]
......@@ -37,7 +37,8 @@ class TestResultLoader : public ResultLoader {
return std::move(callback).Run(std::make_unique<network::ResourceRequest>(),
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 {}
};
......
......@@ -66,10 +66,11 @@ void ResultLoader::Fetch(const PreprocessedOutput& preprocessed_output) {
// Load the resource.
BuildRequest(preprocessed_output,
base::BindOnce(&ResultLoader::OnBuildRequestComplete,
weak_factory_.GetWeakPtr()));
weak_factory_.GetWeakPtr(), preprocessed_output));
}
void ResultLoader::OnBuildRequestComplete(
const PreprocessedOutput& preprocessed_output,
std::unique_ptr<network::ResourceRequest> resource_request,
const std::string& request_body) {
loader_ = network::SimpleURLLoader::Create(std::move(resource_request),
......@@ -81,10 +82,11 @@ void ResultLoader::OnBuildRequestComplete(
loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
network_loader_factory_,
base::BindOnce(&ResultLoader::OnSimpleURLLoaderComplete,
weak_factory_.GetWeakPtr()));
weak_factory_.GetWeakPtr(), preprocessed_output));
}
void ResultLoader::OnSimpleURLLoaderComplete(
const PreprocessedOutput& preprocessed_output,
std::unique_ptr<std::string> response_body) {
base::TimeDelta duration = base::TimeTicks::Now() - fetch_start_time_;
......@@ -95,7 +97,7 @@ void ResultLoader::OnSimpleURLLoaderComplete(
return;
}
ProcessResponse(std::move(response_body),
ProcessResponse(preprocessed_output, std::move(response_body),
base::BindOnce(&ResultLoader::OnResultParserComplete,
weak_factory_.GetWeakPtr()));
}
......
......@@ -83,7 +83,8 @@ class ResultLoader {
BuildRequestCallback callback) const = 0;
// 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;
private:
......@@ -92,9 +93,11 @@ class ResultLoader {
ResultLoaderDelegate* const delegate_;
void OnBuildRequestComplete(
const PreprocessedOutput& preprocessed_output,
std::unique_ptr<network::ResourceRequest> resource_request,
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);
// Time when the query is issued.
......
......@@ -87,6 +87,7 @@ void SearchResultLoader::BuildRequest(
}
void SearchResultLoader::ProcessResponse(
const PreprocessedOutput& preprocessed_output,
std::unique_ptr<std::string> response_body,
ResponseParserCallback complete_callback) {
search_response_parser_ =
......
......@@ -33,7 +33,8 @@ class SearchResultLoader : public ResultLoader {
// ResultLoader:
void BuildRequest(const PreprocessedOutput& preprocessed_output,
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;
private:
......
......@@ -23,14 +23,16 @@ TranslationResponseParser::~TranslationResponseParser() {
}
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(
response_body->c_str(),
base::BindOnce(&TranslationResponseParser::OnJsonParsed,
weak_factory_.GetWeakPtr()));
weak_factory_.GetWeakPtr(), title_text));
}
void TranslationResponseParser::OnJsonParsed(
const std::string& title_text,
data_decoder::DataDecoder::ValueOrError result) {
DCHECK(complete_callback_);
......@@ -60,6 +62,8 @@ void TranslationResponseParser::OnJsonParsed(
auto quick_answer = std::make_unique<QuickAnswer>();
quick_answer->result_type = ResultType::kTranslationResult;
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(
std::make_unique<QuickAnswerResultText>(*translated_text));
......
......@@ -35,10 +35,12 @@ class TranslationResponseParser {
delete;
// 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:
void OnJsonParsed(data_decoder::DataDecoder::ValueOrError result);
void OnJsonParsed(const std::string& title_text,
data_decoder::DataDecoder::ValueOrError result);
TranslationResponseParserCallback complete_callback_;
......
......@@ -64,11 +64,13 @@ TEST_F(TranslationResponseParserTest, ProcessResponseSuccess) {
}
}
)";
constexpr char kTranslationTitle[] = "testo tradotto · Italian";
translation_response_parser_->ProcessResponse(
std::make_unique<std::string>(kTranslationResponse));
std::make_unique<std::string>(kTranslationResponse), kTranslationTitle);
WaitForResponse();
EXPECT_TRUE(quick_answer_);
EXPECT_EQ("translated text", quick_answer_->primary_answer);
EXPECT_EQ(kTranslationTitle, quick_answer_->secondary_answer);
EXPECT_EQ(ResultType::kTranslationResult, quick_answer_->result_type);
}
......@@ -77,14 +79,14 @@ TEST_F(TranslationResponseParserTest, ProcessResponseNoResults) {
{}
)";
translation_response_parser_->ProcessResponse(
std::make_unique<std::string>(kTranslationResponse));
std::make_unique<std::string>(kTranslationResponse), std::string());
WaitForResponse();
EXPECT_FALSE(quick_answer_);
}
TEST_F(TranslationResponseParserTest, ProcessResponseInvalidResponse) {
translation_response_parser_->ProcessResponse(
std::make_unique<std::string>("results {}"));
std::make_unique<std::string>("results {}"), std::string());
WaitForResponse();
EXPECT_FALSE(quick_answer_);
}
......
......@@ -10,10 +10,13 @@
#include "base/json/json_writer.h"
#include "chromeos/components/quick_answers/quick_answers_model.h"
#include "chromeos/services/assistant/public/shared/constants.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "net/base/escape.h"
#include "net/base/url_util.h"
#include "services/network/public/cpp/resource_request.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"
namespace chromeos {
......@@ -40,6 +43,18 @@ constexpr base::StringPiece kQueryKey = "q";
constexpr base::StringPiece kSourceLanguageKey = "source";
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) {
Value payload(Value::Type::DICTIONARY);
......@@ -74,11 +89,14 @@ void TranslationResultLoader::BuildRequest(
}
void TranslationResultLoader::ProcessResponse(
const PreprocessedOutput& preprocessed_output,
std::unique_ptr<std::string> response_body,
ResponseParserCallback complete_callback) {
translation_response_parser_ =
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(
......
......@@ -33,7 +33,8 @@ class TranslationResultLoader : public ResultLoader {
// ResultLoader:
void BuildRequest(const PreprocessedOutput& preprocessed_output,
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;
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