Download the PHP package eklundkristoffer/js-localization without Composer
On this page you can find all versions of the php package eklundkristoffer/js-localization. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download eklundkristoffer/js-localization
More information about eklundkristoffer/js-localization
Files in eklundkristoffer/js-localization
Package js-localization
Short Description Laravel package to provide localizations to the Javascript code.
License MIT
Informations about the package js-localization
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. 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 |
---|---|
5.x | laravel-5 |
4.2 | laravel-4.2 |
4.1 | laravel-4.1 (near end of life) |
4.0 | laravel-4.0 (end of life) |
Installation
Add the following line to the require
section of your Laravel webapp's composer.json
file:
Run composer update
to install the package.
Finally add the following line to the providers
array of your app/config/app.php
file:
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):
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 <script>
tags to your layout. Here is an example blade view:
Remember it's best to not put the @yield('js-localization.head')
in the <head>
as it contains the <script>
tag
shipping the frontend part of this package. It's best practice to put it at the end of the <body>
, but before
other <script>
tags. The example above simply includes it in the head, since it's the simplest form to use it.
Static generation
For increased performance it is possible to generate static JavaScript files with all of your generated strings. These files can either be served directly as static files, or included as a part of your frontend asset build process.
To specify the output directory for the assets, just set the $storage_path
string in your config/js-localization.php
file accordingly (see Configuration).
The files can then be generated using the artisan command:
php artisan js-localization:export
This will generate two files in your target directory:
messages.js
contains your translation stringsconfig.js
contains your exported config values
If you want to automatically split up the messages.js
file into separate .js files for each locale, you can set the following to true in your config/js-localization.php
config file:
This will in turn also generate the following file(s) in your target directory:
lang-{locale}.js
contains one language's translation strings, if thesplit_export_files
config option is set to true
Remember that the files needs to be regenerated using php artisan js-localization:export
every time any translation strings are edited, added or removed.
Features
You may use Lang.get(), Lang.has(), Lang.choice(), Lang.locale() and trans() (alias for Lang.get()) in your Javascript code. They work just like Laravel's Lang
facade.
Additionally, you are able to pass configuration properties to your Javascript code as well. There is Config.get() in Javascript, too. Configure which config properties to pass to the client using the config
field in config/js-localization.php
. Attention: Do not export any security-critical properties like DB credentials or similar, since they would be visible to anyone using your application!
Variables in messages are supported. For instance: "This is my test string for :name."
.
Pluralization is also supported, but does not care about the locale. It only uses the English pluralization rule ("singular text|plural text"
). More complex pluralization quantifiers are not yet supported.
Service providers
Assume you are developing a laravel package that depends on this javascript localization features and you want to configure which messages of your package have to be visible to the JS code.
Fortunately that's pretty easy. Just listen to the JsLocalization.registerMessages
event and use the JsLocalization\Facades\JsLocalizationHelper::addMessagesToExport()
method. Like so:
License
This software is released under the MIT license. See license.