Détail du package

@intlify/vue-i18n-extensions

intlify1.2mMIT6.0.0

vue-i18n extensions

extensions, i18n, optimaization, server-side-rendering

readme

:globe_with_meridians: @intlify/vue-i18n-extensions

CircleCI npm vue-i18n-extensions Dev Token

Extensions for vue-i18n

This library exports the following extensions:

:star: Features

  • directive: v-t custom directive for server-side
  • module: v-t custom directive compiler module for vue-template-compiler or vue-loader (compilerModules option)

:cd: Installation

$ npm i --save-dev @intlify/vue-i18n-extensions

:rocket: Extensions

directive: v-t custom directive for server-side

This directive is v-t custom directive for server-side-rendering. You can specify it as directives option of createRenderer function.

The following example:

import Vue from 'vue'
import VueI18n from 'vue-i18n'
import { createRenderer } from 'vue-server-renderer'
import { directive as t } from '@intlify/vue-i18n-extensions'

Vue.use(VueI18n)

const i18n = new VueI18n({
  locale: 'en',
  messages: {
    en: {
      hello: 'hello'
    },
    ja: {
      hello: 'こんにちは'
    }
  }
})
const renderer = createRenderer({ directives: { t } })

const app = new Vue({
  i18n,
  render (h) {
    // <p v-t="'hello'"></p>
    return h('p', {
      directives: [{
        name: 't', rawName: 'v-t',
        value: ('hello'), expression: "'hello'"
      }]
    })
  }
})

renderer.renderToString(app, (err, html) => {
  console.log(html) // output -> '<p data-server-rendered="true">hello</p>'
})

module: v-t custom directive compiler module

This module is v-t custom directive module for vue compiler. You can specify it as modules option of vue-template-compiler.

:warning: NOTE: This extension is not isomorphic/universal codes. for Node.js environment only.

The following example that use compile function of vue-template-compiler:

import Vue from 'vue'
import VueI18n from 'vue-i18n'
import { compile } from 'vue-template-compiler'
import { module } from '@intlify/vue-i18n-extensions'

Vue.use(VueI18n)

const i18n = new VueI18n({
  locale: 'en',
  messages: {
    en: {
      hello: 'hello'
    },
    ja: {
      hello: 'こんにちは'
    }
  },
  missing: (locale, key) => {
    console.log(`translation missing: locale=${locale}, key=${key}`)
  }
})
const i18nModule = module(i18n)

const { ast, render } = compile(`<p v-t="'hello'"></p>`, { modules: [i18nModule] })
console.log(ast.i18n) // output -> 'hello'
console.log(render) // output -> `with(this){return _c('p',{domProps:{"textContent":_s("hello")}})}`

The following configration example of vue-loader:

const Vue = require('vue')
const VueI18n = require('vue-i18n')
const i18nExtensions = require('@intlify/vue-i18n-extensions')
const messages = require('./locales.json')

Vue.use(VueI18n)

const i18n = new VueI18n({
  locale: 'ja',
  messages: messages
})

module.exports = {
  module: {
    rules: [{
      test: /\.vue$/,
      loader: 'vue',
      options: {
        compilerModules: [i18nExtensions.module(i18n)],
        // other vue-loader options go here
        loaders: {}
      }
    }]
  }
}

:scroll: Changelog

Details changes for each release are documented in the CHANGELOG.md.

:exclamation: Issues

Please make sure to read the Issue Reporting Checklist before opening an issue. Issues not conforming to the guidelines may be closed immediately.

:muscle: Contribution

Please make sure to read the Contributing Guide before making a pull request.

:copyright: License

MIT

changelog

v1.0.2 (2020-05-07)

:bug: Bug Fixes

  • #39 fix: v-t argument evaluation (@kazupon)
  • #32 Added whitespace trim to handle oddly formatted vue templates (@ewohlken2)

Committers: 2

v1.0.1 (2020-02-27)

:bug: Bug Fixes

  • #30 Update ecma keyword detection regex to not misfire on strings containing periods (@ewohlken2)

Committers: 1

v1.0.0 (2020-01-21)

:boom: Breaking Change

Committers: 2

0.2.1 (2019-03-12)

:lock: Security Fixes

  • util: XSS vulnerability (5f00336)

0.2.0 (2018-08-11)

:bug: Bug Fixes

  • fix cannot resolve dependency (c620eda), closes #2

0.1.0 (2017-09-22)

:star: New Features

  • i18n compiler module (578839c)
  • vue-i18n custom directive for server-side-rendering (11f6d01)

:up: Updates