Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
#!/usr/bin/env node
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The Bento authors
//
// Generates type/src/i18n/packed.ts from the per-locale catalogs.
//
// node scripts/build-type-i18n.mjs write the packed table
// node scripts/build-type-i18n.mjs --check fail if it is out of date (CI)
//
// WHY PACK. English-string-as-key means the source sentence is the key in every
// locale, so N plain catalogs store the same English text N times — and deflate
// cannot dedupe it, because the catalogs span far more than its 32KB window. The
// packed form stores each key ONCE with an array of translations by column, and
// `0` for "no translation, use English".
//
// The key universe is swept from the SOURCE, not from the catalogs, so a string
// added to the UI and forgotten in the catalogs shows up as missing rather than
// silently shipping English. Mirrors scripts/build-spaces-i18n.mjs — same shape,
// same reasoning, different app.
import { readFileSync, writeFileSync, readdirSync, existsSync, mkdirSync } from 'node:fs'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
const root = join(dirname(fileURLToPath(import.meta.url)), '..')
const srcDir = join(root, 'type/src')
const i18nDir = join(srcDir, 'i18n')
const OUT = join(i18nDir, 'packed.ts')
/** Locale columns, in order. English is the key, so it is not a column. */
export const LOCALES = ['ja', 'zh-Hans', 'zh-Hant', 'es', 'fr', 'de', 'it', 'pt']
/**
* Every `t('…')` call in the app source, including ones built from adjacent
* string-literal fragments joined by `+` (about.ts wraps a few long strings
* that way for line-length). A plain regex over the opening quote alone would
* capture only the FIRST fragment of those and silently produce a key that
* never matches what t() is actually called with at runtime — so this walks
* the call character-by-character instead of trusting one regex match.
*/
function sweepKeys() {
const keys = new Set()
const walk = (dir) => {
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const full = join(dir, entry.name)
if (entry.isDirectory()) { if (entry.name !== 'i18n') walk(full); continue }
if (!entry.name.endsWith('.ts')) continue
collectFromFile(full, keys)
}
}
walk(srcDir)
return [...keys].sort()
}
function collectFromFile(file, keys) {
const src = readFileSync(file, 'utf8')
const callRe = /\bt\(/g
let m
while ((m = callRe.exec(src))) {
let i = m.index + 2
const skipWs = () => { while (i < src.length && /\s/.test(src[i])) i++ }
skipWs()
if (src[i] !== "'" && src[i] !== '"' && src[i] !== '`') continue
let full = ''
let ok = true
let hadTemplate = false
for (;;) {
skipWs()
const q = src[i]
if (q === "'" || q === '"' || q === '`') {
let j = i + 1
let raw = ''
while (j < src.length) {
if (src[j] === '\\') { raw += src[j] + src[j + 1]; j += 2; continue }
if (src[j] === q) { j++; break }
raw += src[j]; j++
}
if (q === '`') { hadTemplate = true }
else if (q === "'") full += raw.replace(/\\'/g, "'").replace(/\\\\/g, '\\')
else { try { full += JSON.parse('"' + raw + '"') } catch { full += raw } }
i = j
} else { ok = false; break }
skipWs()
if (src[i] === '+') { i++; continue }
break
}
if (!ok || hadTemplate) continue // dynamic key or template literal — not extractable, not swept
keys.add(full)
}
}
/** Read one locale catalog by evaluating its object literal. */
function readCatalog(code) {
const file = join(i18nDir, `${code}.ts`)
if (!existsSync(file)) return {}
const src = readFileSync(file, 'utf8')
const start = src.indexOf('{', src.indexOf(`export const `))
const end = src.lastIndexOf('}')
if (start < 0 || end < 0) return {}
// the catalogs are generated, so this is a literal object of string pairs
return Function(`"use strict"; return (${src.slice(start, end + 1)})`)()
}
const keys = sweepKeys()
const catalogs = Object.fromEntries(LOCALES.map((c) => [c, readCatalog(c)]))
const rows = []
const missing = Object.fromEntries(LOCALES.map((c) => [c, 0]))
for (const key of keys) {
const row = LOCALES.map((c) => {
const v = catalogs[c][key]
if (typeof v === 'string' && v.length) return v
missing[c]++
return 0
})
rows.push([key, row])
}
const body = `// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The Bento authors
//
// GENERATED by scripts/build-type-i18n.mjs — do not edit. Run it after
// changing a catalog or adding a t() string; CI fails if this is out of date.
//
// One row per English key: the key is stored ONCE and the translations follow
// by column. \`0\` means "no translation in that locale" and falls back to the
// English key, which is also the source text — so a missing entry degrades to
// a readable string rather than a blank.
export const PACKED_LOCALES = ${JSON.stringify(LOCALES)} as const
export const PACKED: Record<string, ReadonlyArray<string | 0>> = {
${rows.map(([k, r]) => ` ${JSON.stringify(k)}: ${JSON.stringify(r)},`).join('\n')}
}
`
const current = existsSync(OUT) ? readFileSync(OUT, 'utf8') : null
const report = LOCALES.map((c) => {
const done = keys.length - missing[c]
const pct = keys.length ? Math.round((done / keys.length) * 1000) / 10 : 100
return ` ${c.padEnd(8)} ${String(done).padStart(3)}/${keys.length} ${pct}%`
}).join('\n')
if (process.argv.includes('--check')) {
if (current !== body) {
console.error('type packed.ts is out of date.\nRun: node scripts/build-type-i18n.mjs')
process.exit(1)
}
// Core catalogs ship INSIDE every saved file and cannot be corrected without
// a release, so an incomplete one is a build failure, not a warning.
const short = LOCALES.filter((c) => missing[c] > 0)
if (short.length) {
console.error(`incomplete core catalogs: ${short.map((c) => `${c} (${missing[c]} missing)`).join(', ')}`)
console.error(report)
process.exit(1)
}
console.log(`type i18n up to date (${keys.length} strings × ${LOCALES.length} locales, all complete)`)
} else {
mkdirSync(i18nDir, { recursive: true })
writeFileSync(OUT, body)
console.log(`packed ${keys.length} strings × ${LOCALES.length} locales -> ${OUT}`)
console.log(report)
}