Use Laravel translations in javascript
3,091
4
2
Package Data
Maintainer Username: MisterPaladin
Maintainer Contact: e.min@milax.com (Eugene Min)
Package Create Date: 2016-06-03
Package Last Update: 2017-06-05
Language: PHP
License: MIT
Last Refreshed: 2024-11-22 03:02:29
Package Statistics
Total Downloads: 3,091
Monthly Downloads: 9
Daily Downloads: 0
Total Stars: 4
Total Watchers: 2
Total Forks: 1
Total Open Issues: 0

JSTrans

Latest Stable Version Total Downloads Latest Unstable Version License

Use Laravel translations in javascript

Other languages:

Installation

Install package:

$ composer require misterpaladin/jstrans

Add JSTransServiceProvider to your config/app.php:

<?php

// ...

'providers' => [
    MisterPaladin\JSTrans\JSTransServiceProvider::class,
],

Publish config:

$ php artisan vendor:publish --tag=jstrans

Usage

Edit config/jstrans.php, add translations you wish to use in javascript:

return [
    'validation',
];

Include this script in your views before your javascript files:

<script src="/vendor/misterpaladin/jstrans/js/jstrans.js" type="text/javascript"></script>

And use it in your javascript like Laravel's trans() function:

jstrans('validation'); // Will return whole 'validation.php' array
jstrans('validation.accepted'); // Will return 'The :attribute must be accepted.'
jstrans('validation.foo.bar'); // Will return 'validation.foo.bar'
jstrans('validation.accepted', {attribute: 'Test'}); // Will return 'The Test must be accepted.'

Custom values

You may specify additional values to overwrite localization files:

@jstrans([
    'validation' => 'new value',
])

@jstrans([
    'foo' => trans('bar'),
    'fooo' => 'baaar',
])
var validation = jstrans('validation'); // Will return 'new value', instead of 'validation.php' array