Skip to content

Commit

Permalink
Bug 1749435 - Use Fluent to localize bookmarks.html at run-time rat…
Browse files Browse the repository at this point in the history
…her than build-time. r=flod,mak,fluent-reviewers

This modernizes an old part of the build system to not require
build-time localization at all.  That's generally preferable.

The most significant changes to the in-product functionality is to
make import localize HTML so that we can use Fluent's `data-l10n-id`.

The locale used is the user's current locale.  This is different than
the existing approach, which always uses the build-time (repack)
locale.  I believe this is a strictly superior user experience and it
may lead to future improvements where-in the default bookmarks become
truly dynamic and vary with the user's chosen locale rather than being
point-in-time decisions.

I tried to restrict these changes to only applen when we import the
default bookmarks, but I think the various layers of flags no longer
achieve this restriction in practice and the formatting and
localization will apply to all imported `bookmarks.html` files.  Since
we don't anticipate (nor ourselves write) these new things in
(respectively, to) `bookmarks.html`, and the file is already
user-controlled, I don't think this exposes any meaningful change in
functionality (or in security surface).

Some notes:

1) There's no migration of `.inc` -> `.ftl` because this is the lone
   `.inc` file.

2) I elected to prefix all strings with `default-bookmarks-`, since
   the existing names were very short and likely to collide (now or in
   the future).

3) I elected to change the HTML file name for easier searching.

4) Since the `default-bookmarks.html` file is product-specific and the
   existing tests are in `toolkit/`, I elected to not test the file
   directly in automation.

5) We removed the explicit locale (or equivalent `%LOCALE%`) since
   Mozilla properties will redirect to the appropriate language
   automatically.

Differential Revision: https://phabricator.services.mozilla.com/D135816
  • Loading branch information
ncalexan committed Jan 19, 2022
1 parent 096bfd3 commit 98a644b
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 155 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#filter substitution
#include @BOOKMARKS_INCLUDE_PATH@

#define mozilla_icon data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16'%3E %3Cpath d='M0 0h16v16H0z'/%3E %3Cpath d='M13.994 10.356H15V12h-3.171V7.741c0-1.308-.435-1.81-1.29-1.81-1.04 0-1.46.737-1.46 1.8v2.63h1.006V12H6.918V7.741c0-1.308-.435-1.81-1.291-1.81-1.039 0-1.459.737-1.459 1.8v2.63h1.441V12H1v-1.644h1.006V6.079H1V4.435h3.168v1.139a2.507 2.507 0 0 1 2.3-1.29A2.452 2.452 0 0 1 8.931 5.91 2.535 2.535 0 0 1 11.4 4.284 2.448 2.448 0 0 1 14 6.9v3.458z' fill='%23fff'/%3E %3C/svg%3E

Expand All @@ -14,36 +13,47 @@
- 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/. -->
<!DOCTYPE NETSCAPE-Bookmark-file-1>
<meta charset="UTF-8">
<title>@bookmarks_title@</title>
<h1>@bookmarks_heading@</h1>
<html>
<head>
<meta charset="UTF-8">
<!-- These localization links are not automatically applied to any XHR
response body and must be applied manually as well. They are included
so that viewing the file directly shows the results. -->
<link rel="localization" href="branding/brand.ftl"/>
<link rel="localization" href="browser/profile/default-bookmarks.ftl"/>
<title data-l10n-id="default-bookmarks-title">default-bookmarks-title</title>
</head>
<body>
<h1 data-l10n-id="default-bookmarks-heading">default-bookmarks-heading</h1>

