Giter VIP home page Giter VIP logo

vite-plugin-vue-i18n's People

Contributors

dependabot[bot] avatar hannoeru avatar kazupon avatar lukashass avatar peteralfredlee avatar shanrauf avatar userquin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vite-plugin-vue-i18n's Issues

Add ability to import TypeScript translation file

For convenience, I want to put my translations in TypeScript files.

This allow me to:

  • get autocomplete and typecheck (I've decorated the t function on my side)
  • use one language as a base (English for example) to know which keys are valid or not and improve maintainability of other localisations
  • add comments (OK, JSON5 and YAML allow that too)
  • good/simple IDE integration

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 :)

Unable to import messages as JSON files (Vite/Typescript)

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.

esm-bundler build warning with vite 2

  • vue-i18n: ^9.0.0
  • @intlify/vite-plugin-vue-i18n: 2.x
  • vite: 2.x

Screenshots
Warning!

why 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.

importing messages not working

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?

Can support language files of .tsjs files?

Currently, only three formats of yaml json json5 are supported. Can provide other extensions or expose interfaces for users to implement by themselves

Can it support vue2 with vite-plugin-vue2? Are there any relevant examples?

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.

You are running a development build of vue-i18n

Module versions (please complete the following information):

  • vue: ^3.0.4
  • vue-i18n: ^9.0.0

Hello, 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

Regression: resolve.alias as an array do not work any more

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

"messageCompiler is not a function" after building files.

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.2
  • vite: 2.1.5

Reproduction Link

example link 👉 link

To Reproduce

  1. yarn build
  2. yarn serve
  3. just open the devtool

Expected behavior

Screenshots
image

Additional context

Uncaught SyntaxError: Not available in legacy mode

V
"vue-i18n": "^9.1.6",
"vue-router": "^4.0.8",
"@intlify/vite-plugin-vue-i18n": "^2.1.2",

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,
  },
})

main.js

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);

directory

image

error

image

feat: add default `lang` and `scope` on plugin configuration for `i18n` custom block

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.

Build production issue

Reporting a bug?

  • It's running fine on Dev mode but I got issue when I was tried to build production. Please look at the attachment below.
    Screenshot from 2021-03-24 10-13-54

Module versions (please complete the following information):

  • @intlify/vite-plugin-vue-i18n: 2.0.4
  • vite: 2.1.2

Anyone can have ideas about this issue?

Thank you.

Automatic bundling for production fails in monorepo

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:

build_err

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:

if (env === 'production') {
partialConfig.alias['vue-i18n'] =
'node_modules/vue-i18n/dist/vue-i18n.runtime.esm-bundler.js'
debug('set vue-i18n runtime only')
}

If I remove node_modules/ from the alias path it works correctly in both a monorepo and a standard project structure.

Empty messages object

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...

Config files

These are the configurations I'm using:

  • vite.config.ts:
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/"),
    }),
  ],
  • tsconfig.json:
{
  "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"]
}
  • package.json:
  "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"
  }

Code

  • main.ts:
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");
  • App.vue:
<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>

Console output

Screenshot 2021-02-22 at 20 40 49

Other stuff

  • The url http://localhost:3000/@id/@intlify/vite-plugin-vue-i18n/messages gives:
export default {
  
}
  • If I replace the 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!

SyntaxError: Need to install with `app.use` function

error
image

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,
  },
})

resource
image

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"
  }
}

i have to restart vite server

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.2
  • vite: 2.2.3

You can use reproduction CodeSandbox template: https://codesandbox.io/s/vue-i18n-9-template-h28c0

To Reproduce
Steps to reproduce the behavior:
ex:

  1. Start vite server
  2. Add a new translation to files

Expected behavior
Take the new translation string

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.