From 1656dcc38768040128405005a0cb5b3011f63e68 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 21 Aug 2024 17:15:32 +0200 Subject: [PATCH] Utilize Fluent to format numbers and dates in `PDFDocumentProperties` The `PDFDocumentProperties` dialog may not display correctly formatted data, especially in the GENERIC viewer, since it's using native methods[1] that depend on the *browser* locale instead of the viewer locale as intended. At the time when this dialog was introduced that was probably all we could easily do, but with Fluent we're able to improve things since it's got built-in support for formatting numbers and dates. Not only does this simplify the JavaScript code, but it also gives the localizer more fine-grained control of the desired output. Please find additional information here: - https://projectfluent.org/fluent/guide/builtins.html - https://projectfluent.org/fluent/guide/functions.html --- [1] `toLocaleString`, `toLocaleDateString`, and `toLocaleTimeString`. --- l10n/en-US/viewer.ftl | 17 ++++++++--------- l10n/sv-SE/viewer.ftl | 17 ++++++++--------- web/pdf_document_properties.js | 31 +++++++++++-------------------- 3 files changed, 27 insertions(+), 38 deletions(-) diff --git a/l10n/en-US/viewer.ftl b/l10n/en-US/viewer.ftl index c28352e692e2bf..cea54a28242ae0 100644 --- a/l10n/en-US/viewer.ftl +++ b/l10n/en-US/viewer.ftl @@ -113,14 +113,14 @@ pdfjs-document-properties-file-name = File name: pdfjs-document-properties-file-size = File size: # Variables: -# $size_kb (Number) - the PDF file size in kilobytes -# $size_b (Number) - the PDF file size in bytes -pdfjs-document-properties-kb = { $size_kb } KB ({ $size_b } bytes) +# $kb (Number) - the PDF file size in kilobytes +# $b (Number) - the PDF file size in bytes +pdfjs-document-properties-size-kb = { NUMBER($kb, maximumSignificantDigits: 3) } KB ({ $b } bytes) # Variables: -# $size_mb (Number) - the PDF file size in megabytes -# $size_b (Number) - the PDF file size in bytes -pdfjs-document-properties-mb = { $size_mb } MB ({ $size_b } bytes) +# $mb (Number) - the PDF file size in megabytes +# $b (Number) - the PDF file size in bytes +pdfjs-document-properties-size-mb = { NUMBER($mb, maximumSignificantDigits: 3) } MB ({ $b } bytes) pdfjs-document-properties-title = Title: pdfjs-document-properties-author = Author: @@ -130,9 +130,8 @@ pdfjs-document-properties-creation-date = Creation Date: pdfjs-document-properties-modification-date = Modification Date: # Variables: -# $date (Date) - the creation/modification date of the PDF file -# $time (Time) - the creation/modification time of the PDF file -pdfjs-document-properties-date-string = { $date }, { $time } +# $dateObj (Date) - the creation/modification date and time of the PDF file +pdfjs-document-properties-date-time-string = { DATETIME($dateObj, dateStyle: "short", timeStyle: "medium") } pdfjs-document-properties-creator = Creator: pdfjs-document-properties-producer = PDF Producer: diff --git a/l10n/sv-SE/viewer.ftl b/l10n/sv-SE/viewer.ftl index 1d258946a9f2ba..5b58de66b41144 100644 --- a/l10n/sv-SE/viewer.ftl +++ b/l10n/sv-SE/viewer.ftl @@ -105,13 +105,13 @@ pdfjs-document-properties-button-label = Dokumentegenskaper… pdfjs-document-properties-file-name = Filnamn: pdfjs-document-properties-file-size = Filstorlek: # Variables: -# $size_kb (Number) - the PDF file size in kilobytes -# $size_b (Number) - the PDF file size in bytes -pdfjs-document-properties-kb = { $size_kb } kB ({ $size_b } byte) +# $kb (Number) - the PDF file size in kilobytes +# $b (Number) - the PDF file size in bytes +pdfjs-document-properties-size-kb = { NUMBER($kb, maximumSignificantDigits: 3) } kB ({ $b } byte) # Variables: -# $size_mb (Number) - the PDF file size in megabytes -# $size_b (Number) - the PDF file size in bytes -pdfjs-document-properties-mb = { $size_mb } MB ({ $size_b } byte) +# $mb (Number) - the PDF file size in megabytes +# $b (Number) - the PDF file size in bytes +pdfjs-document-properties-size-mb = { NUMBER($mb, maximumSignificantDigits: 3) } MB ({ $b } byte) pdfjs-document-properties-title = Titel: pdfjs-document-properties-author = Författare: pdfjs-document-properties-subject = Ämne: @@ -119,9 +119,8 @@ pdfjs-document-properties-keywords = Nyckelord: pdfjs-document-properties-creation-date = Skapades: pdfjs-document-properties-modification-date = Ändrades: # Variables: -# $date (Date) - the creation/modification date of the PDF file -# $time (Time) - the creation/modification time of the PDF file -pdfjs-document-properties-date-string = { $date }, { $time } +# $dateObj (Date) - the creation/modification date and time of the PDF file +pdfjs-document-properties-date-time-string = { DATETIME($dateObj, dateStyle: "short", timeStyle: "medium") } pdfjs-document-properties-creator = Skapare: pdfjs-document-properties-producer = PDF-producent: pdfjs-document-properties-version = PDF-version: diff --git a/web/pdf_document_properties.js b/web/pdf_document_properties.js index 39255b24471a35..d250a9e62f10b6 100644 --- a/web/pdf_document_properties.js +++ b/web/pdf_document_properties.js @@ -239,17 +239,12 @@ class PDFDocumentProperties { return this.l10n.get(`pdfjs-document-properties-${id}`, args); } - async #parseFileSize(fileSize = 0) { - const kb = fileSize / 1024, + async #parseFileSize(b = 0) { + const kb = b / 1024, mb = kb / 1024; - if (!kb) { - return undefined; - } - return this.#getL10nStr(mb >= 1 ? "mb" : "kb", { - size_mb: mb >= 1 && (+mb.toPrecision(3)).toLocaleString(), - size_kb: mb < 1 && (+kb.toPrecision(3)).toLocaleString(), - size_b: fileSize.toLocaleString(), - }); + return kb + ? this.#getL10nStr(`size-${mb >= 1 ? "mb" : "kb"}`, { mb, kb, b }) + : undefined; } async #parsePageSize(pageSizeInches, pagesRotation) { @@ -330,8 +325,8 @@ class PDFDocumentProperties { return this.#getL10nStr( `page-size-dimension-${name ? "name-" : ""}string`, { - width: width.toLocaleString(), - height: height.toLocaleString(), + width, + height, unit, name, orientation, @@ -340,14 +335,10 @@ class PDFDocumentProperties { } async #parseDate(inputDate) { - const dateObject = PDFDateString.toDateObject(inputDate); - if (!dateObject) { - return undefined; - } - return this.#getL10nStr("date-string", { - date: dateObject.toLocaleDateString(), - time: dateObject.toLocaleTimeString(), - }); + const dateObj = PDFDateString.toDateObject(inputDate); + return dateObj + ? this.#getL10nStr("date-time-string", { dateObj }) + : undefined; } #parseLinearization(isLinearized) {