An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

laravel-js-localization
=======================
[![Build Status](https://travis-ci.org/andywer/laravel-js-localization.svg?branch=laravel-5)](https://travis-ci.org/andywer/laravel-js-localization) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/andywer/laravel-js-localization/badges/quality-score.png?b=laravel-5)](https://scrutinizer-ci.com/g/andywer/laravel-js-localization/?branch=laravel-5) [![Code Coverage](https://scrutinizer-ci.com/g/andywer/laravel-js-localization/badges/coverage.png?b=laravel-5)](https://scrutinizer-ci.com/g/andywer/laravel-js-localization/?branch=laravel-5) [![Total Downloads](https://poser.pugx.org/andywer/js-localization/downloads.svg)](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') );