Giter Club home page Giter Club logo

Comments (18)

michgeek avatar michgeek commented on May 19, 2024 14

My solution to that problem is to use the command without including Lang.js

  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');
// ...

from laravel-js-localization.

audunru avatar audunru commented on May 19, 2024

Having similar problems.

gulpfile essentials:

mix.browserify('app.js');

app.js:

require('./language.js');

Browser:

Uncaught TypeError: Lang is not a constructor

It throws the exception at line 2 here (from language.js):

(function () {
    Lang = new Lang();

Using version 1.3.2 of Laravel-JS-Localization (which includes Lang.js 1.3.3)

from laravel-js-localization.

rmariuzzo avatar rmariuzzo commented on May 19, 2024

Will check today.

On Oct 19, 2016 5:48 AM, "Audun Rundberg" [email protected] wrote:

Having similar problems.

gulpfile essentials:

mix.browserify('app.js');

app.js:

require('./language.js');

Browser:

Uncaught TypeError: Lang is not a constructor

It throws the exception at line 2 here (from language.js):

(function () {
Lang = new Lang();


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#63 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAa2HyYvdpHduDxBW5hO_3uEGaJRpOUyks5q1edOgaJpZM4KZd9w
.

from laravel-js-localization.

audunru avatar audunru commented on May 19, 2024

OK, thanks. Browserify/Webpack is quite new to me so I tried different variations of require, import, etc, but basically I think me and @kaidoj both would like to combine everything into one JS file.

from laravel-js-localization.

rmariuzzo avatar rmariuzzo commented on May 19, 2024

I'm so sorry, I didn't had the time to check this issue. Will check for sure this weekend. I think I have all the info necessary to reproduce a release a fix.

from laravel-js-localization.

arrilot avatar arrilot commented on May 19, 2024

Hi, any updates on this?

from laravel-js-localization.

rmariuzzo avatar rmariuzzo commented on May 19, 2024

I will check on this again this weekend. Today, I'm in the hospital for the
last chemo.

On Oct 28, 2016 10:05 AM, "Nekrasov Ilya" [email protected] wrote:

Any updates on this?


You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
#63 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAa2H_2VEBEjvdZEdet2xFH3yMFNUBu7ks5q4gCUgaJpZM4KZd9w
.

from laravel-js-localization.

rmariuzzo avatar rmariuzzo commented on May 19, 2024

Will address a fix this week.

from laravel-js-localization.

veksen avatar veksen commented on May 19, 2024

Stopped me from using it. Any attempt at using it globally, or requiering through require() or import didn't work.

from laravel-js-localization.

rmariuzzo avatar rmariuzzo commented on May 19, 2024

Hey @veksen! Via browser of node env? I assume it is under node env. I'm following this issue from a while.

from laravel-js-localization.

tobia avatar tobia commented on May 19, 2024

I have the same problem.

This is my gulpfile:

var langFile = 'resources/assets/js/.lang.js';

gulp.task('lang_js', function() {
    gulp.src('').pipe(shell('php artisan lang:js ' + langFile));
});

elixir(function(mix) {
    ...
    mix.task('lang_js', 'resources/lang/**/*.php');
    mix.browserify(...);
    ...
});

It works like this:

  • I tell Elixir to watch the PHP language files;
  • when any of them changes, it runs lang:js into a hidden file in the resources/assets/js directory, so that it has a fixed name but is not tracked by Git;
  • then I run Browserify on my application JS, which includes the generated file into the build and gives it a local name:
var Lang = require('./.lang');

The problem is that the generated file has this shape:

// part A

(function(root, factory) {
        "use strict";
        if (typeof define === "function" && define.amd) {
            define([], factory)
        } else if (typeof exports === "object") {
            module.exports = factory()              // (1)
        } else {
            root.Lang = factory() 
        }
})(this, function() {
    ...
    var Lang = ...
    ...
    // (2)
    return Lang
});

// part B

(function () {
    Lang = new Lang();
    Lang.setMessages(...);
})();

Part A is compatible with all uses, so it works in Browserify as well as a standalone JS, because it correctly adds Lang to the exports (1). But part B does not work, because it cannot find the name Lang, so it cannot replace it with its own instance, let alone add the messages.

I think that the two statements in part B should be moved in the place I marked as (2), so that they can modify the Lang variable before it's returned.

This would make the code compatible with Browserify and hopefully all the other packers.

from laravel-js-localization.

rmariuzzo avatar rmariuzzo commented on May 19, 2024

Hey @tobia! Thanks for details. Can you prepare a PR for this? This week I'm a little bit tight.

from laravel-js-localization.

SwiTool avatar SwiTool commented on May 19, 2024

Is it still under development ? I'm having trouble with require also.. I'm using Webpack.

error: Uncaught ReferenceError: Lang is not defined at Lang = new Lang()

from laravel-js-localization.

rmariuzzo avatar rmariuzzo commented on May 19, 2024

Yes, I started doing some tests last weekend. I will keep you all posted in this thread within the next 24hrs.

from laravel-js-localization.

rmariuzzo avatar rmariuzzo commented on May 19, 2024

@SwiTool I'm checking this right now, will post progress shortly.

from laravel-js-localization.

SwiTool avatar SwiTool commented on May 19, 2024

@rmariuzzo Hey, take your time.
Hope you are okay with those events .. :)

from laravel-js-localization.

rmariuzzo avatar rmariuzzo commented on May 19, 2024

Quick update, this will be implemented on v2 because it will represent a breaking change. I'm thinking that it will more useful if this package doesn't include the JS library. So the latter will fix this issue properly.

from laravel-js-localization.

audunru avatar audunru commented on May 19, 2024

Thanks for the update. My current solution is to stop trying to use import and require (as the whole issue is that doesn't work), but instead generate the language.js and combine it with my browserified app.js using mix.scripts

That solves my original issue, which was that I wanted to have just one app.js file with everything included.

Here's my gulpfile.js, I've included some comments. Perhaps it's helpful for anyone who comes looking for a fix until this is resolved.

const elixir = require('laravel-elixir');

const gulp = require('gulp');
const shell = require('gulp-shell');

elixir((mix) => {
    mix.task('update_js_routes', ['routes/*.php'])
    .copy('node_modules/bootstrap-sass/assets/fonts/bootstrap', 'public/build/fonts/bootstrap')
    .sass('app.scss')
    // Laravel-JS-Localization: Generate language.js
    .task('update_js_localization', ['resources/lang/en/*.php'])
    // Create intermediate app-browserify.js
    .browserify('app.js', 'resources/assets/js/build/app-browserify.js')
    // Combine and minify language.js and app-browserify.js into app.js
    .scripts(['build/language.js', 'build/app-browserify.js'], 'public/js/app.js')
    .scripts('feedleback-v1.js')
    .browserify('feedleback-v2.js')
    .version([
        'css/app.css',
        'js/app.js',
    ])
    // Use with php artisan serve --host 127.0.0.1
    .browserSync({
        proxy: 'localhost:8000',
    });
});
gulp.task('update_js_routes', shell.task([
    'php artisan routes:javascript',
]));
gulp.task('update_js_localization', shell.task([
    'php artisan lang:js resources/assets/js/build/language.js',
]));

from laravel-js-localization.

Related Issues (20)

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.