From f75f7b749426482f0fcc0a2b67023bdfacdb24ce Mon Sep 17 00:00:00 2001 From: Manuel Bucher Date: Thu, 31 Oct 2024 17:53:03 +0000 Subject: [PATCH] Bug 1733498 - Migrate necko errors l10n strings from properties to Fluent r=necko-reviewers,valentin,flod Differential Revision: https://phabricator.services.mozilla.com/D136612 --- netwerk/locales/en-US/necko.ftl | 18 +++++ netwerk/locales/en-US/necko.properties | 12 --- netwerk/locales/jar.mn | 2 + .../fluent_migrations/bug_1733498_necko.py | 63 +++++++++++++++ uriloader/base/nsDocLoader.cpp | 78 ++++++++++++++++++- uriloader/base/nsDocLoader.h | 6 ++ 6 files changed, 163 insertions(+), 16 deletions(-) create mode 100644 netwerk/locales/en-US/necko.ftl create mode 100644 python/l10n/fluent_migrations/bug_1733498_necko.py diff --git a/netwerk/locales/en-US/necko.ftl b/netwerk/locales/en-US/necko.ftl new file mode 100644 index 000000000000..fe3574ae4d9d --- /dev/null +++ b/netwerk/locales/en-US/necko.ftl @@ -0,0 +1,18 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +## Variables: +## $host (String): A website host address + +network-connection-status-looking-up = Looking up { $host }… +network-connection-status-connected = Connected to { $host }… +network-connection-status-sending-request = Sending request to { $host }… +network-connection-status-transferring-data = Transferring data from { $host }… +network-connection-status-connecting = Connecting to { $host }… +network-connection-status-read = Read { $host } +network-connection-status-wrote = Wrote { $host } +network-connection-status-waiting = Waiting for { $host }… +network-connection-status-looked-up = Looked up { $host }… +network-connection-status-tls-handshake = Performing a TLS handshake to { $host }… +network-connection-status-tls-handshake-finished = The TLS handshake finished for { $host }… diff --git a/netwerk/locales/en-US/necko.properties b/netwerk/locales/en-US/necko.properties index 3b64059f01da..827c45bbf9b6 100644 --- a/netwerk/locales/en-US/necko.properties +++ b/netwerk/locales/en-US/necko.properties @@ -2,18 +2,6 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -3=Looking up %1$S… -4=Connected to %1$S… -5=Sending request to %1$S… -6=Transferring data from %1$S… -7=Connecting to %1$S… -8=Read %1$S -9=Wrote %1$S -10=Waiting for %1$S… -11=Looked up %1$S… -12=Performing a TLS handshake to %1$S… -13=The TLS handshake finished for %1$S… - RepostFormData=This web page is being redirected to a new location. Would you like to resend the form data you have typed to the new location? # Directory listing strings diff --git a/netwerk/locales/jar.mn b/netwerk/locales/jar.mn index 95bafbb8a9d2..6bf42c37df9d 100644 --- a/netwerk/locales/jar.mn +++ b/netwerk/locales/jar.mn @@ -3,6 +3,8 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. +[localization] @AB_CD@.jar: + netwerk/necko.ftl (%necko.ftl) @AB_CD@.jar: % locale necko @AB_CD@ %locale/@AB_CD@/necko/ diff --git a/python/l10n/fluent_migrations/bug_1733498_necko.py b/python/l10n/fluent_migrations/bug_1733498_necko.py new file mode 100644 index 000000000000..27da342697fd --- /dev/null +++ b/python/l10n/fluent_migrations/bug_1733498_necko.py @@ -0,0 +1,63 @@ +# Any copyright is dedicated to the Public Domain. +# http://creativecommons.org/publicdomain/zero/1.0/ + +import fluent.syntax.ast as FTL +from fluent.migrate.helpers import VARIABLE_REFERENCE +from fluent.migrate.transforms import REPLACE + + +def migrate(ctx): + """Bug 1733498 - Convert necko.properties to Fluent, part {index}.""" + + source = "netwerk/necko.properties" + target = "netwerk/necko.ftl" + ctx.add_transforms( + target, + target, + [ + FTL.Message( + id=FTL.Identifier("network-connection-status-looking-up"), + value=REPLACE(source, "3", {"%1$S": VARIABLE_REFERENCE("host")}), + ), + FTL.Message( + id=FTL.Identifier("network-connection-status-connected"), + value=REPLACE(source, "4", {"%1$S": VARIABLE_REFERENCE("host")}), + ), + FTL.Message( + id=FTL.Identifier("network-connection-status-sending-request"), + value=REPLACE(source, "5", {"%1$S": VARIABLE_REFERENCE("host")}), + ), + FTL.Message( + id=FTL.Identifier("network-connection-status-transferring-data"), + value=REPLACE(source, "6", {"%1$S": VARIABLE_REFERENCE("host")}), + ), + FTL.Message( + id=FTL.Identifier("network-connection-status-connecting"), + value=REPLACE(source, "7", {"%1$S": VARIABLE_REFERENCE("host")}), + ), + FTL.Message( + id=FTL.Identifier("network-connection-status-read"), + value=REPLACE(source, "8", {"%1$S": VARIABLE_REFERENCE("host")}), + ), + FTL.Message( + id=FTL.Identifier("network-connection-status-wrote"), + value=REPLACE(source, "9", {"%1$S": VARIABLE_REFERENCE("host")}), + ), + FTL.Message( + id=FTL.Identifier("network-connection-status-waiting"), + value=REPLACE(source, "10", {"%1$S": VARIABLE_REFERENCE("host")}), + ), + FTL.Message( + id=FTL.Identifier("network-connection-status-looked-up"), + value=REPLACE(source, "11", {"%1$S": VARIABLE_REFERENCE("host")}), + ), + FTL.Message( + id=FTL.Identifier("network-connection-status-tls-handshake"), + value=REPLACE(source, "12", {"%1$S": VARIABLE_REFERENCE("host")}), + ), + FTL.Message( + id=FTL.Identifier("network-connection-status-tls-handshake-finished"), + value=REPLACE(source, "13", {"%1$S": VARIABLE_REFERENCE("host")}), + ), + ], + ) diff --git a/uriloader/base/nsDocLoader.cpp b/uriloader/base/nsDocLoader.cpp index 01ca680039ed..e5cc386651e1 100644 --- a/uriloader/base/nsDocLoader.cpp +++ b/uriloader/base/nsDocLoader.cpp @@ -1201,11 +1201,11 @@ NS_IMETHODIMP nsDocLoader::OnStatus(nsIRequest* aRequest, nsresult aStatus, } } - nsCOMPtr sbs = - mozilla::components::StringBundle::Service(); - if (!sbs) return NS_ERROR_FAILURE; + nsAutoString host; + host.Append(aStatusArg); + nsAutoString msg; - nsresult rv = sbs->FormatStatusMessage(aStatus, aStatusArg, msg); + nsresult rv = FormatStatusMessage(aStatus, host, msg); if (NS_FAILED(rv)) return rv; // Keep around the message. In case a request finishes, we need to make sure @@ -1240,6 +1240,76 @@ void nsDocLoader::ClearInternalProgress() { mProgressStateFlags = nsIWebProgressListener::STATE_STOP; } +/* static */ +mozilla::Maybe nsDocLoader::StatusCodeToL10nId( + nsresult aStatus) { + switch (aStatus) { + case NS_NET_STATUS_WRITING: + return mozilla::Some("network-connection-status-wrote"_ns); + case NS_NET_STATUS_READING: + return mozilla::Some("network-connection-status-read"_ns); + case NS_NET_STATUS_RESOLVING_HOST: + return mozilla::Some("network-connection-status-looking-up"_ns); + case NS_NET_STATUS_RESOLVED_HOST: + return mozilla::Some("network-connection-status-looked-up"_ns); + case NS_NET_STATUS_CONNECTING_TO: + return mozilla::Some("network-connection-status-connecting"_ns); + case NS_NET_STATUS_CONNECTED_TO: + return mozilla::Some("network-connection-status-connected"_ns); + case NS_NET_STATUS_TLS_HANDSHAKE_STARTING: + return mozilla::Some("network-connection-status-tls-handshake"_ns); + case NS_NET_STATUS_TLS_HANDSHAKE_ENDED: + return mozilla::Some( + "network-connection-status-tls-handshake-finished"_ns); + case NS_NET_STATUS_SENDING_TO: + return mozilla::Some("network-connection-status-sending-request"_ns); + case NS_NET_STATUS_WAITING_FOR: + return mozilla::Some("network-connection-status-waiting"_ns); + case NS_NET_STATUS_RECEIVING_FROM: + return mozilla::Some("network-connection-status-transferring-data"_ns); + default: + return mozilla::Nothing(); + } +} + +nsresult nsDocLoader::FormatStatusMessage(nsresult aStatus, + const nsAString& aHost, + nsAString& aRetVal) { + auto l10nId = StatusCodeToL10nId(aStatus); + + if (!l10nId) { + return NS_ERROR_FAILURE; + } + + nsAutoCString RetVal; + ErrorResult rv; + auto l10nArgs = dom::Optional(); + l10nArgs.Construct(); + + auto dirArg = l10nArgs.Value().Entries().AppendElement(); + dirArg->mKey = "host"; + dirArg->mValue.SetValue().SetAsUTF8String().Assign( + NS_ConvertUTF16toUTF8(aHost)); + + // Handle mL10n (necko.ftl) on demand + if (!mL10n) { + nsTArray resIds = { + "netwerk/necko.ftl"_ns, + }; + mL10n = mozilla::intl::Localization::Create(resIds, true); + } + MOZ_LOG(gDocLoaderLog, LogLevel::Debug, + ("DocLoader:%p: FormatStatusMessage, [mL10n=%d]\n", this, !!mL10n)); + MOZ_RELEASE_ASSERT(mL10n); + + mL10n->FormatValueSync(*l10nId, l10nArgs, RetVal, rv); + aRetVal = NS_ConvertUTF8toUTF16(RetVal); + if (rv.Failed()) { + return rv.StealNSResult(); + } + return NS_OK; +} + /** * |_code| is executed for every listener matching |_flag| * |listener| should be used inside |_code| as the nsIWebProgressListener var. diff --git a/uriloader/base/nsDocLoader.h b/uriloader/base/nsDocLoader.h index e2ef3b4e77e8..54a13a5282a5 100644 --- a/uriloader/base/nsDocLoader.h +++ b/uriloader/base/nsDocLoader.h @@ -24,6 +24,7 @@ #include "nsCOMPtr.h" #include "PLDHashTable.h" #include "nsCycleCollectionParticipant.h" +#include "mozilla/intl/Localization.h" #include "mozilla/LinkedList.h" #include "mozilla/UniquePtr.h" @@ -362,6 +363,11 @@ class nsDocLoader : public nsIDocumentLoader, bool IsBlockingLoadEvent() const { return mIsLoadingDocument || mDocumentOpenedButNotLoaded; } + + RefPtr mL10n; + static mozilla::Maybe StatusCodeToL10nId(nsresult aStatus); + nsresult FormatStatusMessage(nsresult aStatus, const nsAString& aHost, + nsAString& aRetVal); }; NS_DEFINE_STATIC_IID_ACCESSOR(nsDocLoader, NS_THIS_DOCLOADER_IMPL_CID)