Source code
Revision control
Copy as Markdown
Other Tools
# Any copyright is dedicated to the Public Domain.
import fluent.syntax.ast as FTL
from fluent.migrate.helpers import VARIABLE_REFERENCE, transforms_from
from fluent.migrate.transforms import COPY, REPLACE, Transform
# Locales that reuse the English accesskeys instead of picking their own.
EN_ACCESSKEY_LOCALES = {
"as",
"bn",
"bn-BD",
"bn-IN",
"bo",
"bqi",
"brx",
"en-CA",
"en-GB",
"en-US",
"en-ZA",
"gn",
"gu-IN",
"hi-IN",
"ja",
"ja-JP-mac",
"km",
"kn",
"ko",
"ks",
"lb",
"lo",
"mai",
"mr",
"my",
"ne-NP",
"or",
"pa-IN",
"sat",
"sco",
"si",
"skr",
"ta",
"ta-LK",
"te",
"tl",
"trs",
"ur",
"vi",
"zh-CN",
"zh-TW",
}
DESCENDERS = "gjpqy"
NARROW = "ilI"
def english_accesskey(ctx, reference_path, reference_key):
msg_key, _, attr_key = reference_key.partition(".")
for entry in ctx.reference_resources[reference_path].body:
if isinstance(entry, FTL.Message) and entry.id.name == msg_key:
for attribute in entry.attributes:
if attribute.id.name == attr_key:
return attribute.value.clone()
raise KeyError(f"{reference_key} not found in {reference_path}")
class EN_OR_LOCALE_ACCESSKEY(COPY):
"""Pick the en-US accesskey for locales that use the English accesskey,
otherwise copy the accesskey the locale currently uses.
"""
def __init__(self, path, key, reference_path, reference_key):
super().__init__(path, key)
self.reference_path = reference_path
self.reference_key = reference_key
def __call__(self, ctx):
if ctx.locale in EN_ACCESSKEY_LOCALES:
return english_accesskey(ctx, self.reference_path, self.reference_key)
return super().__call__(ctx)
class ACCESSKEY_FROM_LABEL(COPY):
"""Pick an accesskey from the text of a translated label, preferring
characters that are neither narrow nor have descenders.
The English accesskey is read from `reference_key` in the reference FTL
file at `reference_path`.
"""
def __init__(self, path, key, reference_path, reference_key):
super().__init__(path, key)
self.reference_path = reference_path
self.reference_key = reference_key
def __call__(self, ctx):
if ctx.locale in EN_ACCESSKEY_LOCALES:
return english_accesskey(ctx, self.reference_path, self.reference_key)
pattern = super().__call__(ctx)
label = "".join(
element.value
for element in pattern.elements
if isinstance(element, FTL.TextElement)
)
candidates = [c for c in label if c.isalpha()]
for allowed in (
lambda c: c not in NARROW and c not in DESCENDERS,
lambda c: c not in DESCENDERS,
lambda c: True,
):
for c in candidates:
if allowed(c):
return Transform.pattern_of(FTL.TextElement(c))
return english_accesskey(ctx, self.reference_path, self.reference_key)
def migrate(ctx):
"""Bug 2073896 - Migrate the Network Monitor's request list context menu to Fluent, part {index}."""
source = "devtools/client/netmonitor.properties"
target = "devtools/client/netmonitor.ftl"
ctx.add_transforms(
target,
target,
transforms_from(
"""
network-request-list-copy-value =
.label = { COPY(from_path, "netmonitor.context.copyValue") }
.accesskey = { COPY(from_path, "netmonitor.context.copyValue.accesskey") }
network-request-list-copy-url =
.label = { COPY(from_path, "netmonitor.context.copyUrl") }
.accesskey = { COPY(from_path, "netmonitor.context.copyUrl.accesskey") }
network-request-list-copy-url-params =
.label = { COPY(from_path, "netmonitor.context.copyUrlParams") }
.accesskey = { COPY(from_path, "netmonitor.context.copyUrlParams.accesskey") }
network-request-list-copy-as-curl =
.label = { COPY(from_path, "netmonitor.context.copyAsCurl") }
.accesskey = { COPY(from_path, "netmonitor.context.copyAsCurl.accesskey") }
network-request-list-copy-as-powershell =
.label = { COPY(from_path, "netmonitor.context.copyAsPowerShell") }
.accesskey = { COPY(from_path, "netmonitor.context.copyAsPowerShell.accesskey") }
network-request-list-copy-as-fetch =
.label = { COPY(from_path, "netmonitor.context.copyAsFetch") }
.accesskey = { COPY(from_path, "netmonitor.context.copyAsFetch.accesskey") }
network-request-list-copy-request-headers =
.label = { COPY(from_path, "netmonitor.context.copyRequestHeaders") }
.accesskey = { COPY(from_path, "netmonitor.context.copyRequestHeaders.accesskey") }
network-request-list-copy-response =
.label = { COPY(from_path, "netmonitor.context.copyResponse") }
.accesskey = { COPY(from_path, "netmonitor.context.copyResponse.accesskey") }
network-request-list-copy-image-as-data-uri =
.label = { COPY(from_path, "netmonitor.context.copyImageAsDataUri") }
.accesskey = { COPY(from_path, "netmonitor.context.copyImageAsDataUri.accesskey") }
network-request-list-copy-all-as-har =
.label = { COPY(from_path, "netmonitor.context.copyAllAsHar") }
.accesskey = { COPY(from_path, "netmonitor.context.copyAllAsHar.accesskey") }
network-request-list-save-all-as-har =
.label = { COPY(from_path, "netmonitor.context.saveAllAsHar") }
.accesskey = { COPY(from_path, "netmonitor.context.saveAllAsHar.accesskey") }
network-request-list-save-response-as =
.label = { COPY(from_path, "netmonitor.context.saveResponseAs") }
.accesskey = { COPY(from_path, "netmonitor.context.saveResponseAs.accesskey") }
network-request-list-resend =
.label = { COPY(from_path, "netmonitor.context.resend.label") }
.accesskey = { COPY(from_path, "netmonitor.context.resend.accesskey") }
network-request-list-edit-and-resend =
.label = { COPY(from_path, "netmonitor.context.editAndResend") }
.accesskey = { COPY(from_path, "netmonitor.context.editAndResend.accesskey") }
network-request-list-set-override =
.label = { COPY(from_path, "netmonitor.context.setOverride") }
.accesskey = { COPY(from_path, "netmonitor.context.setOverride.accesskey") }
network-request-list-open-in-new-tab =
.label = { COPY(from_path, "netmonitor.context.newTab") }
.accesskey = { COPY(from_path, "netmonitor.context.newTab.accesskey") }
network-request-list-open-in-debugger =
.label = { COPY(from_path, "netmonitor.context.openInDebugger") }
.accesskey = { COPY(from_path, "netmonitor.context.openInDebugger.accesskey") }
network-request-list-open-in-style-editor =
.label = { COPY(from_path, "netmonitor.context.openInStyleEditor") }
.accesskey = { COPY(from_path, "netmonitor.context.openInStyleEditor.accesskey") }
network-request-list-start-performance-analysis =
.label = { COPY(from_path, "netmonitor.context.perfTools") }
.accesskey = { COPY(from_path, "netmonitor.context.perfTools.accesskey") }
network-request-list-use-as-fetch-in-console =
.label = { COPY(from_path, "netmonitor.context.useAsFetch") }
.accesskey = { COPY(from_path, "netmonitor.context.useAsFetch.accesskey") }
""",
from_path=source,
),
)
def message(id, label, accesskey):
return FTL.Message(
id=FTL.Identifier(id),
attributes=[
FTL.Attribute(id=FTL.Identifier("label"), value=label),
FTL.Attribute(id=FTL.Identifier("accesskey"), value=accesskey),
],
)
def changed_accesskey(id, key):
return EN_OR_LOCALE_ACCESSKEY(source, key, target, f"{id}.accesskey")
def new_accesskey(id, label_key):
return ACCESSKEY_FROM_LABEL(source, label_key, target, f"{id}.accesskey")
ctx.add_transforms(
target,
target,
[
message(
"network-request-list-copy-request-data",
REPLACE(
source,
"netmonitor.context.copyRequestData",
{"%1$S": VARIABLE_REFERENCE("method")},
),
COPY(source, "netmonitor.context.copyRequestData.accesskey"),
),
message(
"network-request-list-copy-as-curl-windows",
REPLACE(
source,
"netmonitor.context.copyAsCurl.win",
{"%1$S": COPY(source, "netmonitor.context.copyAsCurl")},
),
COPY(source, "netmonitor.context.copyAsCurl.win.accesskey"),
),
message(
"network-request-list-copy-as-curl-posix",
REPLACE(
source,
"netmonitor.context.copyAsCurl.posix",
{"%1$S": COPY(source, "netmonitor.context.copyAsCurl")},
),
changed_accesskey(
"network-request-list-copy-as-curl-posix",
"netmonitor.context.copyAsCurl.posix.accesskey",
),
),
message(
"network-request-list-copy-response-headers",
COPY(source, "netmonitor.context.copyResponseHeaders"),
changed_accesskey(
"network-request-list-copy-response-headers",
"netmonitor.context.copyResponseHeaders.accesskey",
),
),
message(
"network-request-list-save-as-har",
COPY(source, "netmonitor.context.saveAsHar"),
changed_accesskey(
"network-request-list-save-as-har",
"netmonitor.context.saveAsHar.accesskey",
),
),
message(
"network-request-list-block-url",
COPY(source, "netmonitor.context.blockURL"),
new_accesskey(
"network-request-list-block-url", "netmonitor.context.blockURL"
),
),
message(
"network-request-list-unblock-url",
COPY(source, "netmonitor.context.unblockURL"),
new_accesskey(
"network-request-list-unblock-url", "netmonitor.context.unblockURL"
),
),
message(
"network-request-list-remove-override",
COPY(source, "netmonitor.context.removeOverride"),
changed_accesskey(
"network-request-list-remove-override",
"netmonitor.context.removeOverride.accesskey",
),
),
],
)