Skip to content

Commit

Permalink
feat: pass nuxt context to loadLanguageAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
derz committed Apr 24, 2019
1 parent 977bcbd commit 3834899
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/lazy-load-translations.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Language file example:
```js
// lang/[lang].js

export default () => {
export default (context) => {
return new Promise(function (resolve) {
resolve({
welcome: 'Welcome'
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { nuxtI18nSeo } from './seo-head'

Vue.use(VueI18n)

export default async ({ app, route, store, req }) => {
export default async (context) => {
const { app, route, store, req } = context;

// Options
const lazy = <%= options.lazy %>
const vuex = <%= JSON.stringify(options.vuex) %>
Expand Down Expand Up @@ -88,7 +90,7 @@ export default async ({ app, route, store, req }) => {
// Lazy-load translations
if (lazy) {
const { loadLanguageAsync } = require('./utils')
const messages = await loadLanguageAsync(app.i18n, app.i18n.locale)
const messages = await loadLanguageAsync(context, app.i18n.locale)
syncVuex(locale, messages)
return messages
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/templates/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import cookie from 'cookie'
import Cookies from 'js-cookie'
import middleware from '../middleware'

middleware['i18n'] = async ({ app, req, res, route, store, redirect, isHMR }) => {
middleware['i18n'] = async (context) => {
const { app, req, res, route, store, redirect, isHMR } = context;

if (isHMR) {
return
}
Expand Down Expand Up @@ -83,7 +85,7 @@ middleware['i18n'] = async ({ app, req, res, route, store, redirect, isHMR }) =>
// Lazy-loading enabled
if (lazy) {
const { loadLanguageAsync } = require('./utils')
const messages = await loadLanguageAsync(app.i18n, newLocale)
const messages = await loadLanguageAsync(context, newLocale)
app.i18n.locale = newLocale
app.i18n.onLanguageSwitched(oldLocale, newLocale)
syncVuex(newLocale, messages)
Expand Down
18 changes: 10 additions & 8 deletions src/templates/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@
* @param {String} lang Language code to load
* @return {Promise}
*/
export async function loadLanguageAsync (i18n, locale) {
export async function loadLanguageAsync (context, locale) {
const LOCALE_CODE_KEY = '<%= options.LOCALE_CODE_KEY %>'
const LOCALE_FILE_KEY = '<%= options.LOCALE_FILE_KEY %>'

if (!i18n.loadedLanguages) {
i18n.loadedLanguages = []
const { app } = context;

if (!app.i18n.loadedLanguages) {
app.i18n.loadedLanguages = []
}
if (!i18n.loadedLanguages.includes(locale)) {
const langOptions = i18n.locales.find(l => l[LOCALE_CODE_KEY] === locale)
if (!app.i18n.loadedLanguages.includes(locale)) {
const langOptions = app.i18n.locales.find(l => l[LOCALE_CODE_KEY] === locale)
if (langOptions) {
const file = langOptions[LOCALE_FILE_KEY]
if (file) {
<% if (options.langDir) { %>
try {
const module = await import(/* webpackChunkName: "lang-[request]" */ '~/<%= options.langDir %>' + file)
const messages = module.default ? module.default : module
const result = typeof messages === 'function' ? await Promise.resolve(messages()) : messages
i18n.setLocaleMessage(locale, result)
i18n.loadedLanguages.push(locale)
const result = typeof messages === 'function' ? await Promise.resolve(messages(context)) : messages
app.i18n.setLocaleMessage(locale, result)
app.i18n.loadedLanguages.push(locale)
return result
} catch (error) {
console.error(error)
Expand Down

0 comments on commit 3834899

Please sign in to comment.