Skip to content

Commit 21480ad

Browse files
authored
Merge pull request #8038 from nextcloud-libraries/backport/8037/stable8
[stable8] build: fix translations in vue modules
2 parents a76dfe1 + ca598e1 commit 21480ad

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

build/extract-l10n.mjs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,33 @@ extractor.createHtmlParser([
2828
])
2929
.parseFilesGlob('./src/**/*.vue')
3030

31+
const messages = extractor.getMessages()
32+
3133
/**
3234
* remove references to avoid conflicts but save them for code splitting
3335
*
3436
* @type {Record<string,string[]>}
3537
*/
36-
export const context = extractor.getMessages().map((msg) => {
37-
const localContext = [msg.text ?? '', [...new Set(msg.references.map((ref) => ref.split(':')[0] ?? ''))].sort().join(':')]
38+
export const context = messages.map((msg) => {
39+
// mapping of translation ID -> filename(s) [id, filename1:filename2:...]
40+
const idUsage = [
41+
msg.text ?? '',
42+
// get the reference file names only (split off line number), remove duplicates and sort them
43+
[...new Set(msg.references.map((ref) => ref.split(':')[0] ?? ''))].sort().join(':'),
44+
]
45+
// remove reference to avoid conflicts on transifex
3846
msg.references = []
39-
return localContext
40-
}).reduce((p, [id, usage]) => {
41-
const localContext = { ...(Array.isArray(p) ? {} : p) }
47+
return idUsage
48+
}).reduce((localContext, [id, usage]) => {
49+
// add translation bundles to their usage context (filenames -> [ids])
4250
if (usage in localContext) {
4351
localContext[usage].push(id)
4452
return localContext
4553
} else {
4654
localContext[usage] = [id]
4755
}
4856
return localContext
49-
})
57+
}, {})
5058

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

build/l10n-plugin.mts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ export default (dir: string) => {
4848
for (const locale in allTranslations) {
4949
const currentTranslations = allTranslations[locale]
5050
for (const [usage, msgIds] of Object.entries(context)) {
51-
if (!(usage in translations)) {
52-
translations[usage] = []
53-
}
51+
translations[usage] ??= []
5452
// split the translations by usage in components
5553
translations[usage].push({
5654
l: locale,
@@ -78,8 +76,9 @@ export default (dir: string) => {
7876
return null
7977
} else if (source.endsWith('l10n.js') && importer && !importer.includes('node_modules')) {
8078
if (dirname(resolve(dirname(importer), source)).split('/').at(-1) === 'src') {
79+
const [path] = importer.split('?', 2)
8180
// return our wrapper for handling the import
82-
return `\0l10nwrapper?source=${encodeURIComponent(importer)}`
81+
return `\0l10nwrapper?source=${encodeURIComponent(path!)}`
8382
}
8483
}
8584
},
@@ -105,7 +104,9 @@ export default (dir: string) => {
105104
return `import {t,n,register,${imports.join(',')}} from '\0l10n';register(${imports.join(',')});export {t,n};`
106105
} else if (id === '\0l10n') {
107106
// exports are all chunked translations
108-
const exports = Object.entries(nameMap).map(([usage, id]) => `export const ${id} = ${JSON.stringify(translations[usage])}`).join(';\n')
107+
const exports = Object.entries(nameMap)
108+
.map(([usage, id]) => `export const ${id} = ${JSON.stringify(translations[usage])}`)
109+
.join(';\n')
109110
return `${l10nRegistrationCode}\n${exports}`
110111
}
111112
},

0 commit comments

Comments
 (0)