<dl><p>
<dt><h3 personal_toolbar_folder="true">@bookmarks_toolbarfolder@</h3></dt>
<dd>@bookmarks_toolbarfolder_description@
<dt><h3 personal_toolbar_folder="true" data-l10n-id="default-bookmarks-toolbarfolder">default-bookmarks-toolbarfolder</h3></dt>
<dd data-l10n-id="default-bookmarks-toolbarfolder-description">default-bookmarks-toolbarfolder-description</dd>
#ifndef NIGHTLY_BUILD
<dl>
<p><dt><a href="https://www.mozilla.org/@AB_CD@/firefox/central/" icon="@firefox_icon@">@getting_started@</a></dt>
</dl>
<p><dt><h3>@firefox_heading@</h3></dt>
<dl><p>
<dt><a href="https://support.mozilla.org/@AB_CD@/products/firefox" icon="@firefox_icon@">@firefox_get_help@</a>
<dt><a href="https://support.mozilla.org/@AB_CD@/kb/customize-firefox-controls-buttons-and-toolbars?utm_source=firefox-browser&utm_medium=default-bookmarks&utm_campaign=customize" icon="@firefox_icon@">@firefox_customize@</a>
<dt><a href="https://www.mozilla.org/@AB_CD@/contribute/" icon="@mozilla_icon@">@firefox_community@</a>
<dt><a href="https://www.mozilla.org/@AB_CD@/about/" icon="@mozilla_icon@">@firefox_about@</a>
</dl>
<dt><a href="https://www.mozilla.org/firefox/central/" icon="@firefox_icon@" data-l10n-id="default-bookmarks-getting-started">default-bookmarks-getting-started</a></dt>
</dl><p>
<dt><h3 data-l10n-id="default-bookmarks-firefox-heading">default-bookmarks-firefox-heading</h3></dt>
<dl><p>
<dt><a href="https://support.mozilla.org/products/firefox" icon="@firefox_icon@" data-l10n-id="default-bookmarks-firefox-get-help">default-bookmarks-firefox-get-help</a></dt>
<dt><a href="https://support.mozilla.org/kb/customize-firefox-controls-buttons-and-toolbars?utm_source=firefox-browser&utm_medium=default-bookmarks&utm_campaign=customize" icon="@firefox_icon@" data-l10n-id="default-bookmarks-firefox-customize">default-bookmarks-firefox-customize</a></dt>
<dt><a href="https://www.mozilla.org/contribute/" icon="@mozilla_icon@" data-l10n-id="default-bookmarks-firefox-community">default-bookmarks-firefox-community</a></dt>
<dt><a href="https://www.mozilla.org/about/" icon="@mozilla_icon@" data-l10n-id="default-bookmarks-firefox-about">default-bookmarks-firefox-about</a></dt>
</dl><p>
#else
<dl>
<p><dt><a href="https://www.mozilla.org/@AB_CD@/contribute/" icon="@mozilla_icon@">@firefox_community@</a>
</dl>
<p><dt><h3>@nightly_heading@</h3></dt>
<dl><p>
<dt><a href="https://blog.nightly.mozilla.org/" icon="@mozilla_icon@">@nightly_blog@</a>
<dt><a href="https://bugzilla.mozilla.org/" icon="@bugzilla_icon@" shortcuturl="bz">@bugzilla@</a>
<dt><a href="https://developer.mozilla.org/" icon="@mdn_icon@" shortcuturl="mdn">@mdn@</a>
<dt><a href="https://addons.mozilla.org/@AB_CD@/firefox/addon/nightly-tester-tools/" icon="@addon_icon@">@nightly_tester_tools@</a>
<dt><a href="about:crashes" icon="@mozilla_icon@">@crashes@</a>
<dt><a href="https://planet.mozilla.org/" icon="@mozilla_icon@">@planet@</a>
</dl>
<dt><a href="https://www.mozilla.org/contribute/" icon="@mozilla_icon@" data-l10n-id="default-bookmarks-firefox-community">default-bookmarks-firefox-community</a></dt>
</dl><p>
<dt><h3 data-l10n-id="default-bookmarks-nightly-heading">default-bookmarks-nightly-heading</h3></dt>
<dl><p>
<dt><a href="https://blog.nightly.mozilla.org/" icon="@mozilla_icon@" data-l10n-id="default-bookmarks-nightly-blog">default-bookmarks-nightly-blog</a></dt>
<dt><a href="https://bugzilla.mozilla.org/" icon="@bugzilla_icon@" shortcuturl="bz" data-l10n-id="default-bookmarks-bugzilla">default-bookmarks-bugzilla</a></dt>
<dt><a href="https://developer.mozilla.org/" icon="@mdn_icon@" shortcuturl="mdn" data-l10n-id="default-bookmarks-mdn">default-bookmarks-mdn</a></dt>
<dt><a href="https://addons.mozilla.org/firefox/addon/nightly-tester-tools/" icon="@addon_icon@" data-l10n-id="default-bookmarks-nightly-tester-tools">default-bookmarks-nightly-tester-tools</a></dt>
<dt><a href="about:crashes" icon="@mozilla_icon@" data-l10n-id="default-bookmarks-crashes">default-bookmarks-crashes</a></dt>
<dt><a href="https://planet.mozilla.org/" icon="@mozilla_icon@" data-l10n-id="default-bookmarks-planet">default-bookmarks-planet</a></dt>
</dl><p>
#endif
</dl>
</body>
</html>
1 change: 1 addition & 0 deletions browser/base/jar.mn
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ browser.jar:
content/browser/upgradeDialog.js (content/upgradeDialog.js)
content/browser/spotlight.html (content/spotlight.html)
content/browser/spotlight.js (content/spotlight.js)
* content/browser/default-bookmarks.html (content/default-bookmarks.html)

