lukasss93 / laravel-larex
Translate your Laravel application from a single CSV file!
Fund package maintenance!
Lukasss93
Installs: 33 341
Dependents: 1
Suggesters: 0
Security: 0
Stars: 95
Watchers: 5
Forks: 7
Open Issues: 3
Requires
- php: ^8.0
- ext-dom: *
- ext-json: *
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
- loilo/fuse: ^6.4.6
- spatie/laravel-collection-macros: ^7.12
- spatie/simple-excel: ^3.0.1
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- pestphp/pest: ^v1.0|^v2.0
- psalm/plugin-laravel: ^2.0
- vimeo/psalm: ^5.0
README
Laravel Larex
Laravel Larex lets you translate your whole Laravel application with a single CSV file.
You can import translation entries from lang folder into a structured CSV, edit the translations and export them back to Laravel PHP files.
Laravel Larex also supports functionalities to sort entries and find strings that aren't localized yet.
See Plugins section for other features.
🚀 Installation
You can install the package using composer
composer require lukasss93/laravel-larex --dev
Then add the service provider to config/app.php
.
This step can be skipped if package auto-discovery is enabled.
'providers' => [ Lukasss93\Larex\LarexServiceProvider::class ];
⚙ Publishing the config file
Publishing the config file is optional:
php artisan vendor:publish --provider="Lukasss93\Larex\LarexServiceProvider" --tag="larex-config"
👓 Usage
-
First, you must create the initial CSV file with
php artisan larex:init
.
Or you can usephp artisan larex:import
to import entries from lang folder.
The csv file has the following columns:- group (basically the file name)
- key (the array key)
- en (the language code)
- other language codes...
-
Open the project-root/lang/localization.csv file and edit it as you see fit.
-
Finally, you can use
php artisan larex:export
to translate your entries from the csv file to the laravel php files.
✨ Tips
- You can import existing laravel php files with
php artisan larex:import
. - You can use
php artisan larex:init --base
to init the CSV file with default Laravel entries. - The key column inside the CSV file supports the dot notation for nested arrays.
- You can watch your CSV file with
php artisan larex:export --watch
- You can use
php artisan larex:sort
to sort the CSV file by group and key. - Be careful when using the
php artisan larex:export
command! It will overwrite all files named with the group names inside the CSV. - Be careful when using the dot notation! Only the last entry will override the value.
- You can use
php artisan larex:insert
to add new items via CLI too! - You can use
php artisan larex:import --include=en,it
to import only "en" and "it" items. - You can use
php artisan larex:import --exclude=it
to import all items except "it" item. - You can use
php artisan larex:export --include=en,it
to export only "en" and "it" columns. - You can use
php artisan larex:export --exclude=it
to export all columns except "it" column. - You can use
php artisan larex:localize
to find unlocalized strings (use the--import
option to add strings in your CSV). - You can use
php artisan larex:find
to search existing groups or keys in your CSV file. - You can use
php artisan larex:remove
to remove existing strings in your CSV file. - You can use
php artisan larex:lang:add
to add a new language column to your CSV file. - You can use
php artisan larex:lang:remove
to remove a language column from your CSV file. - You can use
php artisan larex:lang:order
to reorder language columns in your CSV file.
📝 Example
-
Run
php artisan larex:init
command -
Edit the project-root/lang/localization.csv file
-
Run
php artisan larex:export
command -
You'll get the following files:
//project-root/lang/en/app.php <?php return [ 'hello' => 'Hello', 'list' => [ 'developers' => 'Developers', 'teachers' => 'Teachers', ] ];
//project-root/lang/it/app.php <?php return [ 'hello' => 'Ciao', 'list' => [ 'developers' => 'Sviluppatori', 'teachers' => 'Insegnanti', ] ];
⏫ Exporters
The larex:export <exporter>
command exports CSV strings to a specific location based on the selected exporter.
How to create an exporter:
- Create a class that implements the
Lukasss93\Larex\Contracts\Exporter
interface - Add your exporter inside the larex config
⏬ Importers
The larex:import <importer>
command imports the strings of the selected importer, into the CSV.
How to create an importer:
- Create a class that implements the
Lukasss93\Larex\Contracts\Importer
interface - Add your importer inside the larex config
🔍 Linters
Larex provides a linting system by using the php artisan larex:lint
command to validate your CSV file.
Available linters:
You can enable/disable any linter you want by comment/uncomment it inside the larex config.
How to create a linter:
- Create a class that implements the
Lukasss93\Larex\Contracts\Linter
interface - Add your linter inside the larex config
🧩 Plugins
- Crowdin Plugin
A Laravel Larex plugin to import/export localization strings from/to Crowdin
⚗️ Testing
composer test
🔰 Version Support
📃 Changelog
Please see the CHANGELOG.md for more information on what has changed recently.
🏅 Credits
📖 License
Please see the LICENSE.md file for more information.