Commit 1fc7d7cf authored by Gavin Williams's avatar Gavin Williams Committed by Chromium LUCI CQ

scanning: Add plural string conversions to handler

Creates a function in ScanningHandler to centralize the logic for
localizing plural strings.

Bug: 1059779
Change-Id: I5cdd80ccf04e16945ae1a273941f846e1f39c75b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2584292
Commit-Queue: Gavin Williams <gavinwill@chromium.org>
Reviewed-by: default avatarJesse Schettler <jschettler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#835798}
parent 8623f961
...@@ -42,11 +42,19 @@ export function scanDoneSectionTest() { ...@@ -42,11 +42,19 @@ export function scanDoneSectionTest() {
test('pageNumberUpdatesTitleText', () => { test('pageNumberUpdatesTitleText', () => {
scanDoneSection.pageNumber = 1; scanDoneSection.pageNumber = 1;
return flushTasks()
.then(() => {
assertEquals( assertEquals(
'Scanned file saved!', scanDoneSection.$.title.textContent.trim()); 'Scanned file saved!',
scanDoneSection.$.title.textContent.trim());
scanDoneSection.pageNumber = 2; scanDoneSection.pageNumber = 2;
return flushTasks();
})
.then(() => {
assertEquals( assertEquals(
'Scanned files saved!', scanDoneSection.$.title.textContent.trim()); 'Scanned files saved!',
scanDoneSection.$.title.textContent.trim());
});
}); });
test('showFileLocation', () => { test('showFileLocation', () => {
......
...@@ -17,6 +17,7 @@ export class TestScanningBrowserProxy extends TestBrowserProxy { ...@@ -17,6 +17,7 @@ export class TestScanningBrowserProxy extends TestBrowserProxy {
'initialize', 'initialize',
'requestScanToLocation', 'requestScanToLocation',
'showFileInLocation', 'showFileInLocation',
'getPluralString',
]); ]);
/** @private {?SelectedPath} */ /** @private {?SelectedPath} */
...@@ -46,6 +47,16 @@ export class TestScanningBrowserProxy extends TestBrowserProxy { ...@@ -46,6 +47,16 @@ export class TestScanningBrowserProxy extends TestBrowserProxy {
return Promise.resolve(this.pathToFile_ === pathToFile); return Promise.resolve(this.pathToFile_ === pathToFile);
} }
/**
* @param {string} name
* @param {number} count
*/
getPluralString(name, count) {
this.methodCalled('getPluralString');
return Promise.resolve(
count === 1 ? 'Scanned file saved!' : 'Scanned files saved!');
}
/** @param {!SelectedPath} selectedPath */ /** @param {!SelectedPath} selectedPath */
setSelectedPath(selectedPath) { setSelectedPath(selectedPath) {
this.selectedPath_ = selectedPath; this.selectedPath_ = selectedPath;
......
...@@ -560,10 +560,9 @@ Try tapping the mic to ask me anything. ...@@ -560,10 +560,9 @@ Try tapping the mic to ask me anything.
Scanning page <ph name="PAGE_NUMBER">$1<ex>4</ex></ph> Scanning page <ph name="PAGE_NUMBER">$1<ex>4</ex></ph>
</message> </message>
<message name="IDS_SCANNING_APP_FILE_SAVED_TEXT" desc="The text displayed in the Scanning App when a scan job is completed and the file is saved."> <message name="IDS_SCANNING_APP_FILE_SAVED_TEXT" desc="The text displayed in the Scanning App when a scan job is completed and the file is saved.">
Scanned file saved! {COUNT, plural,
</message> =1 {Scanned file saved!}
<message name="IDS_SCANNING_APP_FILE_SAVED_TEXT_PLURAL" desc="The text displayed in the Scanning App when a scan job is completed and the files are saved."> other {Scanned files saved!}}
Scanned files saved!
</message> </message>
<message name="IDS_SCANNING_APP_DONE_BUTTON_TEXT" desc="The text displayed for the button the user clicks to return to the intial Scanning App page after a scan job is completed."> <message name="IDS_SCANNING_APP_DONE_BUTTON_TEXT" desc="The text displayed for the button the user clicks to return to the intial Scanning App page after a scan job is completed.">
Done Done
......
9c675fc4da82d9b94a2907160131715287febddf 707171624ee868fa8e08fe28f1d65e5d667c9224
\ No newline at end of file \ No newline at end of file
a7a1499711de836527f96f80f5a0fba609bd3637
\ No newline at end of file
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
</style> </style>
<iron-icon id="checkMarkIcon" icon="scanning:check-mark"></iron-icon> <iron-icon id="checkMarkIcon" icon="scanning:check-mark"></iron-icon>
<h1 id="title" role="alert"> <h1 id="title" role="alert">
[[getTitleText_(pageNumber)]] [[titleText_]]
</h1> </h1>
<div class="container"> <div class="container">
<span id="showFileLocationLabel" class="label" aria-hidden="true"> <span id="showFileLocationLabel" class="label" aria-hidden="true">
......
...@@ -30,10 +30,16 @@ Polymer({ ...@@ -30,10 +30,16 @@ Polymer({
properties: { properties: {
/** @type {number} */ /** @type {number} */
pageNumber: Number, pageNumber: {
type: Number,
observer: 'onPageNumberChange_',
},
/** @type {?mojoBase.mojom.FilePath} */ /** @type {?mojoBase.mojom.FilePath} */
lastScannedFilePath: Object, lastScannedFilePath: Object,
/** @private {string} */
titleText_: String,
}, },
/** @override */ /** @override */
...@@ -42,15 +48,6 @@ Polymer({ ...@@ -42,15 +48,6 @@ Polymer({
this.browserProxy_.initialize(); this.browserProxy_.initialize();
}, },
/**
* @return {string}
* @private
*/
getTitleText_() {
return this.i18n(
this.pageNumber > 1 ? 'fileSavedTextPlural' : 'fileSavedText');
},
/** @private */ /** @private */
onDoneClick_() { onDoneClick_() {
this.fire('done-click'); this.fire('done-click');
...@@ -66,4 +63,12 @@ Polymer({ ...@@ -66,4 +63,12 @@ Polymer({
} }
}); });
}, },
/** @private */
onPageNumberChange_() {
this.browserProxy_.getPluralString('fileSavedText', this.pageNumber)
.then(
/* @type {string} */ (pluralString) => this.titleText_ =
pluralString);
},
}); });
...@@ -35,6 +35,14 @@ export class ScanningBrowserProxy { ...@@ -35,6 +35,14 @@ export class ScanningBrowserProxy {
* @return {!Promise<boolean>} True if the file is found and Files app opens. * @return {!Promise<boolean>} True if the file is found and Files app opens.
*/ */
showFileInLocation(pathToFile) {} showFileInLocation(pathToFile) {}
/**
* Returns a localized, pluralized string for |name| based on |count|.
* @param {string} name
* @param {number} count
* @return {!Promise<string>}
*/
getPluralString(name, count) {}
} }
/** @implements {ScanningBrowserProxy} */ /** @implements {ScanningBrowserProxy} */
...@@ -53,6 +61,11 @@ export class ScanningBrowserProxyImpl { ...@@ -53,6 +61,11 @@ export class ScanningBrowserProxyImpl {
showFileInLocation(pathToFile) { showFileInLocation(pathToFile) {
return sendWithPromise('showFileInLocation', pathToFile); return sendWithPromise('showFileInLocation', pathToFile);
} }
/** @override */
getPluralString(name, count) {
return sendWithPromise('getPluralString', name, count);
}
} }
// The singleton instance_ can be replaced with a test version of this wrapper // The singleton instance_ can be replaced with a test version of this wrapper
......
...@@ -46,6 +46,11 @@ void ScanningHandler::RegisterMessages() { ...@@ -46,6 +46,11 @@ void ScanningHandler::RegisterMessages() {
"showFileInLocation", "showFileInLocation",
base::BindRepeating(&ScanningHandler::HandleShowFileInLocation, base::BindRepeating(&ScanningHandler::HandleShowFileInLocation,
base::Unretained(this))); base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getPluralString",
base::BindRepeating(&ScanningHandler::HandleGetPluralString,
base::Unretained(this)));
} }
void ScanningHandler::HandleInitialize(const base::ListValue* args) { void ScanningHandler::HandleInitialize(const base::ListValue* args) {
...@@ -101,6 +106,26 @@ void ScanningHandler::FileSelectionCanceled(void* params) { ...@@ -101,6 +106,26 @@ void ScanningHandler::FileSelectionCanceled(void* params) {
CreateSelectedPathValue(base::FilePath())); CreateSelectedPathValue(base::FilePath()));
} }
void ScanningHandler::AddStringToPluralMap(const std::string& name,
int string_id) {
string_id_map_[name] = string_id;
}
void ScanningHandler::HandleGetPluralString(const base::ListValue* args) {
if (!IsJavascriptAllowed())
return;
CHECK_EQ(3U, args->GetSize());
const std::string callback = args->GetList()[0].GetString();
const std::string name = args->GetList()[1].GetString();
const int count = args->GetList()[2].GetInt();
const base::string16 localized_string = l10n_util::GetPluralStringFUTF16(
string_id_map_.find(name)->second, count);
ResolveJavascriptCallback(base::Value(callback),
base::Value(localized_string));
}
// Uses the full filepath and the base directory (lowest level directory in the // Uses the full filepath and the base directory (lowest level directory in the
// filepath, used to display in the UI) to create a Value object to return to // filepath, used to display in the UI) to create a Value object to return to
// the Scanning UI. // the Scanning UI.
......
...@@ -58,6 +58,9 @@ class ScanningHandler : public content::WebUIMessageHandler, ...@@ -58,6 +58,9 @@ class ScanningHandler : public content::WebUIMessageHandler,
base::Value CreateSelectedPathValue(const base::FilePath& path); base::Value CreateSelectedPathValue(const base::FilePath& path);
// Adds to map of string IDs for pluralization.
void AddStringToPluralMap(const std::string& name, int id);
void SetWebUIForTest(content::WebUI* web_ui); void SetWebUIForTest(content::WebUI* web_ui);
private: private:
...@@ -71,6 +74,9 @@ class ScanningHandler : public content::WebUIMessageHandler, ...@@ -71,6 +74,9 @@ class ScanningHandler : public content::WebUIMessageHandler,
// Opens the Files app to the show the saved scan file. // Opens the Files app to the show the saved scan file.
void HandleShowFileInLocation(const base::ListValue* args); void HandleShowFileInLocation(const base::ListValue* args);
// Returns a localized, pluralized string.
void HandleGetPluralString(const base::ListValue* args);
SelectFilePolicyCreator select_file_policy_creator_; SelectFilePolicyCreator select_file_policy_creator_;
std::string scan_location_callback_id_; std::string scan_location_callback_id_;
...@@ -82,6 +88,8 @@ class ScanningHandler : public content::WebUIMessageHandler, ...@@ -82,6 +88,8 @@ class ScanningHandler : public content::WebUIMessageHandler,
// Opens Files app to the desired file location. // Opens Files app to the desired file location.
OpenFilesAppFunction open_files_app_fn_; OpenFilesAppFunction open_files_app_fn_;
std::map<std::string, int> string_id_map_;
}; };
} // namespace chromeos } // namespace chromeos
......
...@@ -63,8 +63,6 @@ void AddScanningAppStrings(content::WebUIDataSource* html_source) { ...@@ -63,8 +63,6 @@ void AddScanningAppStrings(content::WebUIDataSource* html_source) {
{"defaultSourceOptionText", IDS_SCANNING_APP_DEFAULT_SOURCE_OPTION_TEXT}, {"defaultSourceOptionText", IDS_SCANNING_APP_DEFAULT_SOURCE_OPTION_TEXT},
{"doneButtonText", IDS_SCANNING_APP_DONE_BUTTON_TEXT}, {"doneButtonText", IDS_SCANNING_APP_DONE_BUTTON_TEXT},
{"fileNotFoundToastText", IDS_SCANNING_APP_FILE_NOT_FOUND_TOAST_TEXT}, {"fileNotFoundToastText", IDS_SCANNING_APP_FILE_NOT_FOUND_TOAST_TEXT},
{"fileSavedText", IDS_SCANNING_APP_FILE_SAVED_TEXT},
{"fileSavedTextPlural", IDS_SCANNING_APP_FILE_SAVED_TEXT_PLURAL},
{"fileTypeDropdownLabel", IDS_SCANNING_APP_FILE_TYPE_DROPDOWN_LABEL}, {"fileTypeDropdownLabel", IDS_SCANNING_APP_FILE_TYPE_DROPDOWN_LABEL},
{"fitToScanAreaOptionText", {"fitToScanAreaOptionText",
IDS_SCANNING_APP_FIT_TO_SCAN_AREA_OPTION_TEXT}, IDS_SCANNING_APP_FIT_TO_SCAN_AREA_OPTION_TEXT},
...@@ -109,6 +107,14 @@ void AddScanningAppStrings(content::WebUIDataSource* html_source) { ...@@ -109,6 +107,14 @@ void AddScanningAppStrings(content::WebUIDataSource* html_source) {
html_source->UseStringsJs(); html_source->UseStringsJs();
} }
void AddScanningAppPluralStrings(ScanningHandler* handler) {
static constexpr webui::LocalizedString kLocalizedStrings[] = {
{"fileSavedText", IDS_SCANNING_APP_FILE_SAVED_TEXT}};
for (const auto& str : kLocalizedStrings)
handler->AddStringToPluralMap(str.name, str.id);
}
} // namespace } // namespace
ScanningUI::ScanningUI( ScanningUI::ScanningUI(
...@@ -138,9 +144,12 @@ ScanningUI::ScanningUI( ...@@ -138,9 +144,12 @@ ScanningUI::ScanningUI(
AddScanningAppStrings(html_source.get()); AddScanningAppStrings(html_source.get());
web_ui->AddMessageHandler(std::make_unique<ScanningHandler>( auto handler = std::make_unique<ScanningHandler>(
select_file_policy_creator, std::move(scanning_paths_provider), select_file_policy_creator, std::move(scanning_paths_provider),
open_files_app_fn)); open_files_app_fn);
AddScanningAppPluralStrings(handler.get());
web_ui->AddMessageHandler(std::move(handler));
content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(), content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(),
html_source.release()); html_source.release());
} }
......
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