Download the PHP package scottybo/laravel-translator without Composer
On this page you can find all versions of the php package scottybo/laravel-translator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download scottybo/laravel-translator
More information about scottybo/laravel-translator
Files in scottybo/laravel-translator
Package laravel-translator
Short Description Fork of thiagocordeiro/laravel-translator to search translation keys in PHP and Vue/JS and insert into separate json files to be translated
License MIT
Informations about the package laravel-translator
Laravel-Translator for PHP and Vue
This is a forked version of Laravel-Translator (https://github.com/thiagocordeiro/laravel-translator) which adds the ability to separate generation of Vue translation files and PHP translation files. It's slightly refactored to play nicely with translated strings rather than named keys.
Follow these simple rules when adding translations
- Always use
__()
instead oflang()
,trans()
etc - Any strings that contain a
"
must use"
instead - Always use
""
instead of''
on the string, i.e.{{ __("String that can be translated") }}
- Always use
''
instead of""
on any embedded HTML, i.e.<a href=''></a>
- Always use
{!! !!}
instead of{{ }}
when embedding HTML - Never break translations onto a new line, it will create additional spaces / tabs
- For pluralisation, casing etc refer to: https://laravel.com/docs/5.6/localization
Translations in Laravel Blades
For simple strings:
{{ __("String that can be translated") }}
For strings with variables
{{ __("Hi :name, welcome to :app", ['name' => 'Scott', 'app' => 'Widgets 3000']) }}
Translations in Javascript/Vue
Translations not updating? Clear the cache! $ php artisan cache:clear
Strings used in Vue templates and Javascript code are stored in /resources/lang/_javascript/
and are handled separately from general PHP/system translations. This is to ensure the visitor only downloads translations that are used in JS, keeping file sizes small for faster downloads.
We load <script src="/js/lang-{lang}.js"></script>
on every page of the system. The URL is actually a Laravel route which loads the corresponding language file (e.g. /resources/lang/_javascript/{lang}.json
) and returns a json encoded string of the translations. This string is stored in the variable: window.i18n, which can then be referenced by Javascript.
During the generation of this string will tell Laravel to cache this response forever. Therefore if you ever update the translation file you need to clear the cache via php artisan cache:clear
.
To keep the logic of translations consistent, we have a Vue prototype variable of __
which handles the conversion of the translations (in /resources/assets/js/app.js
):
For simple strings:
{{ __("String that can be translated") }}
For strings with variables:
{{ __("Hi :name, welcome to :app", {'name': 'Scott', 'app', 'Widgets 3000'}) }}
Laravel-translator scans your project resources/view/
resources/js/
and app/
folder to find lang(...)
and __(...)
functions, then it create keys based on first parameter value and insert into json translation files.
Installation
You just have to require the package
This package register the provider automatically, See laravel package discover.
After composer finish it's installation, you'll be able to update your project translation keys running the following command:
if for any reason artisan can't find translator:update
command, you can register the provider manually on your config/app.php
file:
Usage
First you have to create your json translation files:
Keep working as you are used to, when laravel built-in translation funcion can't find given key, it'll return itself, so if you create english keys, you don't need to create an english translation
also you can use params on translation keys
Output
translator:update
command will scan your code to identify new translation keys, then it'll update all json files on app/resources/lang/
folder appending this keys.