Skip to content

Commit

Permalink
Merge pull request #556 from nextcloud/fix-standalone-register
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv committed Jan 19, 2023
2 parents 4f53d4f + f801f7d commit 0fe2ab4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
}
}

Expand Down
62 changes: 62 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
getCanonicalLocale,
translate,
translatePlural,
register,
_unregister,
} from '../lib/index'

const setLocale = (locale) => document.documentElement.setAttribute('data-locale', locale)
Expand Down Expand Up @@ -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')
})
})

0 comments on commit 0fe2ab4

Please sign in to comment.