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 transforms_from
from fluent.migrate.transforms import COPY, 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 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):
source = "devtools/shared/styleinspector.properties"
target = "devtools/client/inspector.ftl"
ctx.add_transforms(
target,
target,
transforms_from(
"""
style-inspector-context-menu-copy =
.label = { COPY(from_path, "styleinspector.contextmenu.copy") }
.accesskey = { COPY(from_path, "styleinspector.contextmenu.copy.accessKey") }
style-inspector-context-menu-copy-color =
.label = { COPY(from_path, "styleinspector.contextmenu.copyColor") }
.accesskey = { COPY(from_path, "styleinspector.contextmenu.copyColor.accessKey") }
style-inspector-context-menu-copy-url =
.label = { COPY(from_path, "styleinspector.contextmenu.copyUrl") }
.accesskey = { COPY(from_path, "styleinspector.contextmenu.copyUrl.accessKey") }
style-inspector-context-menu-copy-image-data-url =
.label = { COPY(from_path, "styleinspector.contextmenu.copyImageDataUrl") }
.accesskey = { COPY(from_path, "styleinspector.contextmenu.copyImageDataUrl.accessKey") }
style-inspector-context-menu-select-all =
.label = { COPY(from_path, "styleinspector.contextmenu.selectAll") }
.accesskey = { COPY(from_path, "styleinspector.contextmenu.selectAll.accessKey") }
style-inspector-context-menu-add-new-rule =
.label = { COPY(from_path, "styleinspector.contextmenu.addNewRule") }
.accesskey = { COPY(from_path, "styleinspector.contextmenu.addNewRule.accessKey") }
style-inspector-context-menu-toggle-original-sources =
.label = { COPY(from_path, "styleinspector.contextmenu.toggleOrigSources") }
.accesskey = { COPY(from_path, "styleinspector.contextmenu.toggleOrigSources.accessKey") }
""",
from_path=source,
),
)
def new_accesskey(id, label_key):
return ACCESSKEY_FROM_LABEL(source, label_key, target, f"{id}.accesskey")
ctx.add_transforms(
target,
target,
[
FTL.Message(
id=FTL.Identifier("style-inspector-context-menu-copy-location"),
attributes=[
FTL.Attribute(
id=FTL.Identifier("label"),
value=COPY(source, "styleinspector.contextmenu.copyLocation"),
),
FTL.Attribute(
id=FTL.Identifier("accesskey"),
value=new_accesskey(
"style-inspector-context-menu-copy-location",
"styleinspector.contextmenu.copyLocation",
),
),
],
),
FTL.Message(
id=FTL.Identifier("style-inspector-context-menu-copy-rule"),
attributes=[
FTL.Attribute(
id=FTL.Identifier("label"),
value=COPY(source, "styleinspector.contextmenu.copyRule"),
),
FTL.Attribute(
id=FTL.Identifier("accesskey"),
value=new_accesskey(
"style-inspector-context-menu-copy-rule",
"styleinspector.contextmenu.copyRule",
),
),
],
),
FTL.Message(
id=FTL.Identifier("style-inspector-context-menu-copy-declaration"),
attributes=[
FTL.Attribute(
id=FTL.Identifier("label"),
value=COPY(
source, "styleinspector.contextmenu.copyDeclaration"
),
),
FTL.Attribute(
id=FTL.Identifier("accesskey"),
value=new_accesskey(
"style-inspector-context-menu-copy-declaration",
"styleinspector.contextmenu.copyDeclaration",
),
),
],
),
FTL.Message(
id=FTL.Identifier("style-inspector-context-menu-copy-property-name"),
attributes=[
FTL.Attribute(
id=FTL.Identifier("label"),
value=COPY(
source, "styleinspector.contextmenu.copyPropertyName"
),
),
FTL.Attribute(
id=FTL.Identifier("accesskey"),
value=new_accesskey(
"style-inspector-context-menu-copy-property-name",
"styleinspector.contextmenu.copyPropertyName",
),
),
],
),
FTL.Message(
id=FTL.Identifier("style-inspector-context-menu-copy-property-value"),
attributes=[
FTL.Attribute(
id=FTL.Identifier("label"),
value=COPY(
source, "styleinspector.contextmenu.copyPropertyValue"
),
),
FTL.Attribute(
id=FTL.Identifier("accesskey"),
value=new_accesskey(
"style-inspector-context-menu-copy-property-value",
"styleinspector.contextmenu.copyPropertyValue",
),
),
],
),
FTL.Message(
id=FTL.Identifier("style-inspector-context-menu-copy-selector"),
attributes=[
FTL.Attribute(
id=FTL.Identifier("label"),
value=COPY(source, "styleinspector.contextmenu.copySelector"),
),
FTL.Attribute(
id=FTL.Identifier("accesskey"),
value=new_accesskey(
"style-inspector-context-menu-copy-selector",
"styleinspector.contextmenu.copySelector",
),
),
],
),
],
)