intlify / vite-plugin-vue-i18n Goto Github PK
View Code? Open in Web Editor NEW:globe_with_meridians: Vite plugin for Vue I18n
License: MIT License
:globe_with_meridians: Vite plugin for Vue I18n
License: MIT License
Was working fine in beta.12
[vite]: Rollup failed to resolve import "@intlify/shared" from "node_modules/vue-i18n/dist/vue-i18n.runtime.esm-bundler.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
Repro: https://github.com/antfu/repro-vite-plugin-vue-i18n-1
For convenience, I want to put my translations in TypeScript files.
This allow me to:
t
function on my side)So, can I just use TypeScript files and get them eat by the vite plugin? Or should I migrate to JSON5 or something, and create a type for my translation keys?
Thank you :)
Hello. I am migrating a project from Vue2 to Vue3 and all locales files were stored in JSON format in /src/locales folder.
In order to import them I attempted to use the recommended solution import messages from @intlify/vite-plugin-vue-i18n/messages
, which sadly does not seem to work because @intlify/vite-plugin-vue-i18n/messages is missing module/type delarations. The error message is "Cannot find module '@intlify/vite-plugin-vue-i18n/messages' or its corresponding type declarations.ts(2307)".
When I try to run it, I get "Cannot read property 'message' of undefined" error message.
I am using the following versions: vue-i18n: ^9.0.0-rc.7, @intlify/vite-plugin-vue-i18n: ^2.0.0-rc.1, vue: ^3.0.5,
Could you please advise if I am missing something or if there is a way to bypass it.
Thank you very much.
vue-i18n
: ^9.0.0@intlify/vite-plugin-vue-i18n
: 2.xvite
: 2.xwhy we are seeing this warning with vite 2.x?
You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.
it should be solved.
Module versions :
@intlify/vite-plugin-vue-i18n
: 2.1.0vite
: ~2.0.5vue
: "^3.0.5"vue-i18n
: "^9.1.0"Hello,
I have problem when i upload my website.
https://compassionate-euclid-d98a00.netlify.app/
I am encountering a trouble : Uncaught ReferenceError: INTLIFY_PROD_DEVTOOLS is not defined
Is there a problem in my configuration ?
Hi, I'm trying to import my messages using the plugin but it's not working. This is my setup:
(The application has been copied from the bananas example of the plugin repository)
main.ts
import { createApp } from 'vue'
import App from './App.vue'
import './index.css'
import router from './router'
import axios from 'axios'
import VueAxios from 'vue-axios'
import { createI18n } from 'vue-i18n'
import messages from "@intlify/vite-plugin-vue-i18n/messages";
// https://www.npmjs.com/package/@intlify/vite-plugin-vue-i18n
const i18n = createI18n({
legacy: false,
locale: 'en',
messages
})
let app = createApp(App)
app.use(router)
app.use(VueAxios, axios)
app.use(i18n)
app.mount('#app')
tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"jest",
"vite/client",
"@types/jest",
"vite-plugin-pwa/client",
"cypress",
"@intlify/vite-plugin-vue-i18n/client"
],
"paths": {
"@/*": ["src/*"]
},
"lib": ["esnext", "dom", "dom.iterable", "scripthost", "WebWorker"]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"src/tests/**/*.ts",
"src/tests/**/*.tsx"
],
"exclude": ["node_modules"]
}
vite.config.ts
import {UserConfig, defineConfig, UserConfigExport} from 'vite'
import Vue from '@vitejs/plugin-vue'
import { VitePWA } from 'vite-plugin-pwa'
import replace from '@rollup/plugin-replace'
import path from 'path'
import vueI18n from '@intlify/vite-plugin-vue-i18n'
const srcPath = path.resolve(__dirname, './src');
const config: UserConfigExport = defineConfig({
resolve: {
alias: [
{find: "@", replacement: path.resolve(__dirname, 'src')}
],
// optimizeDeps: {
// include: [
// "javascript-time-ago/locale/de"
// ],
// },
},
// base: process.env.BASE_URL || 'https://github.com/',
build: {
sourcemap: process.env.SOURCE_MAP === 'true',
},
optimizeDeps: {
include: ['swiper/vue'],
},
plugins: [
Vue(),
vueI18n({
include: path.resolve(__dirname, './**/locales/**'),
}),
VitePWA({
mode: 'development',
srcDir: 'src',
filename: 'sw.ts',
base: '/',
strategies: 'injectManifest',
}),
replace({
preventAssignment: true,
'process.browser': true,
__DATE__: new Date().toISOString(),
}),
],
})
export default config
package.json
{
"name": "vue",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"test": "jest src",
"build": "vite build",
"test:e2e": "start-server-and-test dev http-get://localhost:3000 cypress",
"cypress": "cypress run",
"lint:script": "eslint --ext .ts,vue --ignore-path .gitignore .",
"lint:style": "stylelint src/**/*.{css,scss,vue}",
"lint:markup": "vti diagnostics",
"https-preview": "serve dist"
},
"dependencies": {
"axios": "^0.21.1",
"swiper": "^6.5.7",
"typescript": "^4.2.4",
"vue": "^3.0.4",
"vue-axios": "^3.2.4",
"vue-i18n": "^9.1.6",
"vue-router": "4"
},
"devDependencies": {
"@intlify/vite-plugin-vue-i18n": "^2.1.2",
"@rollup/plugin-replace": "^2.4.2",
"@types/jest": "^26.0.22",
"@types/debug": "^4.1.5",
"@types/jsdom": "^16.2.5",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"@vitejs/plugin-vue": "^1.2.1",
"@vue/compiler-sfc": "^3.0.4",
"@vue/eslint-config-prettier": "^6.0.0",
"@vue/eslint-config-typescript": "^7.0.0",
"@vue/test-utils": "^2.0.0-rc.6",
"cypress": "^7.1.0",
"eslint": "^7.25.0",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-vue": "^7.9.0",
"https-localhost": "^4.6.4",
"husky": "^6.0.0",
"jest": "^26.6.3",
"lint-staged": "^10.5.4",
"prettier": "^2.2.1",
"sass": "^1.32.10",
"start-server-and-test": "^1.12.1",
"stylelint": "^13.12.0",
"stylelint-config-recommended": "^4.0.0",
"stylelint-config-standard": "^21.0.0",
"ts-jest": "^26.5.5",
"typescript-eslint-language-service": "^4.1.3",
"vite": "^2.1.5",
"vite-plugin-pwa": "^0.7.2",
"vti": "^0.1.1",
"vue-jest": "^5.0.0-alpha.7"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{ts,vue}": "eslint --fix",
"*.{css,scss,vue}": "stylelint --fix",
"*": "prettier -w -u"
}
}
Can support language files of .ts
|js
files?
Currently, only three formats of yaml json json5 are supported. Can provide other extensions or expose interfaces for users to implement by themselves
Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
Describe the solution you'd like
A clear and concise description of what you want to happen.
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
Module versions (please complete the following information):
vue
: ^3.0.4vue-i18n
: ^9.0.0Hello, I can't get rid of the following warnings after building my app:
You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.
You are running a development build of vue-i18n. Make sure to use the production build (*.prod.js) when deploying for production.
I'm using Vue 3 with Vite 2, and my locale files are in yaml (there is no . NODE_ENV
is set to production
. I heavily use $t()
in single-page components, have some calls to useI18n()
, but don't use the <i18n>
block at all.
I tried using @intlify/vite-plugin-vue-i18n
in my vite.config.ts
, but without success.
import vue from '@vitejs/plugin-vue'
import vueI18n from '@intlify/vite-plugin-vue-i18n'
const config:= {
plugins: [
vue(),
vueI18n({
// if you want to use Vue I18n Legacy API, you need to set `compositionOnly: false`
compositionOnly: false,
// you need to set i18n resource including paths !
include: path.resolve(__dirname, './src/locales/**')
}),
It's quite certainly a comprehension problem on my part
I have issue with this plugin which says TypeError: vue_i18n_1.friendlyJSONstringify is not a function.
As mentioned on rollup-alias documentation aliases can be defined by an array or an object.
// object aliases
import { defineConfig } from 'vite'
export default defineConfig({
resolve: {
alias: {
'/@src/': `${path.resolve(__dirname, 'src')}/`,
},
},
})
-- or
// array aliases
import { defineConfig } from 'vite'
export default defineConfig({
resolve: {
alias: [
{
find: '/@src/',
replacement: `${path.resolve(__dirname, 'src')}/`,
},
],
},
})
I noticed that when upgrading vite-plugin-vue-i18n
from 1.0.0-beta.16
to 2.0.0-rc.1
the array aliases notation do not work any more for the entire project
Reporting a bug?
i want to use $t
in template section and lifecycle hook functions.
so in vite.config.js
vueI18n({
include: path.resolve(__dirname, ".src/locales/**"),
compositionOnly: false,
}),
it works well in development mode.
but
yarn build
yarn serve
will report an error!
TypeError: messageCompiler is not a function
at compileMessageFormat (vendor.5894b8cb.js:4759)
at translate (vendor.5894b8cb.js:4697)
at vendor.5894b8cb.js:5182
at wrapWithDeps (vendor.5894b8cb.js:5170)
at Object.t (vendor.5894b8cb.js:5182)
at Object.t (vendor.5894b8cb.js:5571)
at Proxy.$t (vendor.5894b8cb.js:5911)
at Proxy._sfc_render (index.21a6f879.js:54)
at renderComponentRoot (vendor.5894b8cb.js:1223)
at componentEffect (vendor.5894b8cb.js:2437)
logError @ vendor.5894b8cb.js:969
Module versions (please complete the following information):
vue-i18n
: "^9.1.6"@intlify/vite-plugin-vue-i18n
: 2.1.2vite
: 2.1.5Reproduction Link
example link 👉 link
To Reproduce
Expected behavior
Screenshots
image
Additional context
V
"vue-i18n": "^9.1.6",
"vue-router": "^4.0.8",
"@intlify/vite-plugin-vue-i18n": "^2.1.2",
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import vueI18n from "@intlify/vite-plugin-vue-i18n";;
export default defineConfig({
plugins: [
vue(),
vueI18n({
// if you want to use Vue I18n Legacy API, you need to set `compositionOnly: false`
compositionOnly: false,
// you need to set i18n resource including paths !
include: path.resolve(__dirname, './src/locales/')
})],
resolve: {
alias: {
'@': path.resolve(__dirname, '/src'),
},
},
server: {
open: true,
},
})
import { createI18n } from 'vue-i18n'
import en from './locales/en.json';
import ar from './locales/ar.json';
const i18n=createI18n(
{
locale:'en',
messages:{
en,
ar
}
}
)
app.use(i18n);
Is your feature request related to a problem?
It is not a problem, it is a new feature.
Describe the solution you'd like
When using i18n
custom block resources inside the sfc
components, it is a pain writing global
and lang
attributes: provide a way on plugin configuration to specify default scope
and lang
.
When using i18n
with src
external resource, it is also a pain to add global
scope.
With these options added to plugin configuration, we have to write less code on each sfc
component.
Also, the plugin should override default ones if provided on i18n
custom block.
This feature shoundn't be present when using src
attribute of i18n
custom block: only scope
.
You can see an example for lang
on vite-plugin-pages
repo for route lang
attribute (route
is also a custom block similar to i18n
).
Describe alternatives you've considered
I have no consider alternatives, should be on this plugin.
Additional context
No more context required.
Hello! I liked this plugin and used it in the first version of vite. I would like to use this plugin in the second version of vite.
I have a project using yarn 1 workspaces, structured like this:
| root/
| -- node_modules/
| -- package.json
| -- client/
| ---- package.json
| -- server/
| ---- package.json
Root package.json
:
...
"workspaces": [
"client",
"server"
],
...
In development everything works fine, but when I try building the client I get this error:
Setting the alias manually in my own vite.config.ts
like this:
alias: {
'vue-i18n': 'vue-i18n/dist/vue-i18n.runtime.esm-bundler.js',
},
doesn't work, the plugin always overrides that with its own path.
I think the problem is here:
vite-plugin-vue-i18n/src/index.ts
Lines 61 to 65 in 1957fee
If I remove node_modules/
from the alias path it works correctly in both a monorepo and a standard project structure.
Hi!
I'm converting a project from Vue 2 to Vue 3.
The translation catalogs are in JSON format:
en.json
:
{
"key": "Value"
}
et cetera...
These are the configurations I'm using:
import path from "path";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueI18n from "@intlify/vite-plugin-vue-i18n";
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
"vue-i18n": "vue-i18n/dist/vue-i18n.runtime.esm-bundler.js",
},
},
plugins: [
vue(),
vueI18n({
include: path.resolve(__dirname, "./src/locales/"),
}),
],
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"jsx": "preserve",
"sourceMap": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"lib": ["esnext", "dom"],
"types": ["vite/client", "@intlify/vite-plugin-vue-i18n/client"],
"plugins": [{ "name": "@vuedx/typescript-plugin-vue" }]
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
}
"dependencies": {
"vue": "^3.0.5",
"vue-i18n": "^9.0.0-rc.7"
},
"devDependencies": {
"@intlify/vite-plugin-vue-i18n": "^2.0.0-rc.2",
"@types/node": "^14.14.31",
"@vitejs/plugin-vue": "^1.1.4",
"@vue/compiler-sfc": "^3.0.5",
"@vuedx/typecheck": "^0.6.0",
"@vuedx/typescript-plugin-vue": "^0.6.3",
"autoprefixer": "^10.2.4",
"postcss": "^8.2.6",
"tailwindcss": "^2.0.3",
"typescript": "^4.1.3",
"vite": "^2.0.0"
}
import { createApp } from "vue";
import { createI18n } from "vue-i18n";
import App from "./App.vue";
import "./index.css";
import messages from "@intlify/vite-plugin-vue-i18n/messages";
console.log("messages:", messages);
const i18n = createI18n({
locale: "en",
messages
});
createApp(App).use(i18n).mount("#app");
<template>
<form>
<select v-model="locale">
<option value="en">en</option>
<option value="nl">nl</option>
</select>
</form>
test: {{ t("General_Ok") }}
<HelloWorld msg="Hello Vue 3 + TypeScript + Vite" />
</template>
<script lang="ts">
import { defineComponent } from "vue";
import { useI18n } from "vue-i18n";
import HelloWorld from "./components/HelloWorld.vue";
export default defineComponent({
name: "App",
components: {
HelloWorld,
},
setup() {
const { locale, t } = useI18n({
inheritLocale: true,
});
return { locale, t };
},
});
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
http://localhost:3000/@id/@intlify/vite-plugin-vue-i18n/messages
gives:export default {
}
path.resolve()
in vite.config.js
with a string path to the locales directory, it doesn't work either...Not sure what I'm missing... probably something small :)
Can you/anyone help me out?
Thanks a lot!
template.js
<template>
<form>
<label>{{ t('language') }}</label>
<select v-model="locale">
<option value="en">en</option>
<option value="ja">ja</option>
</select>
</form>
<p>{{ t('hello') }}</p>
</template>
<script>
import { useI18n } from 'vue-i18n'
export default {
name: 'App',
setup() {
const { locale, t } = useI18n({
inheritLocale: true
})
return { locale, t }
}
}
</script>
<i18n>
{
"en": {
"language": "Language",
"hello": "hello, world!"
},
"ja": {
"language": "言語",
"hello": "こんにちは、世界!"
}
}
</i18n>
main.js
import { createApp } from 'vue'
import './tailwind.css'
import App from './App.vue'
import { routes } from './routes.js'
import { createRouter, createWebHistory } from 'vue-router'
const app = createApp(App)
const router = createRouter({
history: createWebHistory(),
routes,
})
app.use(router)
app.mount('#app')
vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import {vueI18n} from "@intlify/vite-plugin-vue-i18n";
export default defineConfig({
plugins: [vue(),
vueI18n({
// if you want to use Vue I18n Legacy API, you need to set `compositionOnly: false`
// compositionOnly: false,
// you need to set i18n resource including paths !
include: path.resolve(__dirname, './src/locales/**')
})],
resolve: {
alias: {
'@': path.resolve(__dirname, '/src'),
},
},
server: {
open: true,
},
})
package.json
{
"name": "vite2-vue3-tailwind2-starter",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"dependencies": {
"vue": "^3.0.11",
"vue-i18n": "^9.1.6",
"vue-router": "^4.0.8"
},
"devDependencies": {
"@headlessui/vue": "^1.2.0",
"@heroicons/vue": "^1.0.1",
"@intlify/vite-plugin-vue-i18n": "^2.1.2",
"@tailwindcss/aspect-ratio": "^0.2.1",
"@tailwindcss/forms": "^0.3.2",
"@tailwindcss/line-clamp": "^0.2.1",
"@tailwindcss/typography": "^0.4.1",
"@types/tailwindcss": "^2.0.3",
"@vitejs/plugin-vue": "^1.2.2",
"@vue/compiler-sfc": "^3.0.11",
"autoprefixer": "^10.2.6",
"axios": "^0.21.1",
"tailwindcss": "^2.1.2",
"vite": "^2.3.4"
}
}
Every time i add a new translation i have to reload the vite server to get it
Module versions (please complete the following information):
@intlify/vite-plugin-vue-i18n
: 2.1.2vite
: 2.2.3You can use reproduction CodeSandbox template: https://codesandbox.io/s/vue-i18n-9-template-h28c0
To Reproduce
Steps to reproduce the behavior:
ex:
Expected behavior
Take the new translation string
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.