https://github.com/andywer/laravel-js-localization
Simple, ease-to-use and flexible package for the Laravel web framework. Allows you to use localized messages of the Laravel webapp (see `resources/lang` directory) in your Javascript code.
https://github.com/andywer/laravel-js-localization
Last synced: about 1 year ago
JSON representation
Simple, ease-to-use and flexible package for the Laravel web framework. Allows you to use localized messages of the Laravel webapp (see `resources/lang` directory) in your Javascript code.
- Host: GitHub
- URL: https://github.com/andywer/laravel-js-localization
- Owner: andywer
- License: mit
- Created: 2013-10-16T17:46:40.000Z (over 12 years ago)
- Default Branch: laravel-8
- Last Pushed: 2023-06-23T12:59:44.000Z (about 3 years ago)
- Last Synced: 2025-03-12T21:03:47.955Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 280 KB
- Stars: 144
- Watchers: 4
- Forks: 51
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
laravel-js-localization
=======================
[](https://travis-ci.org/andywer/laravel-js-localization) [](https://scrutinizer-ci.com/g/andywer/laravel-js-localization/?branch=laravel-5) [](https://scrutinizer-ci.com/g/andywer/laravel-js-localization/?branch=laravel-5) [](https://packagist.org/packages/andywer/js-localization)
Simple, ease-to-use and flexible package for the [Laravel](http://laravel.com/) web framework. Allows you to use localized messages of the Laravel webapp (see `resources/lang` directory) in your Javascript code. You may easily configure which messages you need to export.
**⚠️ Looking for a new maintainer. Please contact me if you are interested.**
Branches
--------
Laravel | Branch
:----------|:-------
8 | laravel-8
7 | laravel-7
6 | laravel-6
5.8 | laravel-5.8
5.0 - 5.7 | laravel-5 (end of life)
Installation
------------
Add the following line to the `require` section of your Laravel webapp's `composer.json` file:
```javascript
"require": {
"andywer/js-localization": "dev-laravel-6" // see table above
}
```
Run `composer update` to install the package.
Finally add the following line to the `providers` array of your `app/config/app.php` file:
```php
'providers' => [
/* ... */
JsLocalization\JsLocalizationServiceProvider::class
]
```
Configuration
-------------
Run `php artisan vendor:publish` first. This command copies the package's default configuration to `config/js-localization.php`.
You may now edit this file to define the messages you need in your Javascript code. Just edit the `messages` array in the config file.
Example (exports all reminder messages):
```php
['en'],
// Set the keys of the messages you want to use in javascript
'messages' => [
'passwords' => [
'password', 'user', 'token'
]
],
/*
* in short:
* 'messages' => ['passwords']
*
*
* you could also use:
*
* 'messages' => [
* 'passwords.password',
* 'passwords.user',
* 'passwords.token'
* ]
*/
// Set the keys of config properties you want to use in javascript.
// Caution: Do not expose any configuration values that should be kept privately!
'config' => [
'app.debug'
],
// Disables the config cache if set to true, so you don't have to run `php artisan js-localization:refresh`
// each time you change configuration files.
// Attention: Should not be used in production mode due to decreased performance.
'disable_config_cache' => false,
// Split up the exported messages.js file into separate files for each locale.
// This is to ensue faster loading times so one doesn't have to load translations for _all_ languages.
'split_export_files' => true,
];
```
__Important:__
The messages configuration will be cached when the JsLocalizationController is used for the first time. After changing the messages configuration you will need to call __`php artisan js-localization:refresh`__ to refresh that cache. That also affects the config properties you export to javascript, since they are cached, too.
Usage
-----
The translation resources for JavaScript can either be served by your Laravel app at run-time or they can be pre-generated as static JavaScript files, allowing you to serve them straight from your web server or CDN or to be included in your build process.
### Run-time generation
You just need to add the necessary `` tags to your layout. Here is an example blade view:
```html
@include('js-localization::head')
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test view</title>
@yield('js-localization.head')
</head>
<body>
<p>
Here comes a translated message:
<script type="text/javascript">
document.write( Lang.get('reminder.user') );