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

Fix standalone registeration #556

Merged
merged 2 commits into from
Jan 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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),
}
}
Comment on lines 68 to 79
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update warns logic here.

Since it doesn't matter if "no app translation has been registered" or "only appId hasn't been registered", check both situations with optional chaining.

Warn is always about concrete appId, as there is no "general translation registration." It is always some app translation registration.

Also, return default stub for both translations and pluralFunction if there is no value.


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')
})
})