Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh: Compress translations to reduce bundle size #698

Merged
merged 1 commit into from
Jan 23, 2024
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
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
Loading