Bug 1555450 - Remove locale-specific behavior when fully fluent (no longer using properties/json) r=k88hudson

Depends on D36764

Differential Revision: https://phabricator.services.mozilla.com/D36769

--HG--
rename : browser/components/newtab/prerendered/static/activity-stream-debug.html => browser/components/newtab/prerendered/activity-stream-debug.html
rename : browser/components/newtab/prerendered/locales/en-US/activity-stream-noscripts.html => browser/components/newtab/prerendered/activity-stream-noscripts.html
rename : browser/components/newtab/prerendered/locales/en-US/activity-stream.html => browser/components/newtab/prerendered/activity-stream.html
extra : moz-landing-system : lando
This commit is contained in:
Ed Lee 2019-07-03 23:49:07 +00:00
parent 9dab7f6b37
commit c863d3c77b
14 changed files with 23 additions and 214 deletions

View file

@ -50,7 +50,6 @@ browser/components/pocket/content/panels/js/vendor/**
# Kept in sync with browser/components/newtab/.eslintignore
browser/components/newtab/data/
browser/components/newtab/logs/
browser/components/newtab/prerendered/
browser/components/newtab/vendor/
# The only file in browser/locales/ is pre-processed.

2
.gitignore vendored
View file

@ -44,8 +44,6 @@ security/manager/.nss.checkout
/gecko.log
# Ignore newtab component build assets
browser/components/newtab/bin/prerender.js
browser/components/newtab/bin/prerender.js.map
browser/components/newtab/data/locales.json
browser/components/newtab/logs/

View file

@ -34,8 +34,6 @@ var gExceptionPaths = [
// https://github.com/mozilla/activity-stream/issues/3053
"resource://activity-stream/data/content/tippytop/images/",
// https://github.com/mozilla/activity-stream/issues/3758
"resource://activity-stream/prerendered/",
// browser/extensions/pdfjs/content/build/pdf.js#1999
"resource://pdf.js/web/images/",

View file

@ -1,4 +1,3 @@
data/
logs/
prerendered/
vendor/

View file

@ -14,13 +14,8 @@ ChromeUtils.defineModuleGetter(this, "AboutNewTab",
"resource:///modules/AboutNewTab.jsm");
const TOPIC_APP_QUIT = "quit-application-granted";
const TOPIC_LOCALES_CHANGE = "intl:app-locales-changed";
const TOPIC_CONTENT_DOCUMENT_INTERACTIVE = "content-document-interactive";
// Automated tests ensure packaged locales are in this list. Copied output of:
// https://github.com/mozilla/activity-stream/blob/master/bin/render-activity-stream-html.js
const ACTIVITY_STREAM_BCP47 = "en-US".split(" ");
const ABOUT_URL = "about:newtab";
const BASE_URL = "resource://activity-stream/";
const ACTIVITY_STREAM_PAGES = new Set(["home", "newtab", "welcome"]);
@ -35,7 +30,6 @@ const PREF_ACTIVITY_STREAM_DEBUG = "browser.newtabpage.activity-stream.debug";
function AboutNewTabService() {
Services.obs.addObserver(this, TOPIC_APP_QUIT);
Services.obs.addObserver(this, TOPIC_LOCALES_CHANGE);
Services.prefs.addObserver(PREF_SEPARATE_PRIVILEGEDABOUT_CONTENT_PROCESS, this);
if (!IS_RELEASE_OR_BETA) {
Services.prefs.addObserver(PREF_ACTIVITY_STREAM_DEBUG, this);
@ -71,7 +65,7 @@ function AboutNewTabService() {
*
* When the URL loaded is about:newtab, the default behavior, or when entered in the
* URL bar, the redirector is hit. The service is then called to return the
* appropriate activity stream url based on prefs and locales.
* appropriate activity stream url based on prefs.
*
* NOTE: "about:newtab" will always result in a default newtab page, and never an overridden URL.
*
@ -88,7 +82,6 @@ AboutNewTabService.prototype = {
_newTabURL: ABOUT_URL,
_activityStreamEnabled: false,
_activityStreamPath: "",
_activityStreamDebug: false,
_privilegedAboutContentProcess: false,
_overridden: false,
@ -105,11 +98,9 @@ AboutNewTabService.prototype = {
case "nsPref:changed":
if (data === PREF_SEPARATE_PRIVILEGEDABOUT_CONTENT_PROCESS) {
this._privilegedAboutContentProcess = Services.prefs.getBoolPref(PREF_SEPARATE_PRIVILEGEDABOUT_CONTENT_PROCESS);
this.updatePrerenderedPath();
this.notifyChange();
} else if (!IS_RELEASE_OR_BETA && data === PREF_ACTIVITY_STREAM_DEBUG) {
this._activityStreamDebug = Services.prefs.getBoolPref(PREF_ACTIVITY_STREAM_DEBUG, false);
this.updatePrerenderedPath();
this.notifyChange();
}
break;
@ -147,7 +138,6 @@ AboutNewTabService.prototype = {
`${BASE_URL}vendor/prop-types.js`,
`${BASE_URL}vendor/redux.js`,
`${BASE_URL}vendor/react-redux.js`,
`${BASE_URL}prerendered/${this.activityStreamLocale}/activity-stream-strings.js`,
`${BASE_URL}data/content/activity-stream.bundle.js`,
];
@ -174,10 +164,6 @@ AboutNewTabService.prototype = {
Services.obs.removeObserver(this, TOPIC_CONTENT_DOCUMENT_INTERACTIVE);
}
break;
case TOPIC_LOCALES_CHANGE:
this.updatePrerenderedPath();
this.notifyChange();
break;
}
},
@ -209,34 +195,23 @@ AboutNewTabService.prototype = {
if (!IS_RELEASE_OR_BETA) {
this._activityStreamDebug = Services.prefs.getBoolPref(PREF_ACTIVITY_STREAM_DEBUG, false);
}
this.updatePrerenderedPath();
this._newtabURL = ABOUT_URL;
return true;
},
/**
* Figure out what path under prerendered to use based on current state.
*/
updatePrerenderedPath() {
// Debug files are specially packaged in a non-localized directory, but with
// dynamic script loading, localized debug is supported.
this._activityStreamPath = `${this._activityStreamDebug &&
!this._privilegedAboutContentProcess ? "static" : this.activityStreamLocale}/`;
},
/*
* Returns the default URL.
*
* This URL depends on various activity stream prefs and locales. Overriding
* This URL depends on various activity stream prefs. Overriding
* the newtab page has no effect on the result of this function.
*/
get defaultURL() {
// Generate the desired activity stream resource depending on state, e.g.,
// resource://activity-stream/prerendered/ar/activity-stream.html
// resource://activity-stream/prerendered/static/activity-stream-debug.html
// "resource://activity-stream/prerendered/activity-stream.html"
// "resource://activity-stream/prerendered/activity-stream-debug.html"
// "resource://activity-stream/prerendered/activity-stream-noscripts.html"
return [
"resource://activity-stream/prerendered/",
this._activityStreamPath,
"activity-stream",
// Debug version loads dev scripts but noscripts separately loads scripts
this._activityStreamDebug && !this._privilegedAboutContentProcess ? "-debug" : "",
@ -286,21 +261,6 @@ AboutNewTabService.prototype = {
return this._activityStreamDebug;
},
get activityStreamLocale() {
// Pick the best available locale to match the app locales
return Services.locale.negotiateLanguages(
// Fix up incorrect BCP47 that are actually lang tags as a workaround for
// bug 1479606 returning the wrong values in the content process
Services.locale.appLocalesAsBCP47.map(l => l.replace(/^(ja-JP-mac)$/, "$1os")),
ACTIVITY_STREAM_BCP47,
// defaultLocale's strings aren't necessarily packaged, but en-US' are
"en-US",
Services.locale.langNegStrategyLookup
// Convert the BCP47 to lang tag, which is what is used in our paths, as a
// workaround for bug 1478930 negotiating incorrectly with lang tags
)[0].replace(/^(ja-JP-mac)os$/, "$1");
},
resetNewTabURL() {
this._overridden = false;
this._newTabURL = ABOUT_URL;
@ -327,7 +287,6 @@ AboutNewTabService.prototype = {
return;
}
Services.obs.removeObserver(this, TOPIC_APP_QUIT);
Services.obs.removeObserver(this, TOPIC_LOCALES_CHANGE);
Services.prefs.removeObserver(PREF_SEPARATE_PRIVILEGEDABOUT_CONTENT_PROCESS, this);
if (!IS_RELEASE_OR_BETA) {
Services.prefs.removeObserver(PREF_ACTIVITY_STREAM_DEBUG, this);

View file

@ -3,8 +3,6 @@ const fs = require("fs");
const {mkdir} = require("shelljs");
const path = require("path");
const {CENTRAL_LOCALES, DEFAULT_LOCALE} = require("./locales");
// Note: DEFAULT_OPTIONS.baseUrl should match BASE_URL in aboutNewTabService.js
// in mozilla-central.
const DEFAULT_OPTIONS = {
@ -12,52 +10,11 @@ const DEFAULT_OPTIONS = {
baseUrl: "resource://activity-stream/",
};
// Locales that should be displayed RTL
const RTL_LIST = ["ar", "he", "fa", "ur"];
/**
* Get the language part of the locale.
*/
function getLanguage(locale) {
return locale.split("-")[0];
}
/**
* Get the best strings for a single provided locale using similar locales and
* DEFAULT_LOCALE as fallbacks.
*/
function getStrings(locale, allStrings) {
const availableLocales = Object.keys(allStrings);
const language = getLanguage(locale);
const similarLocales = availableLocales.filter(other =>
other !== locale && getLanguage(other) === language);
// Rank locales from least desired to most desired
const localeFallbacks = [DEFAULT_LOCALE, ...similarLocales, locale];
// Get strings from each locale replacing with those from more desired ones
const desired = Object.assign({}, ...localeFallbacks.map(l => allStrings[l]));
// Only include strings that are currently used (defined by default locale)
return Object.assign({}, ...Object.keys(allStrings[DEFAULT_LOCALE]).map(
key => ({[key]: desired[key]})));
}
/**
* Get the text direction of the locale.
*/
function getTextDirection(locale) {
return RTL_LIST.includes(locale.split("-")[0]) ? "rtl" : "ltr";
}
/**
* templateHTML - Generates HTML for activity stream, given some options and
* prerendered HTML if necessary.
*
* @param {obj} options
* {str} options.locale The locale to render in lang="" attribute
* {str} options.direction The language direction to render in dir="" attribute
* {str} options.baseUrl The base URL for all local assets
* {bool} options.debug Should we use dev versions of JS libraries?
* {bool} options.noscripts Should we include scripts in the prerendered files?
@ -73,7 +30,6 @@ function templateHTML(options) {
`${options.baseUrl}vendor/prop-types.js`,
`${options.baseUrl}vendor/redux.js`,
`${options.baseUrl}vendor/react-redux.js`,
`${options.baseUrl}prerendered/${options.locale}/activity-stream-strings.js`,
`${options.baseUrl}data/content/activity-stream.bundle.js`,
];
@ -81,7 +37,7 @@ function templateHTML(options) {
const scriptRender = `\n${scripts.map(script => ` <script src="${script}"></script>`).join("\n")}`;
return `<!doctype html>
<html lang="${options.locale}" dir="${options.direction}">
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; object-src 'none'; script-src resource: chrome:; connect-src https:; img-src https: data: blob:; style-src 'unsafe-inline';">
@ -101,49 +57,24 @@ function templateHTML(options) {
`;
}
/**
* templateJs - Generates a js file that passes the initial state of the prerendered
* DOM to the React version. This is necessary to ensure the checksum matches when
* React mounts so that it can attach to the prerendered elements instead of blowing
* them away.
*
* Note that this may no longer be necessary in React 16 and we should review whether
* it is still necessary.
*
* @param {string} name The name of the global to expose
* @param {string} desc Extra description to include in a js comment
* @param {obj} state The data to expose as a window global
* @return {str} The js file as a string
*/
function templateJs(name, desc, state) {
return `// Note - this is a generated ${desc} file.
window.${name} = ${JSON.stringify(state, null, 2)};
`;
}
/**
* writeFiles - Writes to the desired files the result of a template given
* various prerendered data and options.
*
* @param {string} name Something to identify in the console
* @param {string} destPath Path to write the files to
* @param {Map} filesMap Mapping of a string file name to templater
* @param {Object} options Various options for the templater
*/
function writeFiles(name, destPath, filesMap, options) {
function writeFiles(destPath, filesMap, options) {
for (const [file, templater] of filesMap) {
console.log("\x1b[32m", `${file}`, "\x1b[0m");
fs.writeFileSync(path.join(destPath, file), templater({options}));
}
console.log("\x1b[32m", `${name}`, "\x1b[0m");
}
const STATIC_FILES = new Map([
["activity-stream-debug.html", ({options}) => templateHTML(options)],
]);
const LOCALIZED_FILES = new Map([
["activity-stream-strings.js", ({options: {locale, strings}}) => templateJs("gActivityStreamStrings", locale, strings)],
["activity-stream.html", ({options}) => templateHTML(options)],
["activity-stream-debug.html", ({options}) => templateHTML(Object.assign({}, options, {debug: true}))],
["activity-stream-noscripts.html", ({options}) => templateHTML(Object.assign({}, options, {noscripts: true}))],
]);
@ -162,74 +93,13 @@ function main() { // eslint-disable-line max-statements
},
});
const baseOptions = Object.assign({debug: false}, DEFAULT_OPTIONS, args || {});
const addonPath = path.resolve(__dirname, baseOptions.addonPath);
const allStrings = require(`${baseOptions.addonPath}/data/locales.json`);
const extraLocales = Object.keys(allStrings).filter(locale =>
locale !== DEFAULT_LOCALE && !CENTRAL_LOCALES.includes(locale));
const options = Object.assign({debug: false}, DEFAULT_OPTIONS, args || {});
const addonPath = path.resolve(__dirname, options.addonPath);
const prerenderedPath = path.join(addonPath, "prerendered");
console.log(`Writing prerendered files to individual directories under ${prerenderedPath}:`);
console.log(`Writing prerendered files to ${prerenderedPath}:`);
// Save default locale's strings to compare against other locales' strings
let defaultStrings;
let langStrings;
const isSubset = (strings, existing) => existing &&
Object.keys(strings).every(key => strings[key] === existing[key]);
// Process the default locale first then all the ones from mozilla-central
const localizedLocales = [];
const skippedLocales = [];
for (const locale of [DEFAULT_LOCALE, ...CENTRAL_LOCALES]) {
// Skip the locale if it would have resulted in duplicate packaged files
const strings = getStrings(locale, allStrings);
if (isSubset(strings, defaultStrings) || isSubset(strings, langStrings)) {
skippedLocales.push(locale);
continue;
}
const options = Object.assign({}, baseOptions, {
direction: getTextDirection(locale),
locale,
strings,
});
// Put locale-specific files in their own directory
const localePath = path.join(prerenderedPath, "locales", locale);
mkdir("-p", localePath);
writeFiles(locale, localePath, LOCALIZED_FILES, options);
// Only write static files once for the default locale
if (locale === DEFAULT_LOCALE) {
const staticPath = path.join(prerenderedPath, "static");
mkdir("-p", staticPath);
writeFiles(`${locale} (static)`, staticPath, STATIC_FILES,
Object.assign({}, options, {debug: true}));
// Save the default strings to compare against other locales' strings
defaultStrings = strings;
}
// Save the language's strings to maybe reuse for the next similar locales
if (getLanguage(locale) === locale) {
langStrings = strings;
}
localizedLocales.push(locale);
}
if (skippedLocales.length) {
console.log("\x1b[33m", `Skipped the following locales because they use the same strings as ${DEFAULT_LOCALE} or its language locale: ${skippedLocales.join(", ")}`, "\x1b[0m");
}
if (extraLocales.length) {
console.log("\x1b[33m", `Skipped the following locales because they are not in CENTRAL_LOCALES: ${extraLocales.join(", ")}`, "\x1b[0m");
}
// Convert ja-JP-mac lang tag to ja-JP-macos bcp47 to work around bug 1478930
const bcp47String = localizedLocales.join(" ").replace(/(ja-JP-mac)/, "$1os");
// Provide some help to copy/paste locales if tests are failing
console.log(`\nIf aboutNewTabService tests are failing for unexpected locales, make sure its list is updated:\nconst ACTIVITY_STREAM_BCP47 = "${bcp47String}".split(" ");`);
mkdir("-p", prerenderedPath);
writeFiles(prerenderedPath, STATIC_FILES, options);
}
main();

View file

@ -26,7 +26,8 @@ browser.jar:
#else
res/activity-stream/css/activity-stream.css (./css/activity-stream-linux.css)
#endif
res/activity-stream/prerendered/activity-stream.html (./prerendered/activity-stream.html)
#ifndef RELEASE_OR_BETA
res/activity-stream/prerendered/static/activity-stream-debug.html (./prerendered/static/activity-stream-debug.html)
res/activity-stream/prerendered/activity-stream-debug.html (./prerendered/activity-stream-debug.html)
#endif
res/activity-stream/prerendered/ (./prerendered/locales/*)
res/activity-stream/prerendered/activity-stream-noscripts.html (./prerendered/activity-stream-noscripts.html)

View file

@ -49,11 +49,6 @@ interface nsIAboutNewTabService : nsISupports
*/
readonly attribute bool activityStreamDebug;
/**
* Returns the locale of the activity stream interface
*/
readonly attribute ACString activityStreamLocale;
/**
* Resets to the default resource and also resets the
* overridden attribute to false.

View file

@ -1,5 +1,5 @@
<!doctype html>
<html lang="en-US" dir="ltr">
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; object-src 'none'; script-src resource: chrome:; connect-src https:; img-src https: data: blob:; style-src 'unsafe-inline';">
@ -21,7 +21,6 @@
<script src="resource://activity-stream/vendor/prop-types.js"></script>
<script src="resource://activity-stream/vendor/redux.js"></script>
<script src="resource://activity-stream/vendor/react-redux.js"></script>
<script src="resource://activity-stream/prerendered/en-US/activity-stream-strings.js"></script>
<script src="resource://activity-stream/data/content/activity-stream.bundle.js"></script>
</body>
</html>

View file

@ -1,5 +1,5 @@
<!doctype html>
<html lang="en-US" dir="ltr">
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; object-src 'none'; script-src resource: chrome:; connect-src https:; img-src https: data: blob:; style-src 'unsafe-inline';">

View file

@ -1,5 +1,5 @@
<!doctype html>
<html lang="en-US" dir="ltr">
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; object-src 'none'; script-src resource: chrome:; connect-src https:; img-src https: data: blob:; style-src 'unsafe-inline';">
@ -21,7 +21,6 @@
<script src="resource://activity-stream/vendor/prop-types.js"></script>
<script src="resource://activity-stream/vendor/redux.js"></script>
<script src="resource://activity-stream/vendor/react-redux.js"></script>
<script src="resource://activity-stream/prerendered/en-US/activity-stream-strings.js"></script>
<script src="resource://activity-stream/data/content/activity-stream.bundle.js"></script>
</body>
</html>

View file

@ -1,2 +0,0 @@
// Note - this is a generated en-US file.
window.gActivityStreamStrings = {};

View file

@ -29,12 +29,12 @@ let ACTIVITY_STREAM_URL;
let ACTIVITY_STREAM_DEBUG_URL;
function setExpectedUrlsWithScripts() {
ACTIVITY_STREAM_URL = "resource://activity-stream/prerendered/en-US/activity-stream.html";
ACTIVITY_STREAM_DEBUG_URL = "resource://activity-stream/prerendered/static/activity-stream-debug.html";
ACTIVITY_STREAM_URL = "resource://activity-stream/prerendered/activity-stream.html";
ACTIVITY_STREAM_DEBUG_URL = "resource://activity-stream/prerendered/activity-stream-debug.html";
}
function setExpectedUrlsWithoutScripts() {
ACTIVITY_STREAM_URL = "resource://activity-stream/prerendered/en-US/activity-stream-noscripts.html";
ACTIVITY_STREAM_URL = "resource://activity-stream/prerendered/activity-stream-noscripts.html";
// Debug urls are the same as non-debug because debug scripts load dynamically
ACTIVITY_STREAM_DEBUG_URL = ACTIVITY_STREAM_URL;
@ -192,11 +192,6 @@ addTestsWithPrivilegedContentProcessPref(async function test_welcome_url() {
cleanup();
});
add_task(function test_locale() {
Assert.equal(aboutNewTabService.activityStreamLocale, "en-US",
"The locale for testing should be en-US");
});
/**
* Tests response to updates to prefs
*/

View file

@ -5,7 +5,6 @@ codespell:
- browser/base/content/docs/
- browser/branding/
- browser/components/newtab/docs/
- browser/components/newtab/prerendered/locales/en-US/
- browser/extensions/formautofill/locales/en-US/
- browser/extensions/report-site-issue/locales/en-US/
- browser/installer/windows/docs/