Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions build/extract-l10n.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,33 @@ extractor.createHtmlParser([
])
.parseFilesGlob('./src/**/*.vue')

const messages = extractor.getMessages()

/**
* remove references to avoid conflicts but save them for code splitting
*
* @type {Record<string,string[]>}
*/
export const context = extractor.getMessages().map((msg) => {
const localContext = [msg.text ?? '', [...new Set(msg.references.map((ref) => ref.split(':')[0] ?? ''))].sort().join(':')]
export const context = messages.map((msg) => {
// mapping of translation ID -> filename(s) [id, filename1:filename2:...]
const idUsage = [
msg.text ?? '',
// get the reference file names only (split off line number), remove duplicates and sort them
[...new Set(msg.references.map((ref) => ref.split(':')[0] ?? ''))].sort().join(':'),
]
// remove reference to avoid conflicts on transifex
msg.references = []
return localContext
}).reduce((p, [id, usage]) => {
const localContext = { ...(Array.isArray(p) ? {} : p) }
return idUsage
}).reduce((localContext, [id, usage]) => {
// add translation bundles to their usage context (filenames -> [ids])
if (usage in localContext) {
localContext[usage].push(id)
return localContext
} else {
localContext[usage] = [id]
}
return localContext
})
}, {})

extractor.savePotFile('./l10n/messages.pot')

Expand Down
11 changes: 6 additions & 5 deletions build/l10n-plugin.mts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ export default (dir: string) => {
for (const locale in allTranslations) {
const currentTranslations = allTranslations[locale]!
for (const [usage, msgIds] of Object.entries(context)) {
if (!(usage in translations)) {
translations[usage] = []
}
translations[usage] ??= []
// split the translations by usage in components
translations[usage]!.push({
l: locale,
Expand Down Expand Up @@ -78,8 +76,9 @@ export default (dir: string) => {
return null
} else if (source.endsWith('l10n.ts') && importer && !importer.includes('node_modules')) {
if (dirname(resolve(dirname(importer), source)).split('/').at(-1) === 'src') {
const [path] = importer.split('?', 2)
// return our wrapper for handling the import
return `\0l10nwrapper?source=${encodeURIComponent(importer)}`
return `\0l10nwrapper?source=${encodeURIComponent(path!)}`
}
}
},
Expand All @@ -105,7 +104,9 @@ export default (dir: string) => {
return `import {t,n,register,${imports.join(',')}} from '\0l10n';register(${imports.join(',')});export {t,n};`
} else if (id === '\0l10n') {
// exports are all chunked translations
const exports = Object.entries(nameMap).map(([usage, id]) => `export const ${id} = ${JSON.stringify(translations[usage])}`).join(';\n')
const exports = Object.entries(nameMap)
.map(([usage, id]) => `export const ${id} = ${JSON.stringify(translations[usage])}`)
.join(';\n')
return `${l10nRegistrationCode}\n${exports}`
}
},
Expand Down
Loading