Add prefix for locale files when build

This commit is contained in:
Northword 2023-07-11 18:36:04 +08:00
parent eeb1ace01d
commit a30f0c7c64
2 changed files with 29 additions and 6 deletions

View File

@ -84,6 +84,19 @@ function dateFormat(fmt, date) {
return fmt; return fmt;
} }
function addAddonRefToFlt(fltContent) {
const lines = fltContent.split("\n");
const prefixedLines = lines.map((line) => {
if (line.match(/^(?<message>[a-zA-Z]\S*)([ ]*=[ ]*)(?<pattern>.*)$/gm)) {
// https://regex101.com/r/lQ9x5p/1
return `${config.addonRef}-${line}`;
} else {
return line;
}
});
return prefixedLines.join("\n");
}
async function main() { async function main() {
const t = new Date(); const t = new Date();
const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t); const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t);
@ -121,7 +134,6 @@ async function main() {
/__buildVersion__/g, /__buildVersion__/g,
/__buildTime__/g, /__buildTime__/g,
]; ];
const replaceTo = [author, description, homepage, version, buildTime]; const replaceTo = [author, description, homepage, version, buildTime];
replaceFrom.push( replaceFrom.push(
@ -129,6 +141,9 @@ async function main() {
); );
replaceTo.push(...Object.values(config)); replaceTo.push(...Object.values(config));
replaceFrom.push(/(data-l10n-id=")(.*")/gm);
replaceTo.push(`$1${config.addonRef}-$2`);
const optionsAddon = { const optionsAddon = {
files: [ files: [
join(buildDir, "**/*.xhtml"), join(buildDir, "**/*.xhtml"),
@ -144,11 +159,18 @@ async function main() {
}; };
const replaceResult = sync(optionsAddon); const replaceResult = sync(optionsAddon);
const replaceResultFlt = sync({
files: [join(buildDir, "addon/**/*.ftl")],
processor: [addAddonRefToFlt],
});
console.log( console.log(
"[Build] Run replace in ", "[Build] Run replace in ",
replaceResult replaceResult
.filter((f) => f.hasChanged) .filter((f) => f.hasChanged)
.map((f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}`) .map((f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}`),
replaceResultFlt.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`)
); );
console.log("[Build] Replace OK"); console.log("[Build] Replace OK");

View File

@ -63,16 +63,17 @@ function _getString(
localString: string, localString: string,
options: { branch?: string | undefined; args?: Record<string, unknown> } = {} options: { branch?: string | undefined; args?: Record<string, unknown> } = {}
): string { ): string {
const localStringWithPrefix = `${config.addonRef}-${localString}`;
const { branch, args } = options; const { branch, args } = options;
const pattern = addon.data.locale?.current.formatMessagesSync([ const pattern = addon.data.locale?.current.formatMessagesSync([
{ id: localString, args }, { id: localStringWithPrefix, args },
])[0]; ])[0];
if (!pattern) { if (!pattern) {
return localString; return localStringWithPrefix;
} }
if (branch && pattern.attributes) { if (branch && pattern.attributes) {
return pattern.attributes[branch] || localString; return pattern.attributes[branch] || localStringWithPrefix;
} else { } else {
return pattern.value || localString; return pattern.value || localStringWithPrefix;
} }
} }