diff --git a/lib/registry.ts b/lib/registry.ts index 979d1228..1c7c5c41 100644 --- a/lib/registry.ts +++ b/lib/registry.ts @@ -36,6 +36,12 @@ export function registerAppTranslations( translations: Translations, pluralFunction: PluralFunction ) { + if (window._oc_l10n_registry_translations === undefined) { + window._oc_l10n_registry_translations = {} + } + if (window._oc_l10n_registry_plural_functions === undefined) { + window._oc_l10n_registry_plural_functions = {} + } if (!hasAppTranslations(appId)) { setAppTranslations(appId, translations, pluralFunction) } else { @@ -61,19 +67,14 @@ export function unregisterAppTranslations(appId: string) { */ export function getAppTranslations(appId: string): AppTranslations { if ( - typeof window._oc_l10n_registry_translations === 'undefined' - || typeof window._oc_l10n_registry_plural_functions === 'undefined' + typeof window._oc_l10n_registry_translations?.[appId] === 'undefined' + || typeof window._oc_l10n_registry_plural_functions?.[appId] === 'undefined' ) { - console.warn('No OC L10N registry found') - return { - translations: {}, - pluralFunction: (number: number) => number, - } + console.warn(`No translation for appId "${appId}" have been registered`) } - return { - translations: window._oc_l10n_registry_translations[appId] || {}, - pluralFunction: window._oc_l10n_registry_plural_functions[appId], + translations: window._oc_l10n_registry_translations?.[appId] ?? {}, + pluralFunction: window._oc_l10n_registry_plural_functions?.[appId] ?? ((number: number) => number), } } diff --git a/tests/index.test.js b/tests/index.test.js index e1db63ed..110e5749 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -2,6 +2,8 @@ import { getCanonicalLocale, translate, translatePlural, + register, + _unregister, } from '../lib/index' const setLocale = (locale) => document.documentElement.setAttribute('data-locale', locale) @@ -78,3 +80,63 @@ describe('getCanonicalLocale', () => { expect(getCanonicalLocale()).toEqual('de-DE') }) }) + +describe('register', () => { + beforeEach(() => { + setLocale('de_DE') + window._oc_l10n_registry_translations = undefined + window._oc_l10n_registry_plural_functions = undefined + }) + + it('initial', () => { + register('app', { + Application: 'Anwendung', + '_%n guest_::_%n guests_': ['%n Gast', '%n Gäste'], + }) + expect(translate('app', 'Application')).toBe('Anwendung') + expect(translatePlural('app', '%n guest', '%n guests', 1)).toBe('1 Gast') + expect(translatePlural('app', '%n guest', '%n guests', 2)).toBe('2 Gäste') + }) + + it('extend', () => { + window._oc_l10n_registry_translations = { + app: { + Application: 'Anwendung', + }, + } + window._oc_l10n_registry_plural_functions = { + app: (t) => t === 1 ? 0 : 1, + } + register('app', { + Translation: 'Übersetzung', + }) + expect(translate('app', 'Application')).toBe('Anwendung') + expect(translate('app', 'Translation')).toBe('Übersetzung') + }) + + it('with another app', () => { + window._oc_l10n_registry_translations = { + core: { + 'Hello world!': 'Hallo Welt!', + }, + } + window._oc_l10n_registry_plural_functions = { + core: (t) => t === 1 ? 0 : 1, + } + register('app', { + Application: 'Anwendung', + }) + expect(translate('core', 'Hello world!')).toBe('Hallo Welt!') + expect(translate('app', 'Application')).toBe('Anwendung') + }) + + it('unregister', () => { + window._oc_l10n_registry_translations = {} + window._oc_l10n_registry_plural_functions = {} + register('app', { + Application: 'Anwendung', + }) + _unregister('app') + expect(translate('app', 'Application')).toBe('Application') + }) +})