chore: add i18n:format/types to pre-commit (#6515)

* chore(i18n): update generated files to match repo style

* chore: add i18n:format/types to pre-commit
This commit is contained in:
Slinetrac
2026-03-15 12:51:57 +00:00
committed by GitHub
parent c672a6fef3
commit 9b0aa262bd
4 changed files with 64 additions and 46 deletions
+5 -4
View File
@@ -1122,17 +1122,18 @@ function regenerateLocaleIndex(localeDir, namespaces) {
const filePath = path.join(localeDir, `${namespace}.json`)
if (!fs.existsSync(filePath)) continue
const identifier = toModuleIdentifier(namespace, seen)
imports.push(`import ${identifier} from "./${namespace}.json";`)
mappings.push(` "${namespace}": ${identifier},`)
const key = namespace === identifier ? namespace : `'${namespace}'`
imports.push(`import ${identifier} from './${namespace}.json'`)
mappings.push(` ${key}: ${identifier},`)
}
const content = `${imports.join('\n')}
const resources = {
${mappings.join('\n')}
};
}
export default resources;
export default resources
`
fs.writeFileSync(path.join(localeDir, 'index.ts'), content, 'utf8')
+35 -12
View File
@@ -12,9 +12,21 @@ const RESOURCE_OUTPUT = path.resolve(
ROOT_DIR,
'src/types/generated/i18n-resources.ts',
)
const GENERATED_HEADER_LINES = [
'// This file is auto-generated by scripts/generate-i18n-keys.mjs',
'// Do not edit this file manually.',
]
const IDENTIFIER_PATTERN = /^[A-Za-z_$][A-Za-z0-9_$]*$/
const isPlainObject = (value) =>
typeof value === 'object' && value !== null && !Array.isArray(value)
const getIndent = (size) => ' '.repeat(size)
const formatStringLiteral = (value) =>
`'${JSON.stringify(value).slice(1, -1).replaceAll("'", "\\'")}'`
const formatPropertyKey = (key) =>
IDENTIFIER_PATTERN.test(key) ? key : formatStringLiteral(key)
const buildGeneratedFile = (bodyLines) =>
[...GENERATED_HEADER_LINES, '', ...bodyLines, ''].join('\n')
const flattenKeys = (data, prefix = '') => {
const keys = []
@@ -35,11 +47,11 @@ const buildType = (data, indent = 0) => {
}
const entries = Object.entries(data).sort(([a], [b]) => a.localeCompare(b))
const pad = ' '.repeat(indent)
const pad = getIndent(indent)
const inner = entries
.map(([key, value]) => {
const typeStr = buildType(value, indent + 2)
return `${' '.repeat(indent + 2)}${JSON.stringify(key)}: ${typeStr};`
return `${getIndent(indent + 2)}${formatPropertyKey(key)}: ${typeStr}`
})
.join('\n')
@@ -66,19 +78,30 @@ const loadNamespaceJson = async () => {
}
const buildKeysFile = (keys) => {
const arrayLiteral = keys.map((key) => ` "${key}"`).join(',\n')
return `// This file is auto-generated by scripts/generate-i18n-keys.mjs\n// Do not edit this file manually.\n\nexport const translationKeys = [\n${arrayLiteral}\n] as const;\n\nexport type TranslationKey = typeof translationKeys[number];\n`
const keyLines = keys.map(
(key) => `${getIndent(2)}${formatStringLiteral(key)},`,
)
return buildGeneratedFile([
'export const translationKeys = [',
...keyLines,
'] as const',
'',
'export type TranslationKey = (typeof translationKeys)[number]',
])
}
const buildResourcesFile = (namespaces) => {
const namespaceEntries = namespaces
.map(({ name, json }) => {
const typeStr = buildType(json, 4)
return ` ${JSON.stringify(name)}: ${typeStr};`
})
.join('\n')
return `// This file is auto-generated by scripts/generate-i18n-keys.mjs\n// Do not edit this file manually.\n\nexport interface TranslationResources {\n translation: {\n${namespaceEntries}\n };\n}\n`
const namespaceLines = namespaces.map(({ name, json }) => {
const typeStr = buildType(json, 4)
return `${getIndent(4)}${formatPropertyKey(name)}: ${typeStr}`
})
return buildGeneratedFile([
'export interface TranslationResources {',
' translation: {',
...namespaceLines,
' }',
'}',
])
}
const main = async () => {