misterpaladin/jstrans

Use Laravel translations in javascript

1.3.3 2017-06-05 23:53 UTC

This package is auto-updated.

Last update: 2024-11-13 11:31:11 UTC


README

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