Parse your files and pull translatable phrases.
- PHP >= 7.1
You can install the package via composer:
composer require inwebcomp/translations-parser
Give access to you ./lang folder
chmod 0777 -R ./resources/lang
Add facade to your config/app.php file
'aliases' => [
// ...
'TranslationsParser' => InWeb\TranslationsParser\TranslationsParserFacade::class
],
Run to publish translations-parser.php configuration file
php artisan vendor:publish --provider="InWeb\TranslationsParser\TranslationsParserServiceProvider"
There you can change locales and folders that are used by artisan command
// Locales to save phrases
'locales' => [
'ru',
'en'
],
// Directories in which phrases are searched
'directories' => [
base_path('app'),
resource_path('views'),
resource_path('js')
],
// Excluded directories or files
'excluded' => [
base_path('app/Nova')
],
// Where is your folder with translations
'lang_files_directory' => resource_path('lang')
Sometimes you receive your locales list during the application run. You can just overwrite this setting in your code:
\Config::set('translations-parser.locales', [
'en',
'fr
]);
Parse translations with artisan command
php artisan translations:parse
Force parse translations. Old translations will be removed
php artisan translations:parse --force
Get phrases from string
TranslationsParser::parse('<div>@lang("Phrase")</div>');
// ['Phrase']
Get phrases from files in directories
TranslationsParser::parse([
resouce_path('views'),
resouce_path('js')
]);
// ['Phrase 1', 'Phrase 2', ...]
Can exclude directories or files from list
TranslationsParser::parse([
resouce_path('views')
], [
resouce_path('views/layouts') // Exclude 'views/layouts' directory
]);
Get phrases in {locale}.json file
TranslationsParser::getParsed('en');
Save phrases to .json file
TranslationsParser::save('en', [
'Phrase'
]);
// Or with values
TranslationsParser::save('en', [
'Phrase' => 'Value'
]);
By default, phrases would not be overwritten It will be overwritten only if there is no phrase in your .json file, or phrase translation is empty
Set last parameter to true to force writing in .json. You will lose old data
TranslationsParser::save('en', [
'Phrase' => 'Value'
], true);
Save one phrase translation
TranslationsParser::translate('en', 'Phrase', 'Translation');
composer test
The MIT License (MIT). Please see License File for more information.