PHPackages                             drewneon/lang4js - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. drewneon/lang4js

ActiveLibrary[Localization &amp; i18n](/categories/localization)

drewneon/lang4js
================

Lang4JS extends the core localization functionalities of Laravel to be appplicable in Javascript.

v1.1.0(9y ago)0281MITPHP ~5.6|~7.0

Since Feb 20Pushed 9y agoCompare

[ Source](https://github.com/DrewNeon/Lang4JS)[ Packagist](https://packagist.org/packages/drewneon/lang4js)[ Docs](https://github.com/DrewNeon/Lang4JS)[ RSS](/packages/drewneon-lang4js/feed)WikiDiscussions 1.1.0 Synced 3w ago

READMEChangelog (2)Dependencies (1)Versions (4)Used By (1)

Lang4JS
=======

[](#lang4js)

[![Latest Version on Packagist](https://camo.githubusercontent.com/cbc71d3aa668c59155b60c0557c121dc8bd203fa3269944b1a339d6ad634c3b8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f647265776e656f6e2f6c616e67346a732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/drewneon/lang4js)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/55d5474806d1e73a6eb2f5e961540c67cdc9e0112c10508058831a3453b25abf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f647265776e656f6e2f6c616e67346a732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/drewneon/lang4js)

Localization, one of the core features of Laravel, separates language strings from views, so as to serve a robust foundation for multilingual projects. However, `trans()`, `trans_choice()` or the newly-introduced `__()`, supported by Blade template engine of different version of Laravel, are not usable in Javascript. This issue is definitely inevitable for developing any serious multilingual projects and just what Lang4JS is addressing. When Lang4JS is installed as a package in your Laravel application, you are able to use `trans()`, `trans_choice()` and `__()` in Javascript with the same syntax just as in Blade templates, the language strings defined in files under `resources/lang` folder are retrieved correspondently and displayed right to the users. Lang4JS is very easy to use and lightweight, merely a 3Kb controller file and a 4Kb minified JS file.

Compatibility
-------------

[](#compatibility)

Lang4JS is compatible with Laravel 4.2+.

Installation
------------

[](#installation)

1. Via Composer

```
$ composer require drewneon/lang4js
```

2. Then add the service provider in `config/app.php`:

```
DrewNeon\Lang4JS\Lang4JSServiceProvider::class,
```

3. And publish the asset file, i.e. `lang4js.min.js`:

```
$ php artisan vendor:publish --tag=public --force
```

4. Finally, add a route in `routes/web.php`:

```
Route::post('/lang4js', '\DrewNeon\Lang4JS\Lang4JSController@lang4js');
```

Usage
-----

[](#usage)

1. Language Strings

Lang4JS retrieves language strings directly from your language files resides in `resources/lang` folder. Please add lines supposed to be used in your Javascript as those for your view blades.

2. Load Javascript Libraries

Lang4JS requires *\[jQuery\] ()* library, you have to load it before the `lang4js.min.js`. So in any view blades you want to apply multilingual strings in Javascript, please insert these two lines:

```

```

**3) Absolute Path**

Both `post('/lang4js',` in route and `src="/vendor/lang4js/lang4js.min.js"` in view uses absolute path for the convenience to include the script from any URL. This means you have to point your domain to the `public` folder in your project, instead of the root folder, otherwise, a 404 error will occur or modify the paths according to your own environment.

**4) CSRF Token**

Lang4JS utilizes Ajax requests to retrieve language strings, and this requires CSRF Token in Laravel. There are two ways to set CSRF Token in your webpages.

1. If the webpage contains a form, you may have probably had the line `{{ csrf_field() }}` in there. If not, add the line somewhere between `...`.
2. If the webpage is not for form submission, please add `` into `...`.

In terms of security of you website, it's a good practice to always have CSRF Token presented in all webpages, which can be easily done by adding the meta tag for CSRF Token in the head sub-view for all views.

5. Use Language Keys in Javascript

Now in your Javascript, feel free to use `trans()`, `trans_choice()` as well as `__()` newly introduced in Laravel 5.4. Please refer to the Localization document according to your Laravel version for more information. For example:

- `trans('messages.welcome')` or `__('messages.welcome')` translates to the string defined in the line `'welcome' => ''` of the file `resources/lang/[locale]/messages.php`;
- `trans_choice('messages.apples', 10)` translates to the plural part of the line `'apples' => '|'` in `resources/lang/[locale]/messages.php`.

**The only difference in syntax**

When using the place-holder feature of `trans()` or `__()`, the syntax `['name' => 'dayle']` does **NOT** work for Javascript, please write `{'name' : 'dayle'}` instead.

**Some Issues**

1. `trans()`, `trans_choice()` or `__()` in your Javascript does **NOT** work before the DOM is ready, and will halt your entire Javascript. Please use them in functions.
2. It's also a fatal error when there's no `trans()` nor `trans_choice()` nor `__()` at all in your Javascript. To fix this, just remove `` from you view.
3. When the locale string requires newlines, please always use `` for HTML and `\n` for Javascript. If you want to show `\n` as is in Javascript, please wrap it in double quotes, i.e. `"\n"`.

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Drew Neon](https://github.com/DrewNeon)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity60

Established project with proven stability

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

4

Last Release

3413d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/25809783?v=4)[DrewNeon](/maintainers/DrewNeon)[@DrewNeon](https://github.com/DrewNeon)

---

Tags

javascriptlaravellocalizationmultilinguallaravellocalizationjavascriptmultilingualLang4JS

### Embed Badge

![Health badge](/badges/drewneon-lang4js/health.svg)

```
[![Health](https://phpackages.com/badges/drewneon-lang4js/health.svg)](https://phpackages.com/packages/drewneon-lang4js)
```

###  Alternatives

[themsaid/laravel-langman

Manage language files with ease.

873308.2k11](/packages/themsaid-laravel-langman)[erag/laravel-lang-sync-inertia

A powerful Laravel package for syncing and managing language translations across backend and Inertia.js (Vue/React) frontends, offering effortless localization, auto-sync features, and smooth multi-language support for modern Laravel applications.

4821.5k](/packages/erag-laravel-lang-sync-inertia)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
