wedesignit / laravel-translations-import
Import translations from your Laravel translation files to your database.
Installs: 9 363
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 5
Forks: 4
Open Issues: 6
Requires
- php: ^7.4|^8.0
Requires (Dev)
- orchestra/testbench: ^3.8
- phpunit/phpunit: ^7.5
Suggests
- spatie/laravel-translation-loader: Retrieve translations seemless from the database or language file
README
This package provides you with commands to deal with your lang files and your database. Instead of manually adding translations to your database, this commandset gives you the tools needed to import and export translations, find unimported translations in your php/twig files, clean empty translations from your database and to delete all translations in one go!
Usefulness and credit
It's particularly useful in the case you're using a package like
Spatie's Laravel Translation Loader.
In this case the lang files can be used as 'defaults' to import into a project.
When you want to add new defaults, just add them to your lang file(s), rerun the
import command and the newly added translations will be added for usage, without
the existing translations being modified. You can export the files if you want the updated
translations to be used as default.
Credit to Barryvdh's Translation Manager for creating this package which we could build on.
Upgrading
See upgrading for details.
Usage
The import command
php artisan translations:import
This command will import all translation files located in the set lang folder.
The command offers 6 options:
ignore-locales
: Allows you to set locales that should not be imported.
Example:--ignore-locales=fr,de
.ignore-groups
: Allows you to set groups that should not be imported.
Example:--ignore-groups=routes,admin/dashboard,frontend/employer
.only-groups
: Allows you to set groups that should only be imported. No other group will be imported. Works likeignore-groups
.overwrite
: Option to enable overwriting existing translations in the database. Shortcuto
.allow-vendor
: Option to allow vendor translation overrides to be imported (The lang files atlang/vendor/{package}/
). Shortcuta
.allow-json
: Option to allow JSON translations to be imported. Shortcutj
.
Command options should be split by commas.
For groups, a wildcard (*) can be used at the end to allow all subdirectories
to be processed as well (for either ignore
or only
).
Example: --only-groups=admin/*
.
The export command
php artisan translations:export
This command will take all translations in the database, and write them to the lang folder. Existing translations will be overwritten (except for files that don't exist in the database).
The command offers 4 options:
ignore-groups
only-groups
allow-vendor
allow-json
These options work the same as explained with the import command
The find command
php artisan translations:find
This command will find all translations in your php/twig files, and import them if they do not exist in the database.
Keep in mind that this command should be handled with care, imagine we have this in a blade and trans file:
@foreach(__('admin/employers.datatables.filter.statuses') as $value => $translation) <option value="{{$value}}">{{$translation}}</option> @endforeach
// admin/employers.php 'datatables' => [ 'filter' => [ 'statuses' => [ 'active' => 'Actief', 'not-active' => 'Inactief', 'approved' => 'Goedgekeurd', 'declined' => 'Afgekeurd', ], ], ];
The command will find this translation and import it, it will encode the value of the 'statuses'.
The command offers 1 option:
path
: By default, the find command starts in the root directory. Using this option, you can point the command to only search in a subdirectory.
Example:--path=resources/lang
.force-confirm
: Option to enable automatically confirming all found translations. Without this, you have to confirm each import manually. Shortcutc
.
The clean command
php artisan translations:clean
This command will remove all translations which do not have a stored value.
The command has no options.
The delete command
php artisan translations:nuke
This command will remove all translations.
The command has 1 option:
only-groups
This options works the same as explained with the import command
Installation
You can install the package via composer:
composer require wedesignit/laravel-translations-import
Config
By default, the config sets the tables and columns following the language_lines
table from the Spatie's Laravel Translation Loader
migration, with the following structure:
group
: Stores the group (string
).key
: Stores the key (string
).text
: Stores the translation (in JSON format) (text
).
Optionally you could publish the config file to change table and column names.
php artisan vendor:publish --provider="WeDesignIt\LaravelTranslationsImport\TranslationsImportServiceProvider" --tag="config"
This is the content of the published config file:
<?php return [ /** * The table where the translations are stored */ 'table' => 'language_lines', /** * The column where the translations group should be stored */ 'group' => 'group', /** * The column where the translation key should be stored */ 'key' => 'key', /** * The column where the translation text itself should be stored */ 'translations' => 'text', /** * Array of functions which are used to get translations */ 'trans_functions' => [ 'trans', 'trans_choice', 'Lang::get', 'Lang::choice', 'Lang::trans', 'Lang::transChoice', '@lang', '@choice', '__', '$trans.get', ], ];