% override chrome://global/content/netError.xhtml chrome://browser/content/certerror/aboutNetError.xhtml

Expand Down
5 changes: 3 additions & 2 deletions browser/components/BrowserGlue.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -3223,8 +3223,9 @@ BrowserGlue.prototype = {
// An import operation is about to run.
let bookmarksUrl = null;
if (restoreDefaultBookmarks) {
// User wants to restore bookmarks.html file from default profile folder
bookmarksUrl = "chrome://browser/locale/bookmarks.html";
// User wants to restore the default set of bookmarks shipped with the
// browser, those that new profiles start with.
bookmarksUrl = "chrome://browser/content/default-bookmarks.html";
} else if (await IOUtils.exists(BookmarkHTMLUtils.defaultPath)) {
bookmarksUrl = PathUtils.toFileURI(BookmarkHTMLUtils.defaultPath);
}
Expand Down
2 changes: 1 addition & 1 deletion browser/components/migration/MigrationUtils.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ var MigratorPrototype = {

// Import the default bookmarks. We ignore whether or not we succeed.
await BookmarkHTMLUtils.importFromURL(
"chrome://browser/locale/bookmarks.html",
"chrome://browser/content/default-bookmarks.html",
{
replace: true,
source: PlacesUtils.bookmarks.SOURCES.RESTORE_ON_STARTUP,
Expand Down
52 changes: 52 additions & 0 deletions browser/locales/en-US/browser/profile/default-bookmarks.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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/.

# This file intentionally uses hard-coded brand names instead of Fluent terms.
# This approach minimizes issues across multiple release channels and rebranded
# versions.

default-bookmarks-title = Bookmarks
default-bookmarks-heading = Bookmarks
default-bookmarks-toolbarfolder = Bookmarks Toolbar Folder
default-bookmarks-toolbarfolder-description = Add bookmarks to this folder to see them displayed on the Bookmarks Toolbar
# link title for https://www.mozilla.org/firefox/central/
default-bookmarks-getting-started = Getting Started
# Firefox links folder name
default-bookmarks-firefox-heading = Mozilla Firefox
# link title for https://www.mozilla.org/firefox/help/
default-bookmarks-firefox-get-help = Get Help
# link title for https://www.mozilla.org/firefox/customize/
default-bookmarks-firefox-customize = Customize Firefox
# link title for https://www.mozilla.org/contribute/
default-bookmarks-firefox-community = Get Involved
# link title for https://www.mozilla.org/about/
default-bookmarks-firefox-about = About Us
# Firefox Nightly links folder name
default-bookmarks-nightly-heading = Firefox Nightly Resources
# Nightly builds only, link title for https://blog.nightly.mozilla.org/
default-bookmarks-nightly-blog = Firefox Nightly blog
# Nightly builds only, link title for https://bugzilla.mozilla.org/
default-bookmarks-bugzilla = Mozilla Bug Tracker
# Nightly builds only, link title for https://developer.mozilla.org/
default-bookmarks-mdn = Mozilla Developer Network
# Nightly builds only, link title for https://addons.mozilla.org/firefox/addon/nightly-tester-tools/
default-bookmarks-nightly-tester-tools = Nightly Tester Tools
# Nightly builds only, link title for about:crashes
default-bookmarks-crashes = All your crashes
# Nightly builds only, link title for https://planet.mozilla.org/
default-bookmarks-planet = Planet Mozilla
68 changes: 0 additions & 68 deletions browser/locales/en-US/profile/bookmarks.inc

This file was deleted.

45 changes: 0 additions & 45 deletions browser/locales/generate_bookmarks.py

This file was deleted.

3 changes: 0 additions & 3 deletions browser/locales/jar.mn
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

@[email protected]:
% locale browser @AB_CD@ %locale/browser/
# bookmarks.html is produced by LOCALIZED_GENERATED_FILES.
locale/browser/bookmarks.html (bookmarks.html)

locale/browser/accounts.properties (%chrome/browser/accounts.properties)
locale/browser/browser.properties (%chrome/browser/browser.properties)
locale/browser/customizableui/customizableWidgets.properties (%chrome/browser/customizableui/customizableWidgets.properties)
Expand Down
10 changes: 0 additions & 10 deletions browser/locales/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ if CONFIG["MOZ_UPDATER"]:
# work.
LOCALIZED_FILES[".."] += ["!updater.ini"]

LOCALIZED_GENERATED_FILES += ["bookmarks.html"]
bookmarks = LOCALIZED_GENERATED_FILES["bookmarks.html"]
bookmarks.script = "generate_bookmarks.py"
bookmarks.inputs = [
# This input will not be considered for localization.
"generic/profile/bookmarks.html.in",
# The `locales/en-US/` will be rewritten to the locale-specific path.
"en-US/profile/bookmarks.inc",
]

with Files("**"):
BUG_COMPONENT = ("Firefox Build System", "General")

Expand Down
19 changes: 19 additions & 0 deletions toolkit/components/places/BookmarkHTMLUtils.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,25 @@ BookmarkImporter.prototype = {
*/
async importFromURL(href) {
let data = await fetchData(href);

if (this._isImportDefaults && data) {
// Localize default bookmarks. Find rel="localization" links and manually
// localize using them.
let hrefs = [];
let links = data.head.querySelectorAll("link[rel='localization']");
for (let link of links) {
if (link.getAttribute("href")) {
// We need the text, not the fully qualified URL, so we use `getAttribute`.
hrefs.push(link.getAttribute("href"));
}
}

if (hrefs.length) {
let domLoc = new DOMLocalization(hrefs);
await domLoc.translateFragment(data.body);
}
}

this._walkTreeForImport(data);
await this._importBookmarks();
},
Expand Down
21 changes: 21 additions & 0 deletions toolkit/components/places/tests/unit/bookmarks_html_localized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file.
It will be read and overwritten.
DO NOT EDIT! -->
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<LINK REL="localization" HREF="bookmarks_html_localized.ftl">
</HEAD>
<BODY>
<H1 LAST_MODIFIED="1177541029">Bookmarks</H1>

<DL><p>
<DT><H3 ID="rdf:#$ZvPhC3" data-l10n-id="bookmarks-html-localized-folder">bookmarks-html-localized-folder</H3>
<DL><p>
<DT><A HREF="http://www.mozilla.com/firefox/help/" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dPFpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAEEAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIGTDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8vZswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJFPcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwTAR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg==" ID="rdf:#$22iCK1" data-l10n-id="bookmarks-html-localized-bookmark">bookmarks-html-localized-bookmark</A>
</DL><p>
</DL><p>
</BODY>
</HTML>
Loading

0 comments on commit 98a644b

Please sign in to comment.