UNPKG

@nextcloud/l10n

Version:

Nextcloud localization and translation helpers for apps and libraries

113 lines (112 loc) 3.32 kB
import { a as getLanguage, e as getPlural, t as translate, d as translatePlural } from "./chunks/translation-DoG5ZELJ.mjs"; /*! * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: GPL-3.0-or-later */ class GettextWrapper { bundle; constructor(pluralFunction) { this.bundle = { pluralFunction, translations: {} }; } /** * Append new translations to the wrapper. * * This is useful if translations should be added on demand, * e.g. depending on component usage. * * @param bundle - The new translation bundle to append */ addTranslations(bundle) { const dict = Object.values(bundle.translations[""] ?? {}).map(({ msgid, msgid_plural: msgidPlural, msgstr }) => { if (msgidPlural !== void 0) { return [`_${msgid}_::_${msgidPlural}_`, msgstr]; } return [msgid, msgstr[0]]; }); this.bundle.translations = { ...this.bundle.translations, ...Object.fromEntries(dict) }; } /** * Get translated string (singular form), optionally with placeholders * * @param original original string to translate * @param placeholders map of placeholder key to value */ gettext(original, placeholders = {}) { return translate("", original, placeholders, void 0, { bundle: this.bundle }); } /** * Get translated string with plural forms * * @param singular Singular text form * @param plural Plural text form to be used if `count` requires it * @param count The number to insert into the text * @param placeholders optional map of placeholder key to value */ ngettext(singular, plural, count, placeholders = {}) { return translatePlural("", singular, plural, count, placeholders, { bundle: this.bundle }); } } class GettextBuilder { debug = false; language = "en"; translations = {}; setLanguage(language) { this.language = language; return this; } /** * Try to detect locale from context with `en` as fallback value * This only works within a Nextcloud page context. * * @deprecated use `detectLanguage` instead. */ detectLocale() { return this.detectLanguage(); } /** * Try to detect locale from context with `en` as fallback value. * This only works within a Nextcloud page context. */ detectLanguage() { return this.setLanguage(getLanguage().replace("-", "_")); } /** * Register a new translation bundle for a specified language. * * Please note that existing translations for that language will be overwritten. * * @param language - Language this is the translation for * @param data - The translation bundle */ addTranslation(language, data) { this.translations[language] = data; return this; } enableDebugMode() { this.debug = true; return this; } build() { if (this.debug) { console.debug(`Creating gettext instance for language ${this.language}`); } const wrapper = new GettextWrapper((n) => getPlural(n, this.language)); if (this.language in this.translations) { wrapper.addTranslations(this.translations[this.language]); } return wrapper; } } function getGettextBuilder() { return new GettextBuilder(); } export { getGettextBuilder }; //# sourceMappingURL=gettext.mjs.map