|
| 1 | +import type { BaseNode, Message, MessageReference, Resource } from "@fluent/syntax"; |
| 2 | +import type { BuildConfig } from "../../types/config.js"; |
| 3 | +import { readFile, writeFile } from "node:fs/promises"; |
| 4 | +import { basename, dirname } from "node:path"; |
| 5 | +import { parse, serialize, Transformer } from "@fluent/syntax"; |
| 6 | +import { move } from "fs-extra/esm"; |
| 7 | +import styleText from "node-style-text"; |
| 8 | +import { glob } from "tinyglobby"; |
| 9 | +import { logger } from "../../utils/logger.js"; |
| 10 | +import { toArray } from "../../utils/string.js"; |
| 11 | + |
| 12 | +export default async function buildLocale( |
| 13 | + dist: string, |
| 14 | + namespace: string, |
| 15 | + options: BuildConfig["fluent"], |
| 16 | +) { |
| 17 | + const ignores = toArray(options.ignore); |
| 18 | + const localeNames = await getLocales(dist); |
| 19 | + const messageManager = new MessageManager(ignores); |
| 20 | + |
| 21 | + // Process FTL files and add messages to the manager |
| 22 | + await Promise.all(localeNames.map(async (locale) => { |
| 23 | + const paths = await glob(`${dist}/addon/locale/${locale}/**/*.ftl`); |
| 24 | + await Promise.all(paths.map(async (path) => { |
| 25 | + const fm = new FluentManager(); |
| 26 | + await fm.read(path); |
| 27 | + messageManager.addMessages(locale, fm.getMessages()); |
| 28 | + |
| 29 | + if (options.prefixFluentMessages) { |
| 30 | + fm.prefixMessages(namespace); |
| 31 | + await fm.write(path); |
| 32 | + } |
| 33 | + |
| 34 | + if (options.prefixLocaleFiles) { |
| 35 | + const newPath = `${dirname(path)}/${namespace}-${basename(path)}`; |
| 36 | + await move(path, newPath); |
| 37 | + logger.debug(`Renamed FTL: ${path} → ${newPath}`); |
| 38 | + } |
| 39 | + })); |
| 40 | + })); |
| 41 | + |
| 42 | + // Process HTML files and add messages to the manager |
| 43 | + const htmlPaths = await glob([`${dist}/addon/**/*.xhtml`, `${dist}/addon/**/*.html`]); |
| 44 | + await Promise.all(htmlPaths.map(async (htmlPath) => { |
| 45 | + const content = await readFile(htmlPath, "utf-8"); |
| 46 | + const { processedContent, foundMessages } = processHTMLFile( |
| 47 | + content, |
| 48 | + namespace, |
| 49 | + messageManager.getFTLMessages(), |
| 50 | + ignores, |
| 51 | + htmlPath, |
| 52 | + ); |
| 53 | + |
| 54 | + // Add all found HTML messages |
| 55 | + messageManager.addMessages("html", foundMessages); |
| 56 | + |
| 57 | + if (options.prefixFluentMessages) { |
| 58 | + await writeFile(htmlPath, processedContent); |
| 59 | + } |
| 60 | + })); |
| 61 | + |
| 62 | + // Validate that all HTML messages exist in all locales |
| 63 | + messageManager.validateMessages(); |
| 64 | +} |
| 65 | + |
| 66 | +async function getLocales(dist: string): Promise<string[]> { |
| 67 | + const localePaths = await glob(`${dist}/addon/locale/*`, { onlyDirectories: true }); |
| 68 | + return localePaths.map(p => basename(p)); |
| 69 | +} |
| 70 | + |
| 71 | +export class FluentManager { |
| 72 | + private source?: string; |
| 73 | + private resource?: Resource; |
| 74 | + public readonly messages: string[] = []; |
| 75 | + |
| 76 | + constructor() {} |
| 77 | + |
| 78 | + // Parse Fluent source into an AST and extract messages |
| 79 | + public parse(source: string) { |
| 80 | + this.source = source; |
| 81 | + this.resource = parse(source, {}); |
| 82 | + } |
| 83 | + |
| 84 | + // Read a file, parse its content, and extract messages |
| 85 | + public async read(path: string) { |
| 86 | + const content = await readFile(path, "utf-8"); |
| 87 | + this.parse(content); |
| 88 | + } |
| 89 | + |
| 90 | + // Extract message IDs from the parsed resource |
| 91 | + public getMessages(): string[] { |
| 92 | + if (!this.resource) { |
| 93 | + throw new Error("Resource must be parsed first."); |
| 94 | + } |
| 95 | + this.messages.length = 0; // Clear the previous messages |
| 96 | + this.messages.push( |
| 97 | + ...this.resource.body.filter(entry => entry.type === "Message") |
| 98 | + .map(message => message.id.name), |
| 99 | + ); |
| 100 | + return this.messages; |
| 101 | + } |
| 102 | + |
| 103 | + // Apply namespace prefix to message IDs in the resource |
| 104 | + public prefixMessages(namespace: string) { |
| 105 | + if (!this.resource) { |
| 106 | + throw new Error("Resource must be parsed before applying prefix."); |
| 107 | + } |
| 108 | + new FluentTransformer(namespace).genericVisit(this.resource); |
| 109 | + } |
| 110 | + |
| 111 | + // Serialize the resource back into a string |
| 112 | + public serialize(): string { |
| 113 | + if (!this.resource) { |
| 114 | + throw new Error("Resource not parsed. Cannot serialize."); |
| 115 | + } |
| 116 | + return serialize(this.resource, {}); |
| 117 | + } |
| 118 | + |
| 119 | + // Write the serialized resource to a file |
| 120 | + public async write(path: string) { |
| 121 | + const result = this.serialize(); |
| 122 | + if (result !== this.source) |
| 123 | + await writeFile(path, this.serialize()); |
| 124 | + } |
| 125 | +} |
| 126 | +// Custom Fluent AST transformer to apply message ID prefix |
| 127 | +class FluentTransformer extends Transformer { |
| 128 | + constructor(private readonly prefix: string | false) { |
| 129 | + super(); |
| 130 | + } |
| 131 | + |
| 132 | + private needsPrefix(name: string): boolean { |
| 133 | + return !!this.prefix && !name.startsWith(this.prefix); |
| 134 | + } |
| 135 | + |
| 136 | + visitMessage(node: Message): BaseNode { |
| 137 | + if (this.needsPrefix(node.id.name)) { |
| 138 | + node.id.name = `${this.prefix}-${node.id.name}`; |
| 139 | + } |
| 140 | + return this.genericVisit(node); |
| 141 | + } |
| 142 | + |
| 143 | + visitMessageReference(node: MessageReference): BaseNode { |
| 144 | + if (this.needsPrefix(node.id.name)) { |
| 145 | + node.id.name = `${this.prefix}-${node.id.name}`; |
| 146 | + } |
| 147 | + return this.genericVisit(node); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +export class MessageManager { |
| 152 | + private ftlMessages: Map<string, Set<string>> = new Map(); |
| 153 | + private htmlMessages: Set<string> = new Set(); |
| 154 | + private ignores: string[]; |
| 155 | + |
| 156 | + constructor(ignores: string[]) { |
| 157 | + this.ignores = ignores; |
| 158 | + } |
| 159 | + |
| 160 | + // Add a set of messages (FTL or HTML) for a specific locale or for HTML globally |
| 161 | + addMessages(target: string | "html", messages: string[]) { |
| 162 | + if (target === "html") { |
| 163 | + messages.forEach(msg => this.htmlMessages.add(msg)); |
| 164 | + } |
| 165 | + else { |
| 166 | + let ftlLocaleMessages = this.ftlMessages.get(target); |
| 167 | + if (!ftlLocaleMessages) { |
| 168 | + ftlLocaleMessages = new Set(); |
| 169 | + this.ftlMessages.set(target, ftlLocaleMessages); |
| 170 | + } |
| 171 | + messages.forEach(msg => ftlLocaleMessages.add(msg)); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + validateMessages() { |
| 176 | + // Check miss 1: Cross check in diff locale - seems no need |
| 177 | + // messagesByLocale.forEach((messageInThisLang, lang) => { |
| 178 | + // // Needs Nodejs 22 |
| 179 | + // const diff = allMessages.difference(messageInThisLang); |
| 180 | + // if (diff.size) |
| 181 | + // this.logger.warn(`FTL messages '${Array.from(diff).join(", ")}' don't exist the locale '${lang}'`); |
| 182 | + // }); |
| 183 | + |
| 184 | + // Check miss 2: Check ids in HTML but not in ftl |
| 185 | + this.htmlMessages.forEach((msg) => { |
| 186 | + if (this.ignores.includes(msg)) |
| 187 | + return; |
| 188 | + |
| 189 | + const missingLocales = [...this.ftlMessages.entries()] |
| 190 | + .filter(([_, messages]) => !messages.has(msg)) |
| 191 | + .map(([locale]) => locale); |
| 192 | + if (missingLocales.length > 0) |
| 193 | + logger.warn(`I10N id ${styleText.blue(msg)} missing in locale: ${missingLocales.join(", ")}`); |
| 194 | + }); |
| 195 | + } |
| 196 | + |
| 197 | + getFTLMessages(): Set<string> { |
| 198 | + const allMessages = new Set<string>(); |
| 199 | + this.ftlMessages.forEach(messages => messages.forEach(msg => allMessages.add(msg))); |
| 200 | + return allMessages; |
| 201 | + } |
| 202 | + |
| 203 | + // Get all FTL messages for a specific locale |
| 204 | + getFTLMessagesByLocale(locale: string): Set<string> { |
| 205 | + return this.ftlMessages.get(locale) || new Set(); |
| 206 | + } |
| 207 | + |
| 208 | + // Get all HTML messages |
| 209 | + getHTMLMessages(): Set<string> { |
| 210 | + return this.htmlMessages; |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +// Scan HTML content for l10n references and apply namespace prefix |
| 215 | +export function processHTMLFile( |
| 216 | + content: string, |
| 217 | + namespace: string, |
| 218 | + allMessages: Set<string>, |
| 219 | + ignores: string[], |
| 220 | + filePath: string, |
| 221 | +) { |
| 222 | + const foundMessages = new Set<string>(); |
| 223 | + |
| 224 | + const L10N_PATTERN = new RegExp(`(data-l10n-id)="((?!${namespace})\\S*)"`, "g"); |
| 225 | + const processed = content.replace(L10N_PATTERN, (match, attr, id) => { |
| 226 | + foundMessages.add(id); |
| 227 | + |
| 228 | + if (ignores.includes(id)) { |
| 229 | + logger.debug(`Skipped ignored ID: ${styleText.blue(id)} in ${styleText.gray(filePath)}`); |
| 230 | + return match; |
| 231 | + } |
| 232 | + |
| 233 | + if (!allMessages.has(id)) { |
| 234 | + logger.warn(`I10N id ${styleText.blue(id)} in path ${styleText.gray(filePath)} does not exist in any locale, skip renaming it.`); |
| 235 | + return match; |
| 236 | + } |
| 237 | + |
| 238 | + return `${attr}="${namespace}-${id}"`; |
| 239 | + }); |
| 240 | + |
| 241 | + return { processedContent: processed, foundMessages: [...foundMessages] }; |
| 242 | +} |
0 commit comments