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

Using 'Lang' class with Vue.js #89

Closed
adl1995 opened this issue Feb 24, 2017 · 12 comments
Closed

Using 'Lang' class with Vue.js #89

adl1995 opened this issue Feb 24, 2017 · 12 comments

Comments

@adl1995
Copy link

adl1995 commented Feb 24, 2017

It so happens that I can't import the 'Lang' class in my Vue component; I receive a ReferenceError. Any help would be appreciated.

@rmariuzzo
Copy link
Owner

This is related to how the template is defined. I think this is related to another existing issue. I will check it later, since I'm in the hospital right now.

@rmariuzzo
Copy link
Owner

This one will be deal with #63

@michgeek
Copy link

michgeek commented Mar 26, 2017

My solution to that problem is to use the command without including Lang.js, imo easier than #63

  1. Assuming my output is : /resources/assets/js/messages.js
// Generate the messages without the lib
php artisan lang:js --no-lib
// Install `Lang.js` as a standalone package
npm install lang.js
// or
yarn add lang.js
  1. Create a file in /resources/assets/js/lang.js
// Inside my /resources/assets/js/lang.js
import lang from 'lang.js';
import messages from 'js/messages';

const Lang = new lang({
    messages
});

export default Lang;
  1. Now you can use Lang wherever you need
import Lang from `js/lang`; // ES6, with webpack configured to resolve `js => /resources/assets/js`
var Lang = require('path/to/your/lang.js'); // Alternatively

Lang.get('my.translation');
// ...

@josiasmontag
Copy link

Even simpler solution:

Add this filter to Vue:

Vue.filter('trans', (...args) => {
    return Lang.get(...args);
});

Then you can do stuff like this:

{{ 'messages.home' | trans }}

or:

{{ 'messages.welcome' | trans({ name: 'Joe' }) }}

@antonkomarev
Copy link
Collaborator

@josiasmontag Awesome! Thanks for the tip!

@sistemaswebbrasil
Copy link

Testing just like michgeek showed I get to catch the message object with console.log (Lang), but if I try to get some text with Lang.get ('messages.hello'), the chrome console returns me: Uncaught TypeError : Lang.get is not a function

@PhPPgAdminBug
Copy link

PhPPgAdminBug commented Oct 10, 2017

`window.Lang= require('lang.js');

alert(window.Lang);`

returns
function(options){ options = options || {}; this.locale = options.locale || inferLocale() || defaults.locale; this.fallback = options.fallback; this.messages = options.messag… }

But functions are not avaiable.
alert(window.Lang.get('my.trans_word'));

Returns
Uncaught TypeError : Lang.get is not a function

same as in comment above. Wasted couple of hours, it seems package does not work with Vue.

@rmariuzzo
Copy link
Owner

Hi @PhPPgAdminBug! Try creating an instance:

var lang = new window.Lang;
alert(lang.get('my.trans_word');

@PhPPgAdminBug
Copy link

PhPPgAdminBug commented Oct 10, 2017

@rmariuzzo Uncaught TypeError: window.Lang is not a constructor

This package, has same issue.
https://github.com/andywer/laravel-js-localization

@PhPPgAdminBug
Copy link

PhPPgAdminBug commented Oct 11, 2017

Figured that
window.Lang = require('lang.js/src/lang'); console.log(window.Lang = new Lang({ messages: document.location.hostname + '/js/messages.js', }));

returns
Lang {locale: "ru", fallback: undefined, messages: "domain.app/js/messages.js"}

But:
window.Lang = require('lang.js/src/lang'); console.log(window.Lang = new Lang({ messages: document.location.hostname + '/js/messages.js', }).has('somte.word'));

is always FALSE, while messages path to file is right, and there are all messages

Also, this one part of code will not work
`import lang from 'lang.js';
import messages from 'js/messages';

const Lang = new lang({
messages
});

export default Lang;`

Because it imports node_modules files. If we put messages files to assets/js, it will throw MIX errors, errors, and errors again.

@rmariuzzo
Copy link
Owner

@PhPPgAdminBug the messages value should not be a path, instead it should the messages.js file contents. Please, double check that.

@rmariuzzo
Copy link
Owner

The original reported issue will be tracked at: #63

Repository owner locked and limited conversation to collaborators Oct 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants