diff --git a/README.md b/README.md index bcac050..ae4be31 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,17 @@ resolve: async lang => { } ```` +### With SSR + +The `resolve` method can receive a `require` instead of a `Promise`: + +```js +.use(i18nVue, { + lang: 'pt', + resolve: lang => require(`../../lang/${lang}.json`), +}) +```` + ### Usage ```html diff --git a/src/index.ts b/src/index.ts index d51da85..dbf29f4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import { reactive, Plugin, computed, ComputedRef } from 'vue' import { OptionsInterface } from './interfaces/options' import { LanguageInterface } from './interfaces/language' +import { LanguageJsonFileInterface } from './interfaces/language-json-file' import { ReplacementsInterface } from './interfaces/replacements' import { choose } from './pluralization' @@ -51,8 +52,7 @@ export function loadLanguageAsync(lang: string): Promise { return Promise.resolve(setLanguage(loadedLang)) } - return options - .resolve(lang) + return resolveLang(options.resolve, lang) .then(({ default: messages }) => { const data: LanguageInterface = { lang, messages } loaded.push(data) @@ -134,6 +134,19 @@ function setLanguage({ lang, messages }: LanguageInterface): string { return lang } +/** + * It resolves the language file or data, from direct data, require or Promise. + */ +function resolveLang(callable: Function, lang: string): Promise { + const data = callable(lang) + + if (data instanceof Promise) { + return data + } + + return new Promise((resolve) => resolve({ default: data })) +} + /** * Make the place-holder replacements on a line. */ diff --git a/test/fixtures/lang/en.json b/test/fixtures/lang/en.json index fa7a1da..f10f7c8 100644 --- a/test/fixtures/lang/en.json +++ b/test/fixtures/lang/en.json @@ -1,6 +1,6 @@ { "Welcome!": "Wecome!", - "Welcome, :name!": "Bem-vindo, :name!", + "Welcome, :name!": "Welcome, :name!", "Only Available on EN": "Only Available on EN", "{1} :count minute ago|[2,*] :count minutes ago": "{1} :count minute ago|[2,*] :count minutes ago" } diff --git a/test/translate.test.ts b/test/translate.test.ts index 805ee96..2928a2c 100644 --- a/test/translate.test.ts +++ b/test/translate.test.ts @@ -107,3 +107,32 @@ it('checks if watching translation works', async () => { expect(translated.value) .toBe('Wecome!') }) + +it('resolves translated data without Promise', async () => { + const wrapper = mount({ template: `

{{ $t('Welcome!') }}

` }, { + global: { + plugins: [[i18nVue, { + resolve: () => ({ 'Foo': 'Bar' }), + }]] + } + }); + + await new Promise(resolve => setTimeout(resolve)); + + expect(trans('Foo')).toBe('Bar'); +}); + +it('resolves translated data with require', async () => { + const wrapper = mount({ template: `

{{ $t('Welcome!') }}

` }, { + global: { + plugins: [[i18nVue, { + lang: 'pt', + resolve: (lang) => require(`./fixtures/lang/${lang}.json`), + }]] + } + }); + + await new Promise(resolve => setTimeout(resolve)); + + expect(trans('Welcome!')).toBe('Bem-vindo!'); +});