-
Notifications
You must be signed in to change notification settings - Fork 6
Needs rewrite to work with new translation system in Laravel 5.4 #17
Comments
So I've found some time to get it working for my installation. I've attached the diff here, if somebody has the time to clean it up, put it into the general extension and update the tests, then it would truly be a team effort. Be aware, this is a diff for my already extended custom models for this package (see #16) that I've placed in I've decided that there is now one 'source file' in Laravel. This is named after your mapped base locale name (so You'll also need to update the file_format index in My course of action was to remove all translations in OneSky (backup first) and then push the base language strings from the Laravel app. Then I imported my translations into OneSky manually and pulled them into Laravel again.
Good luck!
|
Actually had time to also create the artisan command for finding translation strings! Thanks @barryvdh for the great starting point. app/Commands/OneSky/Find.php<?php
namespace App\Commands\OneSky;
use Symfony\Component\Finder\Finder;
class Find extends \Ageras\LaravelOneSky\Commands\BaseCommand
{
protected $signature = 'onesky:find';
protected $description = 'Find translatable strings in templates';
public function handle()
{
$keys = [];
$functions = ['__'];
$pattern = // See http://regexr.com/392hu
"[^\w|>]". // Must not have an alphanum or _ or > before real method
"(".implode('|', $functions) .")". // Must start with one of the functions
"\(". // Match opening parenthese
"[\'\"]". // Match " or '
"(". // Start a new group to match:
"(.*)". // Anything
//"([.][^\1)]+)+". // Be followed by one or more items/keys, uncommented for 5.4; groups aren't used anymore
")". // Close group
"[\'\"]". // Closing quote
"[\),]"; // Close parentheses or new parameter
// Find all PHP + Twig files in the app folder, except for storage
$finder = new Finder();
$finder->in(base_path())->exclude('storage')->name('*.php')->name('*.twig')->files();
/** @var \Symfony\Component\Finder\SplFileInfo $file */
foreach ($finder as $file) {
// Search the current file for the pattern
if(preg_match_all("/$pattern/siU", $file->getContents(), $matches)) {
// Get all matches
foreach ($matches[2] as $key) {
$this->info('Found <fg=white>' . $key . '</> in <fg=cyan>' . $file->getFilename() . '</>');
$keys[] = $key;
}
}
}
// Remove duplicates
$keys = array_unique($keys);
// Create combined array to easily overwrite with existing translations
$keys = array_combine($keys, $keys);
$baseLocaleJson = $this->translationsPath() . DIRECTORY_SEPARATOR . $this->baseLocale() . '.json';
if (file_exists($baseLocaleJson)) {
$baseLocaleArray = json_decode(file_get_contents($baseLocaleJson), true);
$baseLocaleArray = array_merge($keys, $baseLocaleArray);
} else {
$baseLocaleArray = $keys;
}
if ($file = $this->ask('To which file do you want to write the found strings?', 'resources/lang/' . $this->baseLocale() . '.json')) {
file_put_contents($file, json_encode($baseLocaleArray, JSON_PRETTY_PRINT));
$this->info('Found strings have successfully been written to ' . $file);
}
}
} app/Providers/OneSkyServiceProvider.php<?php
namespace App\Providers;
use App\Commands\OneSky as Commands;
class OneSkyServiceProvider extends \Ageras\LaravelOneSky\ServiceProvider
{
public function registerCommands()
{
$this->app->bindIf('command.onesky', function () {
return new \Ageras\LaravelOneSky\Commands\OneSky();
});
$this->app->bindIf('command.onesky.pull', function () {
return new Commands\Pull();
});
$this->app->bindIf('command.onesky.push', function () {
return new Commands\Push();
});
$this->app->bindIf('command.onesky.find', function () {
return new Commands\Find();
});
$this->commands(
'command.onesky',
'command.onesky.pull',
'command.onesky.push',
'command.onesky.find'
);
}
} Running it will give you;
At which point you'll run |
great job 👍 i'll give it a try asap :) |
https://laravel-news.com/json-based-translations
I'm willing to do it, just need to find some time.
The text was updated successfully, but these errors were encountered: