martinlindhe / laravel-vue-i18n-generator

Generates a vue-i18n compatible include file from your Laravel translations
MIT License
306 stars 207 forks source link

Improving the README #40

Closed JValck closed 6 years ago

JValck commented 6 years ago

The readme is quiet outdated. Maybe it's not a bad idea to give it an update? Below is an excerpt from the app.js. I updated it so others can find a small boilerplate for starting with this project easily. I'm also referencing #27 and #29 as those issues were similar to mine before getting it to work properly.

As you can see I'm using vuex-i18n.

:bulb: Code

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */
window.Vue = require('vue');

import Vuex from 'vuex';
import VuexI18n from 'vuex-i18n';
import Locales from './vue-i18n-locales.generated.js';

const store = new Vuex.Store();

Vue.use(VuexI18n.plugin, store);

// add translations directly to the application
Object.keys(Locales).forEach(function (lang) {
  Vue.i18n.add(lang, Locales[lang])
});

// set the start locale to use
Vue.i18n.set('nl');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */
Vue.component('example-component', require('./components/ExampleComponent.vue'));

$(document).ready(function() {
    const app = new Vue({
        store,
        el: '#app'
    });
});
martinlindhe commented 6 years ago

The issue with both vue-i18n and vuex-i18n is they have changed their api several times by now, the README tries to document the various versions. I have no idea why they keep breaking the api:s.

I started looking into setting up example tests covering the various versions in order to have proper examples to point to, but never got around to finish it. Any kind of help would be very welcome!

iben12 commented 6 years ago

I've just installed this on Laravel 5.6 and got: "martinlindhe/laravel-vue-i18n-generator": "^0.1.28" and "vue-i18n": "^7.6.0"

It took me a significant amount of time to figure out the correct way to initialize the translations, so here is what worked for me:

import Vue from 'vue';
import VueInternalization from 'vue-i18n';
import Locale from './vue-i18n-locales.generated';

Vue.use(VueInternalization);

const lang = document.documentElement.lang;

const i18n = new VueInternalization({
    locale: lang.substr(0, 2),
    messages: Locale
});

const app = new Vue({
    el: '#app',
    i18n,
    components: {
       ...
    }
}

Should I make a PR for the README?

martinlindhe commented 6 years ago

@iben12 a PR updating the readme is very welcome!

iben12 commented 6 years ago

Do you think I should add one more init help section or phase out the oldest and push one down?

I guess the one that says 'recent version' is the code for vue-i18n < 6.x.

Since we're at 7.x now, I would suggest the latter.

martinlindhe commented 6 years ago

Yea would be helpful to keep instructions for previous version for now, if you don't mind.

martinlindhe commented 6 years ago

This was resolved in #49