Skip to content

Commit

Permalink
build: Adjust l10n plugin for new library version
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux authored and ShGKme committed Feb 13, 2025
1 parent c630730 commit 36228de
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
42 changes: 27 additions & 15 deletions build/l10n-plugin.mts
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,23 @@ export default (dir: string) => {
} 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')
return `import { getGettextBuilder } from '@nextcloud/l10n/gettext'
const gettext = getGettextBuilder().detectLocale().build()
export const n = gettext.ngettext.bind(gettext)
export const t = gettext.gettext.bind(gettext)
export const register = (...chunks) => {
chunks.forEach((chunk) => {
return `import { getLanguage } from '@nextcloud/l10n'
import { getGettextBuilder } from '@nextcloud/l10n/gettext'
const builder = getGettextBuilder().setLanguage(getLanguage())
let gettext = builder.build()
export const n = (...args) => gettext.ngettext(...args)
export const t = (...args) => gettext.gettext(...args)
export function register(...chunks) {
for (const chunk of chunks) {
if (!chunk.registered) {
// for every locale in the chunk: decompress and register
chunk.forEach(({ l: locale, t: translations }) => {
// for every language in the chunk: decompress and register
for (const { l: language, t: translations } of chunk) {
if (language !== getLanguage() || !translations) {
continue
}
const decompressed = Object.fromEntries(
Object.entries(translations)
.map(([id, value]) => [
Expand All @@ -118,15 +126,19 @@ export const register = (...chunks) => {
}
])
)
// We need to do this manually as 'addTranslations' overrides the translations
if (!gettext.gt.catalogs[locale]) {
gettext.gt.catalogs[locale] = { messages: { translations: {}} }
}
gettext.gt.catalogs[locale].messages.translations[''] = { ...gettext.gt.catalogs[locale].messages.translations[''], ...decompressed }
})
gettext = builder.addTranslation(getLanguage(), {
translations: {
'': {
...(gettext.bundle.translations?.[''] ?? {}),
...decompressed,
},
},
}).build()
}
chunk.registered = true
}
})
}
}
${exports}`
}
Expand Down
2 changes: 1 addition & 1 deletion src/l10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// This is only a dummy file, the real translations are injected by the vite plugin from build/l10n-plugin!
import { getGettextBuilder } from '@nextcloud/l10n/gettext'

const gtBuilder = getGettextBuilder().detectLocale()
const gtBuilder = getGettextBuilder().detectLanguage()
const gt = gtBuilder.build()

const n = gt.ngettext.bind(gt)
Expand Down

0 comments on commit 36228de

Please sign in to comment.