Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏗 build and copy story localization strings to dist on amp build and amp dist #37623

Merged
merged 12 commits into from
Feb 9, 2022
96 changes: 96 additions & 0 deletions build-system/tasks/build-story-localization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const fs = require('fs-extra');
const fastGlob = require('fast-glob');
const pathMod = require('path');

const dest = 'dist/v0';

const FALLBACK_LANGUAGE_CODE = 'en';

const LANGUAGE_CODE_CHUNK_REGEX = /\w+/gi;

/**
* Finds fallback language codes for the current language code.
* This code is a copy of the same logic found in
* extensions/amp-story/1.0/amp-story-localization-service.js
erwinmombay marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} languageCode the language code to get fallbacks for
* @return {string[]}
*/
function getLanguageCodeFallbacks(languageCode) {
if (!languageCode) {
return [FALLBACK_LANGUAGE_CODE];
}
const matches = languageCode.match(LANGUAGE_CODE_CHUNK_REGEX) || [];
return matches.reduce(
(fallbackLanguageCodeList, chunk, index) => {
const fallbackLanguageCode = matches
.slice(0, index + 1)
.join('-')
.toLowerCase();
fallbackLanguageCodeList.push(fallbackLanguageCode);
return fallbackLanguageCodeList;
},
[FALLBACK_LANGUAGE_CODE]
);
}

/**
* Reads the language files found in amp-story and stores it in a
* Object.
* @return {Object}
*/
async function getLanguageStrings() {
const langs = Object.create(null);
const jsonFiles = await fastGlob('extensions/amp-story/1.0/_locales/*.json');
for (const jsonFile of jsonFiles) {
const langKey = pathMod.basename(jsonFile, '.json');
const translations = JSON.parse(await fs.readFile(jsonFile, 'utf8'));
for (const [key, value] of Object.entries(translations)) {
translations[key] = value['string'];
erwinmombay marked this conversation as resolved.
Show resolved Hide resolved
}
langs[langKey] = translations;
}
return langs;
}

/**
* Retrieves the fallback language codes for each current locale
* and assigns any strings from the fallback language.
* @param {Object} languages
*/
function ensureFallbacks(languages) {
for (const langKey in languages) {
languages[langKey] = getLanguageCodeFallbacks(languages, langKey)
.map((x) => languages[x])
.reduce((prev, cur) => {
erwinmombay marked this conversation as resolved.
Show resolved Hide resolved
return Object.assign(prev, cur);
}, Object.create(null));
erwinmombay marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Flattens the structure of the json locale strings and writes them out to the
* dist directory.
* @return {Promise<void>}
*/
async function buildStoryLocalization() {
await fs.ensureDir(dest);
const languages = await getLanguageStrings();
ensureFallbacks(languages);
// Write out each individual lang file.
for (const langKey in languages) {
await fs.writeFile(
`${dest}/amp-story.${langKey}.json`,
JSON.stringify(languages[langKey])
);
}
// Write out all the languages into one file.
await fs.writeFile(
`${dest}/amp-story.all-lang.json`,
JSON.stringify(languages)
);
erwinmombay marked this conversation as resolved.
Show resolved Hide resolved
}

module.exports = {buildStoryLocalization};

buildStoryLocalization.description =
'Flattens the structure of the json locale strings and writes them out to the dist directory';
7 changes: 6 additions & 1 deletion build-system/tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {buildExtensions} = require('./extension-helpers');
const {buildVendorConfigs} = require('./3p-vendor-helpers');
const {compileCss} = require('./css');
const {parseExtensionFlags} = require('./extension-helpers');
const {buildStoryLocalization} = require('./build-story-localization');

const argv = require('minimist')(process.argv.slice(2));

Expand All @@ -26,7 +27,11 @@ const argv = require('minimist')(process.argv.slice(2));
* @return {Promise}
*/
async function runPreBuildSteps(options) {
return Promise.all([compileCss(options), bootstrapThirdPartyFrames(options)]);
return Promise.all([
buildStoryLocalization(),
compileCss(options),
bootstrapThirdPartyFrames(options),
]);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions build-system/tasks/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const {compileJison} = require('./compile-jison');
const {formatExtractedMessages} = require('../compile/log-messages');
const {log} = require('../common/logging');
const {VERSION} = require('../compile/internal-version');
const {buildStoryLocalization} = require('./build-story-localization');

const {cyan, green} = colors;
const argv = require('minimist')(process.argv.slice(2));
Expand Down Expand Up @@ -93,6 +94,7 @@ async function runPreDistSteps(options) {
await compileJison();
await copyParsers();
await bootstrapThirdPartyFrames(options);
await buildStoryLocalization();
displayLifecycleDebugging();
}

Expand Down