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

Minimize bundled translations #1775

Merged
merged 1 commit into from
Mar 29, 2021
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
33 changes: 32 additions & 1 deletion src/l10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,38 @@ import { getGettextBuilder } from '@nextcloud/l10n/dist/gettext'

const gtBuilder = getGettextBuilder()
.detectLocale()
TRANSLATIONS.map(data => gtBuilder.addTranslation(data.locale, data.json))

// Decompress Translations to gettext format and add to gtBuilder
TRANSLATIONS.forEach((lang) => {
const translations = {}

for (const key in lang.translations) {
// Plural
if (lang.translations[key].pluralId) {
translations[key] = {
msgid: key,
msgid_plural: lang.translations[key].pluralId,
msgstr: lang.translations[key].msgstr,
}
continue
}

// Singular
translations[key] = {
msgid: key,
msgstr: [
lang.translations[key],
],
}
}

gtBuilder.addTranslation(lang.locale, {
translations: {
'': translations,
},
})
})

const gt = gtBuilder.build()

const n = gt.ngettext.bind(gt)
Expand Down
21 changes: 20 additions & 1 deletion webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,28 @@ const translations = fs

const po = fs.readFileSync(path)
const json = gettextParser.po.parse(po)

// Compress translations Content
const translations = {}
for (key in json.translations['']) {
if (key !== '') {
// Plural
if ('msgid_plural'in json.translations[''][key]) {
translations[json.translations[''][key].msgid] = {
pluralId: json.translations[''][key].msgid_plural,
msgstr: json.translations[''][key].msgstr
}
continue
}

// Singular
translations[json.translations[''][key].msgid] = json.translations[''][key].msgstr[0]
}
}

return {
locale,
json,
translations,
}
})

Expand Down