# Any copyright is dedicated to the Public Domain. # http://creativecommons.org/publicdomain/zero/1.0/ import fluent.syntax.ast as FTL from fluent.migrate.transforms import COPY_PATTERN, FluentSource, Transform # Locales that use the en-US accesskey letters across Firefox rather than # localizing them. For these, the migration sets the en-US accesskey; for every # other locale, ACCESSKEY_FROM_LABEL picks a letter from the translated label. 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" class ACCESSKEY_FROM_LABEL(FluentSource): """Pick an accesskey from the text of a Fluent label. If `preferred`, a (path, key) pair, holds a letter that occurs in the label, that letter wins, so the item keeps the accesskey the locale already uses for the same command elsewhere. A locale without that string falls back. Otherwise pick a character that is neither narrow nor has a descender. 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, preferred=None): super().__init__(path, key) self.reference_path = reference_path self.reference_key = reference_key self.preferred = preferred def english_accesskey(self, ctx): msg_key, _, attr_key = self.reference_key.partition(".") for entry in ctx.reference_resources[self.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"{self.reference_key} not found in {self.reference_path}") @staticmethod def text_of(pattern): return "".join( element.value for element in pattern.elements if isinstance(element, FTL.TextElement) ) def preferred_accesskey(self, ctx): path, key = self.preferred if path not in ctx.localization_resources: ctx.maybe_add_localization(path) if path not in ctx.localization_resources: return None pattern = ctx.get_fluent_source_pattern(path, key) return self.text_of(pattern).strip() if pattern else None def __call__(self, ctx): if ctx.locale in EN_ACCESSKEY_LOCALES: return self.english_accesskey(ctx) label = self.text_of(super().__call__(ctx)) if self.preferred: key = self.preferred_accesskey(ctx) if key and len(key) == 1 and key.lower() in label.lower(): return Transform.pattern_of(FTL.TextElement(key)) 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 self.english_accesskey(ctx) PATH = "browser/browser/sidebar.ftl" PLACES = "browser/browser/places.ftl" CONTEXT = "browser/browser/browserContext.ftl" SYNCED_TABS = "browser/browser/syncedTabs.ftl" TEXT_ACTIONS = "toolkit/toolkit/global/textActions.ftl" # New message id, old message id, and the (file, accesskey) holding the # locale's existing accesskey for the same command, if there is one. MESSAGES = [ ( "sidebar-context-menu-open-in-tab2", "sidebar-context-menu-open-in-tab", (PLACES, "places-open-in-tab.accesskey"), ), ( "sidebar-context-menu-open-in-container-tab2", "sidebar-context-menu-open-in-container-tab", (PLACES, "places-open-in-container-tab.accesskey"), ), ( "sidebar-context-menu-open-in-window2", "sidebar-context-menu-open-in-window", (PLACES, "places-open-in-window.accesskey"), ), ( "sidebar-context-menu-open-in-private-window2", "sidebar-context-menu-open-in-private-window", (SYNCED_TABS, "synced-tabs-context-open-in-private-window.accesskey"), ), ("sidebar-context-menu-forget-site2", "sidebar-context-menu-forget-site", None), ( "sidebar-context-menu-bookmark-tab2", "sidebar-context-menu-bookmark-tab", (SYNCED_TABS, "synced-tabs-context-bookmark.accesskey"), ), ( "sidebar-context-menu-copy-link2", "sidebar-context-menu-copy-link", (CONTEXT, "main-context-menu-copy-link-simple.accesskey"), ), ( "sidebar-context-menu-close-remote-tab2", "sidebar-context-menu-close-remote-tab", None, ), ( "sidebar-history-context-menu-delete-page-3", "sidebar-history-context-menu-delete-page-2", (PLACES, "places-delete-page.accesskey"), ), ( "sidebar-history-context-menu-bookmark-page2", "sidebar-history-context-menu-bookmark-page", (PLACES, "places-create-bookmark.accesskey"), ), ( "sidebar-history-context-menu-delete-pages2", "sidebar-history-context-menu-delete-pages", (PLACES, "places-delete-page.accesskey"), ), ( "sidebar-history-context-menu-delete2", "sidebar-history-context-menu-delete", (TEXT_ACTIONS, "text-action-delete.accesskey"), ), ( "sidebar-bookmarks-context-menu-edit-bookmark2", "sidebar-bookmarks-context-menu-edit-bookmark", (PLACES, "places-edit-bookmark.accesskey"), ), ( "sidebar-bookmarks-context-menu-delete-bookmark2", "sidebar-bookmarks-context-menu-delete-bookmark", (PLACES, "places-delete-bookmark.accesskey"), ), ( "sidebar-bookmarks-context-menu-delete-separator2", "sidebar-bookmarks-context-menu-delete-separator", (TEXT_ACTIONS, "text-action-delete.accesskey"), ), ("sidebar-opentabs-context-close-tab2", "sidebar-opentabs-context-close-tab", None), ( "sidebar-opentabs-sort-option-order2", "sidebar-opentabs-sort-option-order", None, ), ( "sidebar-opentabs-sort-option-recency2", "sidebar-opentabs-sort-option-recency", None, ), ] def message(new_id, old_id, preferred): return FTL.Message( id=FTL.Identifier(new_id), attributes=[ FTL.Attribute( id=FTL.Identifier("label"), value=COPY_PATTERN(PATH, f"{old_id}.label"), ), FTL.Attribute( id=FTL.Identifier("accesskey"), value=ACCESSKEY_FROM_LABEL( PATH, f"{old_id}.label", PATH, f"{new_id}.accesskey", preferred, ), ), ], ) def migrate(ctx): """Bug 2073894 - Give the sidebar context menu items accesskeys, part {index}.""" ctx.add_transforms(PATH, PATH, [message(*m) for m in MESSAGES])