@nuxtjs/i18n-edge 8.0.0-alpha.0-27419678.39bcf0f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -0
- package/README.md +7 -0
- package/dist/module.cjs +5 -0
- package/dist/module.d.ts +52 -0
- package/dist/module.json +5 -0
- package/dist/module.mjs +280 -0
- package/dist/runtime/bridge.plugin.d.ts +1 -0
- package/dist/runtime/bridge.plugin.mjs +54 -0
- package/dist/runtime/nuxt3.plugin.d.ts +2 -0
- package/dist/runtime/nuxt3.plugin.mjs +46 -0
- package/dist/types.d.ts +6 -0
- package/package.json +71 -0
- package/scripts/postinstall.mjs +8 -0
- package/scripts/utils.mjs +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Nuxt Community
|
|
4
|
+
Copyright (c) Rafał Chłodnicki (@rchl)
|
|
5
|
+
Copyright (c) Paul Gascou-Vaillancourt (@paulgv)
|
|
6
|
+
Copyright (c) Kazuya Kawaguchi (@kazupon)
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
in the Software without restriction, including without limitation the rights
|
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
furnished to do so, subject to the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/module.cjs
ADDED
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
import * as vue_i18n_routing from 'vue-i18n-routing';
|
|
3
|
+
import { LocaleObject, BaseUrlResolveHandler, VueI18nRoutingOptions } from 'vue-i18n-routing';
|
|
4
|
+
import { Locale, I18nOptions, Composer } from 'vue-i18n';
|
|
5
|
+
|
|
6
|
+
declare type RedirectOnOptions = 'all' | 'root' | 'no prefix';
|
|
7
|
+
interface LazyOptions {
|
|
8
|
+
skipNuxtState?: boolean;
|
|
9
|
+
}
|
|
10
|
+
interface DetectBrowserLanguageOptions {
|
|
11
|
+
alwaysRedirect?: boolean;
|
|
12
|
+
cookieCrossOrigin?: boolean;
|
|
13
|
+
cookieDomain?: string | null;
|
|
14
|
+
cookieKey?: string;
|
|
15
|
+
cookieSecure?: boolean;
|
|
16
|
+
fallbackLocale?: Locale | null;
|
|
17
|
+
redirectOn?: RedirectOnOptions;
|
|
18
|
+
useCookie?: boolean;
|
|
19
|
+
}
|
|
20
|
+
declare type LocaleInfo = {
|
|
21
|
+
path: string;
|
|
22
|
+
} & LocaleObject;
|
|
23
|
+
interface RootRedirectOptions {
|
|
24
|
+
path: string;
|
|
25
|
+
statusCode: number;
|
|
26
|
+
}
|
|
27
|
+
declare type CustomRoutePages = {
|
|
28
|
+
[key: string]: false | {
|
|
29
|
+
[key: string]: false | string;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
declare type NuxtI18nOptions<BaseUrl extends BaseUrlResolveHandler = BaseUrlResolveHandler> = {
|
|
33
|
+
langDir?: string | null;
|
|
34
|
+
lazy?: boolean | LazyOptions;
|
|
35
|
+
pages?: CustomRoutePages;
|
|
36
|
+
vueI18n?: I18nOptions | string;
|
|
37
|
+
} & Pick<VueI18nRoutingOptions<BaseUrl>, 'baseUrl' | 'strategy' | 'defaultDirection' | 'defaultLocale' | 'defaultLocaleRouteNameSuffix' | 'locales' | 'routesNameSeparator' | 'trailingSlash'>;
|
|
38
|
+
|
|
39
|
+
declare const _default: _nuxt_schema.NuxtModule<NuxtI18nOptions<vue_i18n_routing.BaseUrlResolveHandler>>;
|
|
40
|
+
|
|
41
|
+
declare module '@nuxt/kit' {
|
|
42
|
+
interface NuxtApp {
|
|
43
|
+
$i18n: Composer;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
declare module '@nuxt/schema' {
|
|
47
|
+
interface NuxtConfig {
|
|
48
|
+
i18n?: NuxtI18nOptions;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { CustomRoutePages, DetectBrowserLanguageOptions, LazyOptions, LocaleInfo, NuxtI18nOptions, RedirectOnOptions, RootRedirectOptions, _default as default };
|
package/dist/module.json
ADDED
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import createDebug from 'debug';
|
|
2
|
+
import { isString, isObject, isRegExp, isFunction, isArray } from '@intlify/shared';
|
|
3
|
+
import { resolveModule, addPluginTemplate, extendWebpackConfig, extendViteConfig, resolveFiles, extendPages, templateUtils, defineNuxtModule, isNuxt2, isNuxt3, getNuxtVersion, addTemplate } from '@nuxt/kit';
|
|
4
|
+
import { dirname, resolve, parse } from 'pathe';
|
|
5
|
+
import defu from 'defu';
|
|
6
|
+
import webpack from 'webpack';
|
|
7
|
+
import { fileURLToPath } from 'url';
|
|
8
|
+
import { vueI18n } from '@intlify/vite-plugin-vue-i18n';
|
|
9
|
+
import { localizeRoutes } from 'vue-i18n-routing';
|
|
10
|
+
import { genImport } from 'knitwork';
|
|
11
|
+
|
|
12
|
+
const distDir = dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
|
|
14
|
+
async function setupNuxtBridge(nuxt) {
|
|
15
|
+
nuxt.options.alias["vue-i18n"] = resolveModule("vue-i18n-legacy/dist/vue-i18n.esm.js", {
|
|
16
|
+
paths: nuxt.options.modulesDir
|
|
17
|
+
});
|
|
18
|
+
nuxt.options.build.transpile.push("vue-i18n");
|
|
19
|
+
const vueI18nBridgePath = nuxt.options.dev ? "vue-i18n-bridge/dist/vue-i18n-bridge.esm-bundler.js" : "vue-i18n-bridge/dist/vue-i18n-bridge-runtime.esm-bundler.js";
|
|
20
|
+
nuxt.options.alias["vue-i18n-bridge"] = resolveModule(vueI18nBridgePath, {
|
|
21
|
+
paths: nuxt.options.modulesDir
|
|
22
|
+
});
|
|
23
|
+
nuxt.options.build.transpile.push("vue-i18n-bridge");
|
|
24
|
+
addPluginTemplate({
|
|
25
|
+
filename: "runtime/bridge.plugin.mjs",
|
|
26
|
+
src: resolve(distDir, "runtime/bridge.plugin.mjs")
|
|
27
|
+
});
|
|
28
|
+
extendWebpackConfig((config) => {
|
|
29
|
+
config.plugins.push(new webpack.DefinePlugin({
|
|
30
|
+
__VUE_I18N_FULL_INSTALL__: "true",
|
|
31
|
+
__VUE_I18N_LEGACY_API__: "true",
|
|
32
|
+
__INTLIFY_PROD_DEVTOOLS__: "false"
|
|
33
|
+
}));
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function setupAutoImports(nuxt) {
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function extendBundler(hasLocaleFiles, langPath) {
|
|
41
|
+
extendViteConfig((config) => {
|
|
42
|
+
const viteOptions = {
|
|
43
|
+
compositionOnly: false
|
|
44
|
+
};
|
|
45
|
+
if (hasLocaleFiles) {
|
|
46
|
+
viteOptions["include"] = resolve(langPath, "./**");
|
|
47
|
+
}
|
|
48
|
+
config.plugins.push(vueI18n(viteOptions));
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function setupNuxt3(nuxt) {
|
|
53
|
+
const vueI18nPath = nuxt.options.dev ? "vue-i18n/dist/vue-i18n.esm-bundler.js" : "vue-i18n/dist/vue-i18n.runtime.esm-bundler.js";
|
|
54
|
+
nuxt.options.alias["vue-i18n"] = resolveModule(vueI18nPath, {
|
|
55
|
+
paths: nuxt.options.modulesDir
|
|
56
|
+
});
|
|
57
|
+
nuxt.options.build.transpile.push("vue-i18n");
|
|
58
|
+
addPluginTemplate({
|
|
59
|
+
filename: "runtime/nuxt3.plugin.mjs",
|
|
60
|
+
src: resolve(distDir, "runtime/nuxt3.plugin.mjs")
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const STRATEGY_PREFIX = "prefix";
|
|
65
|
+
const STRATEGY_PREFIX_EXCEPT_DEFAULT = "prefix_except_default";
|
|
66
|
+
const STRATEGY_PREFIX_AND_DEFAULT = "prefix_and_default";
|
|
67
|
+
const STRATEGY_NO_PREFIX = "no_prefix";
|
|
68
|
+
const STRATEGIES = {
|
|
69
|
+
PREFIX: STRATEGY_PREFIX,
|
|
70
|
+
PREFIX_EXCEPT_DEFAULT: STRATEGY_PREFIX_EXCEPT_DEFAULT,
|
|
71
|
+
PREFIX_AND_DEFAULT: STRATEGY_PREFIX_AND_DEFAULT,
|
|
72
|
+
NO_PREFIX: STRATEGY_NO_PREFIX
|
|
73
|
+
};
|
|
74
|
+
const DEFAULT_OPTIONS = {
|
|
75
|
+
vueI18n: void 0,
|
|
76
|
+
locales: [],
|
|
77
|
+
defaultLocale: "",
|
|
78
|
+
defaultDirection: "ltr",
|
|
79
|
+
routesNameSeparator: "___",
|
|
80
|
+
trailingSlash: false,
|
|
81
|
+
defaultLocaleRouteNameSuffix: "default",
|
|
82
|
+
strategy: STRATEGY_PREFIX_EXCEPT_DEFAULT,
|
|
83
|
+
langDir: null,
|
|
84
|
+
baseUrl: "",
|
|
85
|
+
pages: {}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
function getNormalizedLocales(locales) {
|
|
89
|
+
locales = locales || [];
|
|
90
|
+
const normalized = [];
|
|
91
|
+
for (const locale of locales) {
|
|
92
|
+
if (isString(locale)) {
|
|
93
|
+
normalized.push({ code: locale });
|
|
94
|
+
} else {
|
|
95
|
+
normalized.push(locale);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return normalized;
|
|
99
|
+
}
|
|
100
|
+
async function resolveLocales(path, locales) {
|
|
101
|
+
const files = await resolveFiles(path, "**/*{json,json5,yaml,yml}");
|
|
102
|
+
return files.map((file) => {
|
|
103
|
+
const parsed = parse(file);
|
|
104
|
+
const locale = findLocales(locales, parsed.base);
|
|
105
|
+
return locales == null ? {
|
|
106
|
+
path: file,
|
|
107
|
+
file: parsed.base,
|
|
108
|
+
code: parsed.name
|
|
109
|
+
} : Object.assign({ path: file }, locale);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
function findLocales(locales, filename) {
|
|
113
|
+
const ret = locales.find((locale) => isObject(locale) && locale.file === filename);
|
|
114
|
+
return ret != null ? ret : null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const debug$2 = createDebug("@nuxtjs/i18n:pages");
|
|
118
|
+
function setupPages(options, nuxt, additionalOptions = {
|
|
119
|
+
isBridge: false,
|
|
120
|
+
localeCodes: []
|
|
121
|
+
}) {
|
|
122
|
+
const { isBridge } = additionalOptions;
|
|
123
|
+
let includeUprefixedFallback = nuxt.options.target === "static";
|
|
124
|
+
nuxt.hook("generate:before", () => {
|
|
125
|
+
debug$2("called generate:before hook");
|
|
126
|
+
includeUprefixedFallback = true;
|
|
127
|
+
});
|
|
128
|
+
const pagesDir = nuxt.options.dir && nuxt.options.dir.pages ? nuxt.options.dir.pages : "pages";
|
|
129
|
+
const { trailingSlash } = nuxt.options.router;
|
|
130
|
+
debug$2(`pagesDir: ${pagesDir}, tailingSlash: ${trailingSlash}`);
|
|
131
|
+
extendPages((pages) => {
|
|
132
|
+
const localizedPages = localizeRoutes(pages, {
|
|
133
|
+
...options,
|
|
134
|
+
includeUprefixedFallback,
|
|
135
|
+
optionsResolver: getRouteOptionsResolver(options.pages, pagesDir, options.defaultLocale)
|
|
136
|
+
});
|
|
137
|
+
if (isBridge) {
|
|
138
|
+
pages.splice(0, pages.length);
|
|
139
|
+
pages.unshift(...localizedPages);
|
|
140
|
+
} else {
|
|
141
|
+
localizedPages.forEach((page) => pages.push(page));
|
|
142
|
+
}
|
|
143
|
+
debug$2("made pages ...", pages);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
function getRouteOptionsResolver(pages, pagesDir, defaultLocale) {
|
|
147
|
+
return (route, localeCodes) => {
|
|
148
|
+
const options = {
|
|
149
|
+
locales: localeCodes,
|
|
150
|
+
paths: {}
|
|
151
|
+
};
|
|
152
|
+
const pattern = new RegExp(`${pagesDir}/`, "i");
|
|
153
|
+
const chunkName = route.chunkName ? route.chunkName.replace(pattern, "") : route.name;
|
|
154
|
+
const pageOptions = chunkName ? pages[chunkName] : void 0;
|
|
155
|
+
if (pageOptions === false) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
if (!pageOptions) {
|
|
159
|
+
return options;
|
|
160
|
+
}
|
|
161
|
+
options.locales = options.locales.filter((locale) => pageOptions[locale] !== false);
|
|
162
|
+
for (const locale of options.locales) {
|
|
163
|
+
const customLocalePath = pageOptions[locale];
|
|
164
|
+
if (typeof customLocalePath === "string") {
|
|
165
|
+
options.paths[locale] = customLocalePath;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const customDefaultLocalePath = pageOptions[defaultLocale];
|
|
169
|
+
if (typeof customDefaultLocalePath === "string") {
|
|
170
|
+
options.paths[locale] = customDefaultLocalePath;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return options;
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const debug$1 = createDebug("@nuxtjs/i18n:gen");
|
|
178
|
+
function generateLoaderOptions(options = {}) {
|
|
179
|
+
const genCode = `${Object.entries(options).map(([rootKey, rootValue]) => {
|
|
180
|
+
if (rootKey === "nuxtI18nOptions") {
|
|
181
|
+
return `export const ${rootKey} = Object({${Object.entries(rootValue).map(([key, value]) => {
|
|
182
|
+
if (key === "vueI18n") {
|
|
183
|
+
return `${key}: ${isObject(value) ? toCode(value) : isString(value) ? `(context) => import(${toCode(value)}).then(r => (r.default || r)(context))` : `${toCode({})}`}`;
|
|
184
|
+
} else {
|
|
185
|
+
return `${key}: ${toCode(value)}`;
|
|
186
|
+
}
|
|
187
|
+
}).join(`,`)}})`;
|
|
188
|
+
} else if (rootKey === "localeInfo") {
|
|
189
|
+
const localeInfo = options.localeInfo || [];
|
|
190
|
+
const importMapper = new Map();
|
|
191
|
+
localeInfo.forEach(({ code }) => {
|
|
192
|
+
importMapper.set(code, templateUtils.importName(`locale_${code}`));
|
|
193
|
+
});
|
|
194
|
+
return `${localeInfo.map((l) => genImport(l.path, importMapper.get(l.code))).join(`
|
|
195
|
+
`)}
|
|
196
|
+
export const messages = () => Promise.resolve(Object({${[...importMapper].map((i) => `${templateUtils.serialize(i[0])}:${i[1]}`).join(`,`)}}))`;
|
|
197
|
+
} else {
|
|
198
|
+
return `export const ${rootKey} = ${toCode(rootValue)}`;
|
|
199
|
+
}
|
|
200
|
+
}).join("\n")}`;
|
|
201
|
+
debug$1("generate code", genCode);
|
|
202
|
+
return genCode;
|
|
203
|
+
}
|
|
204
|
+
function stringifyObj(obj) {
|
|
205
|
+
return `Object({${Object.entries(obj).map(([key, value]) => `${JSON.stringify(key)}:${toCode(value)}`).join(`,`)}})`;
|
|
206
|
+
}
|
|
207
|
+
function toCode(code) {
|
|
208
|
+
if (code === null) {
|
|
209
|
+
return `null`;
|
|
210
|
+
}
|
|
211
|
+
if (code === void 0) {
|
|
212
|
+
return `undefined`;
|
|
213
|
+
}
|
|
214
|
+
if (isString(code)) {
|
|
215
|
+
return JSON.stringify(code);
|
|
216
|
+
}
|
|
217
|
+
if (isRegExp(code) && code.toString) {
|
|
218
|
+
return code.toString();
|
|
219
|
+
}
|
|
220
|
+
if (isFunction(code) && code.toString) {
|
|
221
|
+
return `(${code.toString()})`;
|
|
222
|
+
}
|
|
223
|
+
if (isArray(code)) {
|
|
224
|
+
return `[${code.map((c) => toCode(c)).join(`,`)}]`;
|
|
225
|
+
}
|
|
226
|
+
if (isObject(code)) {
|
|
227
|
+
return stringifyObj(code);
|
|
228
|
+
}
|
|
229
|
+
return code + ``;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const debug = createDebug("@nuxtjs/i18n:module");
|
|
233
|
+
const module = defineNuxtModule({
|
|
234
|
+
meta: {
|
|
235
|
+
name: "@nuxtjs/i18n",
|
|
236
|
+
configKey: "i18n"
|
|
237
|
+
},
|
|
238
|
+
defaults: {},
|
|
239
|
+
async setup(i18nOptions, nuxt) {
|
|
240
|
+
const options = defu(i18nOptions, DEFAULT_OPTIONS);
|
|
241
|
+
options.langDir = options.langDir || "locales";
|
|
242
|
+
debug("options", options);
|
|
243
|
+
if (isNuxt2(nuxt)) {
|
|
244
|
+
await setupNuxtBridge(nuxt);
|
|
245
|
+
} else if (isNuxt3(nuxt)) {
|
|
246
|
+
await setupNuxt3(nuxt);
|
|
247
|
+
} else {
|
|
248
|
+
throw new Error(`Cannot support nuxt version: ${getNuxtVersion(nuxt)}`);
|
|
249
|
+
}
|
|
250
|
+
const langPath = resolve(nuxt.options.srcDir, options.langDir);
|
|
251
|
+
debug("langDir path", langPath);
|
|
252
|
+
const normalizedLocales = options.locales = getNormalizedLocales(options.locales);
|
|
253
|
+
const hasLocaleFiles = normalizedLocales.length > 0;
|
|
254
|
+
const localeCodes = normalizedLocales.map((locale) => locale.code);
|
|
255
|
+
const localeInfo = await resolveLocales(langPath, normalizedLocales);
|
|
256
|
+
debug("localeInfo", localeInfo);
|
|
257
|
+
options.vueI18n = isObject(options.vueI18n) ? options.vueI18n : isString(options.vueI18n) ? resolve(nuxt.options.rootDir, options.vueI18n) : {};
|
|
258
|
+
if (options.strategy !== STRATEGIES.NO_PREFIX && localeCodes.length) {
|
|
259
|
+
await setupPages(options, nuxt, { isBridge: isNuxt2(nuxt), localeCodes });
|
|
260
|
+
}
|
|
261
|
+
nuxt.options.alias["@intlify/shared"] = resolveModule("@intlify/shared/dist/shared.esm-bundler.js", {
|
|
262
|
+
paths: nuxt.options.modulesDir
|
|
263
|
+
});
|
|
264
|
+
nuxt.options.build.transpile.push("@intlify/shared");
|
|
265
|
+
nuxt.options.alias["vue-i18n-routing"] = resolveModule("vue-i18n-routing/dist/vue-i18n-routing.es.js", {
|
|
266
|
+
paths: nuxt.options.modulesDir
|
|
267
|
+
});
|
|
268
|
+
nuxt.options.build.transpile.push("vue-i18n-routing");
|
|
269
|
+
addTemplate({
|
|
270
|
+
filename: "i18n.options.mjs",
|
|
271
|
+
getContents: () => {
|
|
272
|
+
return generateLoaderOptions({ localeCodes, localeInfo, nuxtI18nOptions: options });
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
await setupAutoImports();
|
|
276
|
+
await extendBundler(hasLocaleFiles, langPath);
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
export { module as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function (context: any, inject: any): Promise<void>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import Vue from "vue";
|
|
2
|
+
import { install, ref, computed } from "vue-demi";
|
|
3
|
+
import VueI18n from "vue-i18n";
|
|
4
|
+
import { createI18n } from "vue-i18n-bridge";
|
|
5
|
+
import { createLocaleFromRouteGetter, resolveBaseUrl } from "vue-i18n-routing";
|
|
6
|
+
import { isEmptyObject } from "@intlify/shared";
|
|
7
|
+
import {
|
|
8
|
+
messages as loadMessages,
|
|
9
|
+
localeCodes,
|
|
10
|
+
nuxtI18nOptions
|
|
11
|
+
} from "#build/i18n.options.mjs";
|
|
12
|
+
install();
|
|
13
|
+
const getLocaleFromRoute = createLocaleFromRouteGetter(localeCodes, nuxtI18nOptions.routesNameSeparator, nuxtI18nOptions.defaultLocaleRouteNameSuffix);
|
|
14
|
+
export default async function(context, inject) {
|
|
15
|
+
console.log("bridge.plugin setup", context);
|
|
16
|
+
Vue.use(VueI18n, { bridge: true });
|
|
17
|
+
const messages = await loadMessages();
|
|
18
|
+
if (!isEmptyObject(messages)) {
|
|
19
|
+
nuxtI18nOptions.vueI18n.messages = messages;
|
|
20
|
+
}
|
|
21
|
+
const initialLocale = nuxtI18nOptions.vueI18n.locale;
|
|
22
|
+
const i18n = createI18n({
|
|
23
|
+
legacy: false,
|
|
24
|
+
globalInjection: true,
|
|
25
|
+
...nuxtI18nOptions.vueI18n,
|
|
26
|
+
locale: nuxtI18nOptions.defaultLocale
|
|
27
|
+
}, VueI18n);
|
|
28
|
+
Vue.use(i18n);
|
|
29
|
+
const global = i18n.global;
|
|
30
|
+
const _locales = ref(nuxtI18nOptions.locales);
|
|
31
|
+
const _localeCodes = ref(localeCodes);
|
|
32
|
+
global.locales = computed(() => _locales.value);
|
|
33
|
+
global.localeCodes = computed(() => _localeCodes.value);
|
|
34
|
+
global.__baseUrl = resolveBaseUrl(nuxtI18nOptions.baseUrl, {});
|
|
35
|
+
inject("i18n", global);
|
|
36
|
+
context.app.i18n = global;
|
|
37
|
+
if (i18n.mode === "composition" && process.client) {
|
|
38
|
+
;
|
|
39
|
+
["t", "d", "n"].forEach((key) => inject(key, (...args) => Reflect.apply(composer[key], composer, [...args])));
|
|
40
|
+
}
|
|
41
|
+
if (process.client) {
|
|
42
|
+
context.app.router.beforeEach(async (to, from, next) => {
|
|
43
|
+
const currentLocale = global.locale.value;
|
|
44
|
+
const finalLocale = getLocaleFromRoute(to) || nuxtI18nOptions.defaultLocale || initialLocale;
|
|
45
|
+
if (currentLocale !== finalLocale) {
|
|
46
|
+
global.locale.value = finalLocale;
|
|
47
|
+
}
|
|
48
|
+
next();
|
|
49
|
+
});
|
|
50
|
+
} else {
|
|
51
|
+
const finalLocale = getLocaleFromRoute(context.route) || nuxtI18nOptions.defaultLocale || initialLocale;
|
|
52
|
+
global.locale.value = finalLocale;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ref, computed } from "vue-demi";
|
|
2
|
+
import { createI18n } from "vue-i18n";
|
|
3
|
+
import { isEmptyObject } from "@intlify/shared";
|
|
4
|
+
import { createLocaleFromRouteGetter, resolveBaseUrl } from "vue-i18n-routing";
|
|
5
|
+
import { defineNuxtPlugin } from "#app";
|
|
6
|
+
import {
|
|
7
|
+
messages as loadMessages,
|
|
8
|
+
localeCodes,
|
|
9
|
+
nuxtI18nOptions
|
|
10
|
+
} from "#build/i18n.options.mjs";
|
|
11
|
+
const getLocaleFromRoute = createLocaleFromRouteGetter(localeCodes, nuxtI18nOptions.routesNameSeparator, nuxtI18nOptions.defaultLocaleRouteNameSuffix);
|
|
12
|
+
export default defineNuxtPlugin(async (nuxt) => {
|
|
13
|
+
const { vueApp: app } = nuxt;
|
|
14
|
+
console.log("nuxt.plugin setup", nuxt);
|
|
15
|
+
const messages = await loadMessages();
|
|
16
|
+
if (!isEmptyObject(messages)) {
|
|
17
|
+
nuxtI18nOptions.vueI18n.messages = messages;
|
|
18
|
+
}
|
|
19
|
+
const initialLocale = nuxtI18nOptions.vueI18n.locale;
|
|
20
|
+
const i18n = createI18n({
|
|
21
|
+
legacy: false,
|
|
22
|
+
globalInjection: true,
|
|
23
|
+
...nuxtI18nOptions.vueI18n,
|
|
24
|
+
locale: nuxtI18nOptions.defaultLocale
|
|
25
|
+
});
|
|
26
|
+
const global = i18n.global;
|
|
27
|
+
const _locales = ref(nuxtI18nOptions.locales);
|
|
28
|
+
const _localeCodes = ref(localeCodes);
|
|
29
|
+
global.locales = computed(() => _locales.value);
|
|
30
|
+
global.localeCodes = computed(() => _localeCodes.value);
|
|
31
|
+
global.__baseUrl = resolveBaseUrl(nuxtI18nOptions.baseUrl, {});
|
|
32
|
+
app.use(i18n);
|
|
33
|
+
nuxt.provide("i18n", global);
|
|
34
|
+
if (process.client) {
|
|
35
|
+
addRouteMiddleware("locale-changing", (to, from) => {
|
|
36
|
+
const currentLocale = global.locale.value;
|
|
37
|
+
const finalLocale = getLocaleFromRoute(to) || nuxtI18nOptions.defaultLocale || initialLocale;
|
|
38
|
+
if (currentLocale !== finalLocale) {
|
|
39
|
+
global.locale.value = finalLocale;
|
|
40
|
+
}
|
|
41
|
+
}, { global: true });
|
|
42
|
+
} else {
|
|
43
|
+
const finalLocale = getLocaleFromRoute(nuxt.ssrContext.url) || nuxtI18nOptions.defaultLocale || initialLocale;
|
|
44
|
+
global.locale.value = finalLocale;
|
|
45
|
+
}
|
|
46
|
+
});
|
package/dist/types.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nuxtjs/i18n-edge",
|
|
3
|
+
"description": "i18n for Nuxt",
|
|
4
|
+
"version": "8.0.0-alpha.0-27419678.39bcf0f",
|
|
5
|
+
"homepage": "https://i18n.nuxtjs.org",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/nuxt-community/i18n-module/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/nuxt-community/i18n-module.git",
|
|
12
|
+
"directory": "packages/nuxt-i18n"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"contributors": [
|
|
16
|
+
{
|
|
17
|
+
"name": "Paul Gascou-Vaillancourt (@paulgv)"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"name": "Rafal Chlodnicki (@rchl)"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"name": "Kazuya Kawaguchi (@kazupon)"
|
|
24
|
+
}
|
|
25
|
+
],
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": "./dist/module.mjs",
|
|
29
|
+
"require": "./dist/module.cjs"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"main": "./dist/module.cjs",
|
|
33
|
+
"types": "./dist/types.d.ts",
|
|
34
|
+
"type": "module",
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"scripts"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "nuxt-module-build",
|
|
41
|
+
"stub": "nuxt-module-build --stub",
|
|
42
|
+
"postinstall": "node ./scripts/postinstall.mjs"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@intlify/shared": "next",
|
|
46
|
+
"@intlify/vite-plugin-vue-i18n": "next",
|
|
47
|
+
"@intlify/vue-i18n-loader": "next",
|
|
48
|
+
"@nuxt/kit": "npm:@nuxt/kit-edge@3.0.0-27398533.8edd481",
|
|
49
|
+
"debug": "^4.3.2",
|
|
50
|
+
"defu": "latest",
|
|
51
|
+
"knitwork": "^0.1.0",
|
|
52
|
+
"mlly": "^0.3.19",
|
|
53
|
+
"pathe": "^0.2.0",
|
|
54
|
+
"vue-i18n": "beta",
|
|
55
|
+
"vue-i18n-bridge": "beta",
|
|
56
|
+
"vue-i18n-legacy": "npm:vue-i18n@8",
|
|
57
|
+
"vue-i18n-routing": "edge"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@nuxt/module-builder": "latest",
|
|
61
|
+
"@nuxt/schema": "npm:@nuxt/schema-edge@3.0.0-27398533.8edd481",
|
|
62
|
+
"@types/debug": "^4.1.7",
|
|
63
|
+
"nuxt3": "latest"
|
|
64
|
+
},
|
|
65
|
+
"engines": {
|
|
66
|
+
"node": "^14.16.0 || ^16.11.0 || ^17.0.0"
|
|
67
|
+
},
|
|
68
|
+
"publishConfig": {
|
|
69
|
+
"access": "public"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { loadModule } from './utils.mjs'
|
|
2
|
+
;(async () => {
|
|
3
|
+
const Vue = loadModule('vue')
|
|
4
|
+
if (Vue && Vue.version.startsWith('2.')) {
|
|
5
|
+
const { switchVersion } = loadModule('@intlify/vue-i18n-bridge/scripts/utils.js')
|
|
6
|
+
switchVersion(8, 'vue-i18n-legacy')
|
|
7
|
+
}
|
|
8
|
+
})()
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// import { pathToFileURL } from 'url'
|
|
2
|
+
import { createCommonJS } from 'mlly'
|
|
3
|
+
|
|
4
|
+
const NUXT_I18N_ID = '@nuxtjs/i18n'
|
|
5
|
+
|
|
6
|
+
const cjs = createCommonJS(import.meta.url)
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
const _default = r => r.default || r
|
|
10
|
+
|
|
11
|
+
// FIXME: https://github.com/microsoft/TypeScript/issues/43329
|
|
12
|
+
// module: node12 will be replace it
|
|
13
|
+
const _importDynamic = new Function('modulePath', 'return import(modulePath)')
|
|
14
|
+
function importModule(path) {
|
|
15
|
+
return _importDynamic(pathToFileURL(path).href).then(_default)
|
|
16
|
+
}
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export function loadModule(name) {
|
|
20
|
+
try {
|
|
21
|
+
return cjs.require(name)
|
|
22
|
+
} catch (e) {
|
|
23
|
+
err(e)
|
|
24
|
+
return undefined
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function err(...args) {
|
|
29
|
+
console.error(`[${NUXT_I18N_ID}] `, ...args)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function warn(...args) {
|
|
33
|
+
console.warn(`[${NUXT_I18N_ID}] `, ...args)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function log(...args) {
|
|
37
|
+
console.log(`[${NUXT_I18N_ID}] `, ...args)
|
|
38
|
+
}
|