Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to config vue-i18n v-t directive with nuxt ? #1826

Closed
alanaasmaa opened this issue Oct 11, 2017 · 7 comments
Closed

How to config vue-i18n v-t directive with nuxt ? #1826

alanaasmaa opened this issue Oct 11, 2017 · 7 comments
Labels

Comments

@alanaasmaa
Copy link

alanaasmaa commented Oct 11, 2017

I just cant figure it out.

Here is an example:

import Vue from 'vue'
import VueI18n from 'vue-i18n'
import { createRenderer } from 'vue-server-renderer'
import { directive as t } from '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>'
})

https://github.com/kazupon/vue-i18n-extensions

This question is available on Nuxt.js community (#c1641)
@cretueusebiu
Copy link

Why don't just use {{ $t('hello') }} ? It will render on the server side too.

@alanaasmaa
Copy link
Author

@cretueusebiu I'm using now but i wanna PERFORMANCEEE http://kazupon.github.io/vue-i18n/en/directive.html#t-vs-v-t

@kazupon
Copy link
Member

kazupon commented Oct 12, 2017

You can check the example. 👀
https://github.com/kazupon/vue-i18n/tree/dev/examples/ssr/nuxt

@kazupon
Copy link
Member

kazupon commented Oct 12, 2017

@alanaasmaa
Copy link
Author

alanaasmaa commented Oct 12, 2017

@kazupon Thanks i got it to work now, how could i load i18n as a plugin ? Is it somehow possible ?

I would like to use vuex to define locale option. But i can't access store in config.

export default ({app, store}) => {
  app.$i18n = new VueI18n({
    locale: store.state.locale,
    fallbackLocale: 'en',
    messages: {
      'fi': require('~/locales/fi.json'),
      'en': require('~/locales/en.json')
    }
  })
}

EDIT: I can't keep the locale without refresh, and adonisjs sends to vuex the locale that is set from server

EDIT2: It seems i have to wire a module for i18n ? But how can i access i18n plugin in module.

My code goes like this =>

1. AdonisJS sends current locale to Nuxt in request
2. Vuex Store then saves lang from request on nuxtServerInit => https://nuxtjs.org/guide/vuex-store/#the-nuxtserverinit-action
3. i18n plugin loads and gets current locale from store => this is the place im stuck...

@paulmelero
Copy link
Contributor

paulmelero commented Nov 4, 2017

Accessing the store, @alanaasmaa:

export default ({ app, isClient, store }) => {
    app.i18n = new VueI18n({
        locale: store.state.locale,
        fallbackLocale: 'en',
        messages
    });
};

A little bit different than what the author of vue-i18n, @kazupon, suggests in:

kazupon/vue-i18n@44d4ebe#diff-e635ad7197be34317008bbbec46c7b7b

@lock
Copy link

lock bot commented Nov 2, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 2, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants