From a30f0c7c64ebc128cbc511e15eee43739293eb8b Mon Sep 17 00:00:00 2001 From: Northword Date: Tue, 11 Jul 2023 18:36:04 +0800 Subject: [PATCH] Add prefix for locale files when build --- scripts/build.mjs | 26 ++++++++++++++++++++++++-- src/utils/locale.ts | 9 +++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/scripts/build.mjs b/scripts/build.mjs index 21d4554..1104b6a 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -84,6 +84,19 @@ function dateFormat(fmt, date) { return fmt; } +function addAddonRefToFlt(fltContent) { + const lines = fltContent.split("\n"); + const prefixedLines = lines.map((line) => { + if (line.match(/^(?[a-zA-Z]\S*)([ ]*=[ ]*)(?.*)$/gm)) { + // https://regex101.com/r/lQ9x5p/1 + return `${config.addonRef}-${line}`; + } else { + return line; + } + }); + return prefixedLines.join("\n"); +} + async function main() { const t = new Date(); const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t); @@ -121,7 +134,6 @@ async function main() { /__buildVersion__/g, /__buildTime__/g, ]; - const replaceTo = [author, description, homepage, version, buildTime]; replaceFrom.push( @@ -129,6 +141,9 @@ async function main() { ); replaceTo.push(...Object.values(config)); + replaceFrom.push(/(data-l10n-id=")(.*")/gm); + replaceTo.push(`$1${config.addonRef}-$2`); + const optionsAddon = { files: [ join(buildDir, "**/*.xhtml"), @@ -144,11 +159,18 @@ async function main() { }; const replaceResult = sync(optionsAddon); + + const replaceResultFlt = sync({ + files: [join(buildDir, "addon/**/*.ftl")], + processor: [addAddonRefToFlt], + }); + console.log( "[Build] Run replace in ", replaceResult .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"); diff --git a/src/utils/locale.ts b/src/utils/locale.ts index 516dec5..2642675 100644 --- a/src/utils/locale.ts +++ b/src/utils/locale.ts @@ -63,16 +63,17 @@ function _getString( localString: string, options: { branch?: string | undefined; args?: Record } = {} ): string { + const localStringWithPrefix = `${config.addonRef}-${localString}`; const { branch, args } = options; const pattern = addon.data.locale?.current.formatMessagesSync([ - { id: localString, args }, + { id: localStringWithPrefix, args }, ])[0]; if (!pattern) { - return localString; + return localStringWithPrefix; } if (branch && pattern.attributes) { - return pattern.attributes[branch] || localString; + return pattern.attributes[branch] || localStringWithPrefix; } else { - return pattern.value || localString; + return pattern.value || localStringWithPrefix; } }