Skip to content

Commit

Permalink
Merge pull request #698 from nextcloud-libraries/enh/compress-transla…
Browse files Browse the repository at this point in the history
…tions

enh: Compress translations to reduce bundle size
  • Loading branch information
susnux authored Jan 23, 2024
2 parents d7d0d92 + 238df68 commit 8a77735
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { GetTextTranslations } from 'gettext-parser'
declare global {
interface Window {
nc_pageLoad: number
nc_lastLogin: number
backendAllowsPasswordConfirmation: boolean
}

export declare global {
interface Window {
nc_pageLoad: number
nc_lastLogin: number
backendAllowsPasswordConfirmation: boolean
}

const __TRANSLATIONS__: Array<{ locale: string, json: GetTextTranslations }>
const __TRANSLATIONS__: Array<{ locale: string, translations: { msgid: string, msgid_plural?: string, msgstr: string }[] }>
}

export {}
6 changes: 5 additions & 1 deletion src/utils/l10n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const gtBuilder = getGettextBuilder()
.detectLocale()

__TRANSLATIONS__
.map(({ locale, json }) => gtBuilder.addTranslation(locale, json))
.map(({ locale, translations }) => gtBuilder.addTranslation(locale, {
translations: {
'': Object.fromEntries(translations.map((t) => [t.msgid, t])),
},
}))

const gt = gtBuilder.build()

Expand Down
13 changes: 11 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ const translations = fs
const po = fs.readFileSync(path)
const json = poParser.parse(po)

return { locale, json }
// Compress translations by removing everything not needed
const translations = Object.values(json.translations[''])
.filter((value) => value.msgid !== '')
.map((value) => ({
msgid: value.msgid,
msgid_plural: value.msgid_plural,
msgstr: value.msgstr,
}))

return { locale, translations }
})

export default createLibConfig({
index: resolve(__dirname, 'src/main.ts'),
}, {
libraryFormats: ['cjs', 'es'],
// Rename CSS chunk
assetFileNames: (chunkInfo) => chunkInfo.name.endsWith('.css') ? 'style.css' : undefined,
assetFileNames: (chunkInfo) => chunkInfo.name?.endsWith('.css') ? 'style.css' : undefined,
replace: {
__TRANSLATIONS__: `;${JSON.stringify(translations)}`,
},
Expand Down

0 comments on commit 8a77735

Please sign in to comment.