PHPackages                             sirgrimorum/jslocalization - 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. sirgrimorum/jslocalization

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

sirgrimorum/jslocalization
==========================

Put localization translations in js

1.3.4(2mo ago)0265↓50%MITPHPPHP ^8.2CI failing

Since Feb 17Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/sirgrimorum/jslocalization)[ Packagist](https://packagist.org/packages/sirgrimorum/jslocalization)[ RSS](/packages/sirgrimorum-jslocalization/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (17)Used By (0)

JSLocalization
==============

[](#jslocalization)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f6a1c800509a1a6775e65231a6326e6cadf890c08f348a018b6ee5e6ad41a3e7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7369726772696d6f72756d2f6a736c6f63616c697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/f6a1c800509a1a6775e65231a6326e6cadf890c08f348a018b6ee5e6ad41a3e7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7369726772696d6f72756d2f6a736c6f63616c697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)[![PHP Version](https://camo.githubusercontent.com/40a28a01b6b940eece0d427ff4606f7747f50f79a06154c6e39f588a2f3fe9e9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7369726772696d6f72756d2f6a736c6f63616c697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/40a28a01b6b940eece0d427ff4606f7747f50f79a06154c6e39f588a2f3fe9e9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7369726772696d6f72756d2f6a736c6f63616c697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)[![Total Downloads](https://camo.githubusercontent.com/e713970587a768f2a60b52a54a3412242ebfaa8324cd58726b1e2f07f2ff58a2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7369726772696d6f72756d2f6a736c6f63616c697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/e713970587a768f2a60b52a54a3412242ebfaa8324cd58726b1e2f07f2ff58a2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7369726772696d6f72756d2f6a736c6f63616c697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)[![License](https://camo.githubusercontent.com/6fa88f1ba7385cbe7ac45c8ed0c168b55534816651e93a3ec60caff598f3ca6c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7369726772696d6f72756d2f6a736c6f63616c697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/6fa88f1ba7385cbe7ac45c8ed0c168b55534816651e93a3ec60caff598f3ca6c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7369726772696d6f72756d2f6a736c6f63616c697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)

Expose Laravel translation files and Eloquent models to JavaScript with a single line. Bind PHP i18n arrays to `window` globals so your frontend code shares the same translations and data as your backend — no AJAX calls, no duplication.

Features
--------

[](#features)

- **Translation binding** — publish any Laravel language file to a JavaScript global variable
- **Model/collection binding** — serialize any Eloquent model, collection, or array to a JavaScript variable
- **Nested JS object creation** — nested groups are created safely without clobbering existing keys
- **Static method support** — call `Auth::user()`, method chains, or static properties directly from the Blade directive
- **Zero configuration required** — works out of the box with sensible defaults

Requirements
------------

[](#requirements)

- PHP &gt;= 8.2
- Laravel &gt;= 9.0

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

[](#installation)

```
composer require sirgrimorum/jslocalization
```

### Publish configuration (optional)

[](#publish-configuration-optional)

```
php artisan vendor:publish --provider="Sirgrimorum\JSLocalization\JSLocalizationServiceProvider" --tag=config
```

Publishes `config/sirgrimorum/jslocalization.php`.

Configuration
-------------

[](#configuration)

`config/sirgrimorum/jslocalization.php`

```
return [
    // View that receives the injected  tags (used for auto-injection)
    'bind_trans_vars_to_this_view' => 'layouts.app',

    // Default lang group to expose (the array key inside the language file)
    'trans_group' => 'messages',

    // Global JS variable name that holds all translations
    'default_base_var' => 'translations',

    // Path to language files
    'default_lang_path' => '/resources/lang/',
];
```

Usage
-----

[](#usage)

### Bind a translation file to JavaScript

[](#bind-a-translation-file-to-javascript)

```
{{-- Exposes resources/lang/{locale}/messages.php as window.translations.messages --}}
@jslocalization('messages')

{{-- Expose only a specific group within the file --}}
@jslocalization('messages', 'errors')

{{-- Use a custom JS variable name --}}
@jslocalization('messages', 'errors', 'myApp')
```

The directive outputs a `` tag:

```

window.translations = window.translations || {};
translations.messages = { "welcome": "Welcome!", "errors": { ... } };

```

### Use translations in JavaScript

[](#use-translations-in-javascript)

```
// After @jslocalization('messages')
console.log(translations.messages.welcome);  // "Welcome!"
alert(translations.messages.errors.not_found); // "Not found"
```

### Bind a model to JavaScript

[](#bind-a-model-to-javascript)

```
{{-- Serialize the authenticated user --}}
@jsmodel('Auth::user()')

{{-- Custom variable name --}}
@jsmodel('Auth::user()', 'currentUser')

{{-- Method chains --}}
@jsmodel('Auth::user()->profile()', 'userProfile')

{{-- Static property --}}
@jsmodel('App\Models\Setting::$defaults', 'appDefaults')
```

Output:

```

currentUser = {"id":1,"name":"Alice","email":"alice@example.com"};

```

### Bind any variable from PHP

[](#bind-any-variable-from-php)

You can also pass variables directly using the facade:

```
// In a controller or view composer
$script = JSLocalization::get('messages', 'errors');    // returns ...
$script = JSLocalization::put($collection, 'myData');   // serialize a collection
echo $script;
```

API Reference
-------------

[](#api-reference)

### `JSLocalization::get()`

[](#jslocalizationget)

```
JSLocalization::get(
    string $langfile,       // Language file name (without .php extension)
    string $group   = '',   // Optional key within the file to expose
    string $basevar = ''    // JS global variable (default: config 'default_base_var')
): string
```

Returns a `` tag that assigns the translation array to `window.{basevar}.{langfile}`.

### `JSLocalization::put()`

[](#jslocalizationput)

```
JSLocalization::put(
    mixed  $model,          // Eloquent model, collection, array, or object
    string $variable = ''   // JS variable name (default: class basename)
): string
```

Returns a `` tag that assigns the JSON-encoded value to a global variable.

### Blade directive — `@jslocalization`

[](#blade-directive--jslocalization)

```
@jslocalization(string $langfile, string $group = '', string $basevar = '')
```

### Blade directive — `@jsmodel`

[](#blade-directive--jsmodel)

```
@jsmodel(string $expression, string $variable = '')
```

`$expression` can be:

- A static method call: `'Auth::user()'`
- A method chain: `'Auth::user()->roles()'`
- A static property: `'App\Models\Config::$defaults'`

License
-------

[](#license)

The MIT License (MIT). See [LICENSE](LICENSE).

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance85

Actively maintained with recent releases

Popularity15

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity84

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~195 days

Recently: every ~2 days

Total

16

Last Release

77d ago

PHP version history (3 changes)1.0.0PHP &gt;=7.0

1.3.0PHP ^8.0

1.3.3PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/38ced8f27be30def379c1900bf2a56c7e682180f6a693f17d8168e4e74b8ca82?d=identicon)[sirgrimorum](/maintainers/sirgrimorum)

---

Top Contributors

[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sirgrimorum-jslocalization/health.svg)

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

###  Alternatives

[mcamara/laravel-localization

Easy localization for Laravel

3.5k9.1M112](/packages/mcamara-laravel-localization)[typicms/base

A modular multilingual CMS built with Laravel, enabling developers to manage structured content like pages, news, events, and more.

1.6k20.3k](/packages/typicms-base)[vemcogroup/laravel-translation

Translation package for Laravel to scan for localisations and up/download to poeditor

135304.0k2](/packages/vemcogroup-laravel-translation)

PHPackages © 2026

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