nuxt-i18n
Advanced tools
Comparing version 6.27.2 to 6.27.3
{ | ||
"name": "nuxt-i18n", | ||
"version": "6.27.2", | ||
"version": "6.27.3", | ||
"description": "i18n for Nuxt", | ||
@@ -100,3 +100,3 @@ "license": "MIT", | ||
"lodash.merge": "^4.6.2", | ||
"ufo": "^0.7.5", | ||
"ufo": "^0.7.7", | ||
"vue-i18n": "^8.24.5" | ||
@@ -109,16 +109,16 @@ }, | ||
"@nuxt/types": "2.15.7", | ||
"@nuxtjs/composition-api": "0.24.4", | ||
"@nuxtjs/composition-api": "0.24.6", | ||
"@nuxtjs/eslint-config-typescript": "6.0.1", | ||
"@nuxtjs/module-test-utils": "1.6.3", | ||
"@release-it/conventional-changelog": "3.0.1", | ||
"@types/argparse": "2.0.8", | ||
"@types/cookie": "0.4.0", | ||
"@types/express": "4.17.12", | ||
"@types/jest": "26.0.23", | ||
"@types/jest-dev-server": "4.2.0", | ||
"@types/js-cookie": "2.2.6", | ||
"@types/jsdom": "16.2.11", | ||
"@types/argparse": "2.0.10", | ||
"@types/cookie": "0.4.1", | ||
"@types/express": "4.17.13", | ||
"@types/jest": "26.0.24", | ||
"@types/jest-dev-server": "4.2.1", | ||
"@types/js-cookie": "2.2.7", | ||
"@types/jsdom": "16.2.13", | ||
"@types/lodash.merge": "4.6.6", | ||
"@types/request-promise-native": "1.0.17", | ||
"@types/serve-static": "1.13.9", | ||
"@types/request-promise-native": "1.0.18", | ||
"@types/serve-static": "1.13.10", | ||
"argparse": "2.0.1", | ||
@@ -128,7 +128,7 @@ "babel-core": "7.0.0-bridge.0", | ||
"codecov": "3.8.2", | ||
"core-js": "3.15.1", | ||
"eslint": "7.29.0", | ||
"core-js": "3.15.2", | ||
"eslint": "7.30.0", | ||
"express": "4.17.1", | ||
"husky": "4.3.8", | ||
"jest": "27.0.5", | ||
"jest": "27.0.6", | ||
"jest-dev-server": "5.0.3", | ||
@@ -138,7 +138,7 @@ "jsdom": "16.6.0", | ||
"nuxt": "2.15.7", | ||
"playwright-chromium": "1.12.2", | ||
"playwright-chromium": "1.12.3", | ||
"release-it": "14.10.0", | ||
"serve-static": "1.14.1", | ||
"typescript": "^4.3.4" | ||
"typescript": "^4.3.5" | ||
} | ||
} |
@@ -0,1 +1,2 @@ | ||
import { withoutTrailingSlash, withTrailingSlash } from 'ufo' | ||
import { STRATEGIES } from './constants' | ||
@@ -152,3 +153,3 @@ import { extractComponentOptions } from './components' | ||
if (path.length) { // Don't replace empty (child) path with a slash! | ||
path = path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || (isChildWithRelativePath ? '' : '/') | ||
path = (trailingSlash ? withTrailingSlash(path, true) : withoutTrailingSlash(path, true)) || (isChildWithRelativePath ? '' : '/') | ||
} | ||
@@ -155,0 +156,0 @@ |
@@ -96,2 +96,3 @@ import { resolve, join } from 'path' | ||
this.options.alias['~i18n-klona'] = require.resolve('klona/full').replace(/\.js$/, '.mjs') | ||
this.options.alias['~i18n-ufo'] = require.resolve('ufo').replace(/\.js$/, '.mjs') | ||
@@ -98,0 +99,0 @@ if (!Array.isArray(this.options.router.middleware)) { |
import Vue from 'vue' | ||
import VueI18n from 'vue-i18n' | ||
import { joinURL } from 'ufo' | ||
import { nuxtI18nHead, nuxtI18nSeo } from './head-meta' | ||
@@ -11,11 +10,10 @@ import { Constants, nuxtOptions, options } from './options' | ||
getLocalesRegex, | ||
resolveBaseUrl, | ||
matchBrowserLocale, | ||
parseAcceptLanguage, | ||
registerStore, | ||
setLocaleCookie, | ||
syncVuex | ||
setLocaleCookie | ||
} from './utils-common' | ||
import { loadLanguageAsync } from './utils' | ||
import { loadLanguageAsync, resolveBaseUrl, registerStore, syncVuex } from './plugin.utils' | ||
// @ts-ignore | ||
import { joinURL } from '~i18n-ufo' | ||
// @ts-ignore | ||
import { klona } from '~i18n-klona' | ||
@@ -22,0 +20,0 @@ |
import './middleware' | ||
import Vue from 'vue' | ||
import { Constants, nuxtOptions, options } from './options' | ||
import { getDomainFromLocale } from './utils-common' | ||
import { getDomainFromLocale } from './plugin.utils' | ||
// @ts-ignore | ||
import { withoutTrailingSlash, withTrailingSlash } from '~i18n-ufo' | ||
@@ -89,3 +91,3 @@ /** | ||
} | ||
localizedRoute.path = localizedRoute.path.replace(/\/+$/, '') + (nuxtOptions.trailingSlash ? '/' : '') || '/' | ||
localizedRoute.path = nuxtOptions.trailingSlash ? withTrailingSlash(localizedRoute.path, true) : withoutTrailingSlash(localizedRoute.path, true) | ||
} | ||
@@ -92,0 +94,0 @@ } else { |
import Cookie from 'cookie' | ||
import JsCookie from 'js-cookie' | ||
import isHTTPS from 'is-https' | ||
import { hasProtocol } from 'ufo' | ||
@@ -89,55 +87,2 @@ /** @typedef {import('../../types/internal').ResolvedOptions} ResolvedOptions */ | ||
/** | ||
* Resolves base URL value if provided as function. Otherwise just returns verbatim. | ||
* | ||
* @param {string | ((context: import('@nuxt/types').Context) => string)} baseUrl | ||
* @param {import('@nuxt/types').Context} context | ||
* @param {import('../../types').Locale} localeCode | ||
* @param {Pick<ResolvedOptions, 'differentDomains' | 'normalizedLocales'>} options | ||
* @return {string} | ||
*/ | ||
export function resolveBaseUrl (baseUrl, context, localeCode, { differentDomains, normalizedLocales }) { | ||
if (typeof baseUrl === 'function') { | ||
return baseUrl(context) | ||
} | ||
if (differentDomains && localeCode) { | ||
// Lookup the `differentDomain` origin associated with given locale. | ||
const domain = getDomainFromLocale(localeCode, context.req, { normalizedLocales }) | ||
if (domain) { | ||
return domain | ||
} | ||
} | ||
return baseUrl | ||
} | ||
/** | ||
* Gets the `differentDomain` domain from locale. | ||
* | ||
* @param {string} localeCode | ||
* @param {import('http').IncomingMessage | undefined} req | ||
* @param {Pick<ResolvedOptions, 'normalizedLocales'>} options | ||
* @return {string | undefined} | ||
*/ | ||
export function getDomainFromLocale (localeCode, req, { normalizedLocales }) { | ||
// Lookup the `differentDomain` origin associated with given locale. | ||
const lang = normalizedLocales.find(locale => locale.code === localeCode) | ||
if (lang && lang.domain) { | ||
if (hasProtocol(lang.domain)) { | ||
return lang.domain | ||
} | ||
let protocol | ||
if (process.server) { | ||
protocol = (req && isHTTPS(req)) ? 'https' : 'http' | ||
} else { | ||
protocol = window.location.protocol.split(':')[0] | ||
} | ||
return `${protocol}://${lang.domain}` | ||
} | ||
// eslint-disable-next-line no-console | ||
console.warn(formatMessage(`Could not find domain name for locale ${localeCode}`)) | ||
} | ||
/** | ||
* Get locale code that corresponds to current hostname | ||
@@ -278,136 +223,1 @@ * | ||
} | ||
/** | ||
* @param {import('vuex').Store<Record<string, boolean>>} store | ||
* @param {Required<import('../../types').VuexOptions>} vuex | ||
* @param {readonly string[]} localeCodes | ||
*/ | ||
export function registerStore (store, vuex, localeCodes) { | ||
/** @typedef {{ | ||
* locale?: string | ||
* messages?: Record<string, string> | ||
* routeParams?: Record<string, Record<string, string>> | ||
* }} ModuleStore | ||
* | ||
* @type {import('vuex').Module<ModuleStore, {}>} | ||
*/ | ||
const storeModule = { | ||
namespaced: true, | ||
state: () => ({ | ||
...(vuex.syncLocale ? { locale: '' } : {}), | ||
...(vuex.syncMessages ? { messages: {} } : {}), | ||
...(vuex.syncRouteParams ? { routeParams: {} } : {}) | ||
}), | ||
actions: { | ||
...(vuex.syncLocale | ||
? { | ||
setLocale ({ commit }, locale) { | ||
commit('setLocale', locale) | ||
} | ||
} | ||
: {}), | ||
...(vuex.syncMessages | ||
? { | ||
setMessages ({ commit }, messages) { | ||
commit('setMessages', messages) | ||
} | ||
} | ||
: {}), | ||
...(vuex.syncRouteParams | ||
? { | ||
setRouteParams ({ commit }, params) { | ||
if (process.env.NODE_ENV === 'development') { | ||
validateRouteParams(params, localeCodes) | ||
} | ||
commit('setRouteParams', params) | ||
} | ||
} | ||
: {}) | ||
}, | ||
mutations: { | ||
...(vuex.syncLocale | ||
? { | ||
setLocale (state, locale) { | ||
state.locale = locale | ||
} | ||
} | ||
: {}), | ||
...(vuex.syncMessages | ||
? { | ||
setMessages (state, messages) { | ||
state.messages = messages | ||
} | ||
} | ||
: {}), | ||
...(vuex.syncRouteParams | ||
? { | ||
setRouteParams (state, params) { | ||
state.routeParams = params | ||
} | ||
} | ||
: {}) | ||
}, | ||
getters: { | ||
...(vuex.syncRouteParams | ||
? { | ||
localeRouteParams: ({ routeParams }) => { | ||
/** @type {(locale: string) => Record<string, string>} */ | ||
const paramsGetter = locale => (routeParams && routeParams[locale]) || {} | ||
return paramsGetter | ||
} | ||
} | ||
: {}) | ||
} | ||
} | ||
store.registerModule(vuex.moduleName, storeModule, { preserveState: !!store.state[vuex.moduleName] }) | ||
} | ||
/** | ||
* Dispatch store module actions to keep it in sync with app's locale data | ||
* | ||
* @param {import('vuex').Store<void>} store | ||
* @param {string | null} locale The current locale | ||
* @param {object | null} messages Current messages | ||
* @param {ResolvedOptions['vuex']} vuex | ||
* @return {Promise<void>} | ||
*/ | ||
export async function syncVuex (store, locale = null, messages = null, vuex) { | ||
if (vuex && store) { | ||
if (locale !== null && vuex.syncLocale) { | ||
await store.dispatch(vuex.moduleName + '/setLocale', locale) | ||
} | ||
if (messages !== null && vuex.syncMessages) { | ||
await store.dispatch(vuex.moduleName + '/setMessages', messages) | ||
} | ||
} | ||
} | ||
/** | ||
* @param {any} value | ||
* @return {boolean} | ||
*/ | ||
const isObject = value => value && !Array.isArray(value) && typeof value === 'object' | ||
/** | ||
* Validate setRouteParams action's payload | ||
* | ||
* @param {object} routeParams The action's payload | ||
* @param {readonly string[]} localeCodes | ||
*/ | ||
export function validateRouteParams (routeParams, localeCodes) { | ||
if (!isObject(routeParams)) { | ||
// eslint-disable-next-line no-console | ||
console.warn(formatMessage('Route params should be an object')) | ||
return | ||
} | ||
for (const [key, value] of Object.entries(routeParams)) { | ||
if (!localeCodes.includes(key)) { | ||
// eslint-disable-next-line no-console | ||
console.warn(formatMessage(`Trying to set route params for key ${key} which is not a valid locale`)) | ||
} else if (!isObject(value)) { | ||
// eslint-disable-next-line no-console | ||
console.warn(formatMessage(`Trying to set route params for locale ${key} with a non-object value`)) | ||
} | ||
} | ||
} |
83479
2096
Updatedufo@^0.7.7