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
10 changes: 8 additions & 2 deletions lib/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { GettextTranslationBundle } from '@nextcloud/l10n/gettext'
import type { GettextTranslation } from '@nextcloud/l10n/gettext'

declare const __TRANSLATIONS__: { locale: string, json: GettextTranslationBundle }[]
declare global {
// Allow injected by vite
const __TRANSLATIONS__: {
language: string
translations: GettextTranslation[]
}[]
}

// Allow css modules
declare module '*.module.css';
Expand Down
15 changes: 15 additions & 0 deletions lib/utils/l10n.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { setLanguage } from '@nextcloud/l10n'
import { beforeEach, expect, test, vi } from 'vitest'

beforeEach(() => vi.resetModules())

test('translation compressing and decompressing is working', async () => {
setLanguage('de')
const { t } = await import('./l10n.ts')
expect(t('Names must not be empty.')).toMatchInlineSnapshot('"Namen dürfen nicht leer sein."')
})
13 changes: 12 additions & 1 deletion lib/utils/l10n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { GettextTranslation, GettextTranslationBundle } from '@nextcloud/l10n/gettext'

import { getGettextBuilder } from '@nextcloud/l10n/gettext'

const gtBuilder = getGettextBuilder()
.detectLanguage()

__TRANSLATIONS__.map((data) => gtBuilder.addTranslation(data.locale, data.json))
for (const data of __TRANSLATIONS__) {
const { language, translations } = data as { language: string, translations: GettextTranslation[] }
const bundle: GettextTranslationBundle = {
headers: {},
translations: {
'': Object.fromEntries(translations.map((translation: GettextTranslation) => [translation.msgid, translation])),
},
}
gtBuilder.addTranslation(language, bundle)
}

const gt = gtBuilder.build()

Expand Down
16 changes: 11 additions & 5 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ import { readdirSync, readFileSync } from 'fs'
import { po as poParser } from 'gettext-parser'

const translations = readdirSync('./l10n')
.filter(name => name !== 'messages.pot' && name.endsWith('.pot'))
.map(file => {
.filter((name: string) => name !== 'messages.pot' && name.endsWith('.pot'))
.map((file: string) => {
const path = './l10n/' + file
const locale = file.slice(0, -'.pot'.length)
const language = file.slice(0, -'.pot'.length)

const po = readFileSync(path)
const json = poParser.parse(po)
// compress the translations
const translations = Object.entries(json.translations[''])
.filter(([key]) => key)
.map(([, value]) => value)

return {
locale,
json,
language,
translations,
}
})
.filter(({ translations }) => translations.length > 1)

export default createLibConfig({
index: 'lib/index.ts',
Expand Down
Loading