Download the PHP package hadefication/polyglot without Composer
On this page you can find all versions of the php package hadefication/polyglot. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hadefication/polyglot
More information about hadefication/polyglot
Files in hadefication/polyglot
Package polyglot
Short Description Extend Laravel Translation to JavaScript.
License MIT
Informations about the package polyglot
Polyglot
Polyglot creates a blade directive @polyglot
that you can add to your master blade file. This will then export a global variable Polyglot
where all of your app's current locale translation keys are stored. The root object keys are base from the defined translation files located in the Polyglot's configuration file.
Installation
composer require hadefication/polyglot
- Add
Hadefication\Polyglot\PolyglotServiceProvider::class
to yourconfig/app.php
under theproviders
array. This step is not needed for version 5.5 and above where package auto-discovery is introduced. - Include
@polyglot
blade directive to your master blade file on top of your JavaScript files -- probably in the header or before the body tag ends.
Usage
Once installed, this package will then expose a Polyglot
variable where all of your current locale translation keys are stored.
A nifty JavaScript helper function will be exposed too that you can use to translate translation keys like what we're doing in Laravel. Accidentally named it trans
too. See examples below for more details on trans
helper function.
Example
Without param
Translations with params
Config
A configuration file is included too to customize the translation files that will be loaded to Polyglot
. To publish the included config file, run php artisan vendor:publish
.
Artisan Command
An artisan command is also provided where it will dump a JavaScript file that houses all collected translation keys including the importable route method helper. Upon using this approach, including the @polyglot
blade directive won't be necessary.
The command above should dump a JavaScript file named polyglot.js in your /resources/assets/js
directory. You can also supply --path=/path/to/where/the/dump/file/will/be/exported
to dumpt the file in other location. The command should look like php artisan polyglot:dump --path=./resources/assets/js/vendor/polyglot.js
.
The code above should be added to your bootstrap file or to the main JavaScript file if you have a custom entry point.
Laravel Mix
You can also automate the dumping by installing a webpack plugin that runs a simple artisan command on every build so you are sure that you got the latest translation files included in your build. Follow steps below:
- Install the webpack shell plugin:
npm install --save-dev webpack-shell-plugin
oryarn add --dev webpack-shell-plugin
-
Include the plugin to your
webpack.mix.js
file: - Done! This will run
php artisan polyglot:dump
on start of the build so you get the latest translation files.