|
| 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 ftlPaths = await glob(`${dist}/addon/locale/${locale}/**/*.ftl`); |
| 24 | + await Promise.all(ftlPaths.map(async (ftlPath) => { |
| 25 | + const originalContent = await readFile(ftlPath, "utf-8"); |
| 26 | + const { messages, processedContent } = processFTLFile(originalContent, namespace, options.prefixFluentMessages); |
| 27 | + |
| 28 | + // Add FTL messages for the current locale |
| 29 | + messageManager.addMessages(locale, messages); |
| 30 | + |
| 31 | + if (options.prefixFluentMessages) { |
| 32 | + await writeFile(ftlPath, processedContent); |
| 33 | + } |
| 34 | + |
| 35 | + if (options.prefixLocaleFiles) { |
| 36 | + const newPath = `${dirname(ftlPath)}/${namespace}-${basename(ftlPath)}`; |
| 37 | + await move(ftlPath, newPath); |
| 38 | + logger.debug(`Renamed FTL: ${ftlPath} → ${newPath}`); |
| 39 | + } |
| 40 | + })); |
| 41 | + })); |
| 42 | + |
| 43 | + // Process HTML files and add messages to the manager |
| 44 | + const htmlPaths = await glob([`${dist}/addon/**/*.xhtml`, `${dist}/addon/**/*.html`]); |
| 45 | + await Promise.all(htmlPaths.map(async (htmlPath) => { |
| 46 | + const content = await readFile(htmlPath, "utf-8"); |
| 47 | + const { processedContent, foundMessages } = processHTMLFile( |
| 48 | + content, |
| 49 | + namespace, |
| 50 | + messageManager.getFTLMessages(), |
| 51 | + ignores, |
| 52 | + htmlPath, |
| 53 | + ); |
| 54 | + |
| 55 | + // Add all found HTML messages |
| 56 | + messageManager.addMessages("html", foundMessages); |
| 57 | + |
| 58 | + if (options.prefixFluentMessages) { |
| 59 | + await writeFile(htmlPath, processedContent); |
| 60 | + } |
| 61 | + })); |
| 62 | + |
| 63 | + // Validate that all HTML messages exist in all locales |
| 64 | + messageManager.validateMessages(); |
| 65 | +} |
| 66 | + |
| 67 | +export class MessageManager { |
| 68 | + private ftlMessages: Map<string, Set<string>> = new Map(); |
| 69 | + private htmlMessages: Set<string> = new Set(); |
| 70 | + private ignores: string[]; |
| 71 | + |
| 72 | + constructor(ignores: string[]) { |
| 73 | + this.ignores = ignores; |
| 74 | + } |
| 75 | + |
| 76 | + // Add a set of messages (FTL or HTML) for a specific locale or for HTML globally |
| 77 | + addMessages(target: string | "html", messages: string[]) { |
| 78 | + if (target === "html") { |
| 79 | + messages.forEach(msg => this.htmlMessages.add(msg)); |
| 80 | + } |
| 81 | + else { |
| 82 | + let ftlLocaleMessages = this.ftlMessages.get(target); |
| 83 | + if (!ftlLocaleMessages) { |
| 84 | + ftlLocaleMessages = new Set(); |
| 85 | + this.ftlMessages.set(target, ftlLocaleMessages); |
| 86 | + } |
| 87 | + messages.forEach(msg => ftlLocaleMessages.add(msg)); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Validate that all HTML messages exist in all FTL locales |
| 92 | + validateMessages() { |
| 93 | + this.htmlMessages.forEach((msg) => { |
| 94 | + if (this.ignores.includes(msg)) |
| 95 | + return; |
| 96 | + |
| 97 | + this.ftlMessages.forEach((messages, locale) => { |
| 98 | + if (!messages.has(msg)) { |
| 99 | + logger.warn(`Missing message: ${styleText.blue(msg)} in locale: ${locale}`); |
| 100 | + } |
| 101 | + }); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + getFTLMessages(): Set<string> { |
| 106 | + const allMessages = new Set<string>(); |
| 107 | + this.ftlMessages.forEach(messages => messages.forEach(msg => allMessages.add(msg))); |
| 108 | + return allMessages; |
| 109 | + } |
| 110 | + |
| 111 | + // Get all FTL messages for a specific locale |
| 112 | + getFTLMessagesByLocale(locale: string): Set<string> { |
| 113 | + return this.ftlMessages.get(locale) || new Set(); |
| 114 | + } |
| 115 | + |
| 116 | + // Get all HTML messages |
| 117 | + getHTMLMessages(): Set<string> { |
| 118 | + return this.htmlMessages; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// Step 1: Extract all locale folder names |
| 123 | +async function getLocales(dist: string): Promise<string[]> { |
| 124 | + const localePaths = await glob(`${dist}/addon/locale/*`, { onlyDirectories: true }); |
| 125 | + return localePaths.map(p => basename(p)); |
| 126 | +} |
| 127 | + |
| 128 | +// Parse and optionally prefix messages in an FTL file |
| 129 | +export function processFTLFile( |
| 130 | + content: string, |
| 131 | + namespace: string, |
| 132 | + shouldPrefix: boolean, |
| 133 | +) { |
| 134 | + const messages = extractMessages(content); |
| 135 | + const processed = shouldPrefix ? transformFluent(content, namespace) : content; |
| 136 | + return { messages, processedContent: processed }; |
| 137 | +} |
| 138 | + |
| 139 | +// Scan HTML content for l10n references and apply namespace prefix |
| 140 | +export function processHTMLFile( |
| 141 | + content: string, |
| 142 | + namespace: string, |
| 143 | + allMessages: Set<string>, |
| 144 | + ignores: string[], |
| 145 | + filePath: string, |
| 146 | +) { |
| 147 | + const foundMessages = new Set<string>(); |
| 148 | + |
| 149 | + const L10N_PATTERN = new RegExp(`(data-l10n-id)="((?!${namespace})\\S*)"`, "g"); |
| 150 | + const processed = content.replace(L10N_PATTERN, (match, attr, id) => { |
| 151 | + foundMessages.add(id); |
| 152 | + |
| 153 | + if (ignores.includes(id)) { |
| 154 | + logger.debug(`Skipped ignored ID: ${styleText.blue(id)} in ${styleText.gray(filePath)}`); |
| 155 | + return match; |
| 156 | + } |
| 157 | + |
| 158 | + if (!allMessages.has(id)) { |
| 159 | + logger.warn(`Missing FTL: ${styleText.blue(id)} in ${styleText.gray(filePath)}`); |
| 160 | + return match; |
| 161 | + } |
| 162 | + |
| 163 | + return `${attr}="${namespace}-${id}"`; |
| 164 | + }); |
| 165 | + |
| 166 | + return { processedContent: processed, foundMessages: [...foundMessages] }; |
| 167 | +} |
| 168 | + |
| 169 | +// Fluent parsing and serialization helpers |
| 170 | +export function parseFluent(source: string): Resource { |
| 171 | + return parse(source, {}); |
| 172 | +} |
| 173 | + |
| 174 | +export function extractMessages(source: string): string[] { |
| 175 | + return parseFluent(source) |
| 176 | + .body |
| 177 | + .filter(entry => entry.type === "Message") |
| 178 | + .map(message => message.id.name); |
| 179 | +} |
| 180 | + |
| 181 | +export function serializeFluent(resource: Resource): string { |
| 182 | + return serialize(resource, {}); |
| 183 | +} |
| 184 | + |
| 185 | +// Prefix Fluent message IDs using a transformer |
| 186 | +export function transformFluent(source: string, prefix: string | false): string { |
| 187 | + const resource = parseFluent(source); |
| 188 | + new FluentTransformer(prefix).genericVisit(resource); |
| 189 | + return serializeFluent(resource); |
| 190 | +} |
| 191 | + |
| 192 | +// Custom Fluent AST transformer to apply message ID prefix |
| 193 | +class FluentTransformer extends Transformer { |
| 194 | + constructor(private readonly prefix: string | false) { |
| 195 | + super(); |
| 196 | + } |
| 197 | + |
| 198 | + private needsPrefix(name: string): boolean { |
| 199 | + return !!this.prefix && !name.startsWith(this.prefix); |
| 200 | + } |
| 201 | + |
| 202 | + visitMessage(node: Message): BaseNode { |
| 203 | + if (this.needsPrefix(node.id.name)) { |
| 204 | + node.id.name = `${this.prefix}-${node.id.name}`; |
| 205 | + } |
| 206 | + return this.genericVisit(node); |
| 207 | + } |
| 208 | + |
| 209 | + visitMessageReference(node: MessageReference): BaseNode { |
| 210 | + if (this.needsPrefix(node.id.name)) { |
| 211 | + node.id.name = `${this.prefix}-${node.id.name}`; |
| 212 | + } |
| 213 | + return this.genericVisit(node); |
| 214 | + } |
| 215 | +} |
0 commit comments