Bug 1662538 - patch 2 - Localize paper names in the fallback paper size list used for Save to PDF. r=jwatt

Depends on D90073

Differential Revision: https://phabricator.services.mozilla.com/D90074
This commit is contained in:
Jonathan Kew 2020-09-17 20:05:16 +00:00
Родитель e206cf3d9e
Коммит 9a746a0d41
1 изменённых файлов: 46 добавлений и 32 удалений

Просмотреть файл

@ -6,9 +6,11 @@
#include "nsPrinterListBase.h"
#include "PrintBackgroundTask.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/intl/Localization.h"
#include "xpcpublic.h"
using mozilla::ErrorResult;
using mozilla::intl::Localization;
using PrinterInfo = nsPrinterListBase::PrinterInfo;
nsPrinterListBase::nsPrinterListBase() = default;
@ -95,46 +97,58 @@ Maybe<PrinterInfo> nsPrinterListBase::NamedOrDefaultPrinter(
NS_IMETHODIMP nsPrinterListBase::GetFallbackPaperList(JSContext* aCx,
Promise** aResult) {
ErrorResult rv;
RefPtr<Promise> promise = Promise::Create(xpc::CurrentNativeGlobal(aCx), rv);
if (MOZ_UNLIKELY(rv.Failed())) {
*aResult = nullptr;
return rv.StealNSResult();
}
promise->MaybeResolve(FallbackPaperList());
promise.forget(aResult);
return NS_OK;
}
nsTArray<RefPtr<nsPaper>> nsPrinterListBase::FallbackPaperList() const {
#define mm *72.0 / 25.4
#define in *72.0
// As paper IDs, we use the PWG standardized names.
// The names will be localized using strings in printUI.ftl.
static const mozilla::PaperInfo kPapers[] = {
{u"iso_a5"_ns, u"A5"_ns, {148 mm, 210 mm}, Some(MarginDouble{})},
{u"iso_a4"_ns, u"A4"_ns, {210 mm, 297 mm}, Some(MarginDouble{})},
{u"iso_a3"_ns, u"A3"_ns, {297 mm, 420 mm}, Some(MarginDouble{})},
{u"iso_b5"_ns, u"B5"_ns, {176 mm, 250 mm}, Some(MarginDouble{})},
{u"iso_b4"_ns, u"B4"_ns, {250 mm, 353 mm}, Some(MarginDouble{})},
{u"jis_b5"_ns, u"JIS-B5"_ns, {182 mm, 257 mm}, Some(MarginDouble{})},
{u"jis_b4"_ns, u"JIS-B4"_ns, {257 mm, 364 mm}, Some(MarginDouble{})},
{u"na_letter"_ns, u"US Letter"_ns, {8.5 in, 11 in}, Some(MarginDouble{})},
{u"na_legal"_ns, u"US Legal"_ns, {8.5 in, 14 in}, Some(MarginDouble{})},
{u"na_ledger"_ns, u"Tabloid"_ns, {11 in, 17 in}, Some(MarginDouble{})},
{u"iso_a5"_ns, u"a5"_ns, {148 mm, 210 mm}, Some(MarginDouble{})},
{u"iso_a4"_ns, u"a4"_ns, {210 mm, 297 mm}, Some(MarginDouble{})},
{u"iso_a3"_ns, u"a3"_ns, {297 mm, 420 mm}, Some(MarginDouble{})},
{u"iso_b5"_ns, u"b5"_ns, {176 mm, 250 mm}, Some(MarginDouble{})},
{u"iso_b4"_ns, u"b4"_ns, {250 mm, 353 mm}, Some(MarginDouble{})},
{u"jis_b5"_ns, u"jis-b5"_ns, {182 mm, 257 mm}, Some(MarginDouble{})},
{u"jis_b4"_ns, u"jis-b4"_ns, {257 mm, 364 mm}, Some(MarginDouble{})},
{u"na_letter"_ns, u"letter"_ns, {8.5 in, 11 in}, Some(MarginDouble{})},
{u"na_legal"_ns, u"legal"_ns, {8.5 in, 14 in}, Some(MarginDouble{})},
{u"na_ledger"_ns, u"tabloid"_ns, {11 in, 17 in}, Some(MarginDouble{})},
};
#undef mm
#undef in
// TODO:
// Replace the en-US strings above with lowercased, "US"-stripped versions
// as found in printUI.ftl, and call Fluent to get localized paper names.
// Consider whether any more sizes should be included (A0-A2?).
nsTArray<RefPtr<nsPaper>> result;
result.SetCapacity(mozilla::ArrayLength(kPapers));
for (const auto& info : kPapers) {
result.AppendElement(MakeRefPtr<nsPaper>(info));
ErrorResult rv;
nsCOMPtr<nsIGlobalObject> global = xpc::CurrentNativeGlobal(aCx);
RefPtr<Promise> promise = Promise::Create(global, rv);
if (MOZ_UNLIKELY(rv.Failed())) {
*aResult = nullptr;
return rv.StealNSResult();
}
return result;
// Apply localization to the names while constructing the nsIPapers, if
// available (otherwise leave them as the internal keys, which are at least
// somewhat recognizable).
RefPtr<Localization> l10n = Localization::Create(global, true, {});
l10n->AddResourceId(u"toolkit/printing/printUI.ftl"_ns);
nsTArray<RefPtr<nsPaper>> papers;
papers.SetCapacity(mozilla::ArrayLength(kPapers));
for (const auto& info : kPapers) {
nsAutoCString key("printui-paper-");
AppendUTF16toUTF8(info.mName, key);
nsAutoCString name;
l10n->FormatValueSync(aCx, key, {}, name, rv);
if (rv.Failed() || name.IsEmpty()) {
// No localized name found; use original key from the PaperInfo array.
papers.AppendElement(MakeRefPtr<nsPaper>(info));
} else {
// Use the localized paper name.
mozilla::PaperInfo locInfo = {info.mId, NS_ConvertUTF8toUTF16(name),
info.mSize, info.mUnwriteableMargin};
papers.AppendElement(MakeRefPtr<nsPaper>(locInfo));
}
}
promise->MaybeResolve(papers);
promise.forget(aResult);
return NS_OK;
}