176
common/Localizations/Commands/ExportTranslations.php
Executable file
176
common/Localizations/Commands/ExportTranslations.php
Executable file
@@ -0,0 +1,176 @@
|
||||
<?php namespace Common\Localizations\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use Common\Auth\Permissions\Permission;
|
||||
use Common\Core\Values\ValueLists;
|
||||
use Error;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
use SplFileInfo;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
class ExportTranslations extends Command
|
||||
{
|
||||
protected $signature = 'translations:export';
|
||||
|
||||
protected $description = 'Export default laravel translations as flattened json file.';
|
||||
|
||||
public function __construct(protected Filesystem $fs)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$messages = array_merge(
|
||||
$this->getLaravelTranslationMessages(),
|
||||
$this->getDefaultMenuLabels(),
|
||||
$this->getPermissionNamesAndDescriptions(),
|
||||
);
|
||||
|
||||
$messages = array_filter(
|
||||
$messages,
|
||||
fn($value, $key) => $value && $key,
|
||||
ARRAY_FILTER_USE_BOTH,
|
||||
);
|
||||
|
||||
$this->fs->put(
|
||||
resource_path('server-translations.json'),
|
||||
json_encode($messages),
|
||||
);
|
||||
|
||||
$this->info('Translation lines exported as json.');
|
||||
}
|
||||
|
||||
protected function getLaravelTranslationMessages()
|
||||
{
|
||||
$files = collect(
|
||||
(new Finder())
|
||||
->in([
|
||||
base_path('app'),
|
||||
base_path('common'),
|
||||
resource_path('views'),
|
||||
resource_path('lang/en'),
|
||||
base_path('vendor/laravel'),
|
||||
])
|
||||
->name(['*.php'])
|
||||
->files(),
|
||||
);
|
||||
|
||||
$lines = $files
|
||||
->map(function (SplFileInfo $file) {
|
||||
$functions = ['__', 'trans', '@lang', 'Lang::get'];
|
||||
$lines = [];
|
||||
$contents = $file->getContents();
|
||||
|
||||
if (Str::contains($contents, 'extends BaseFormRequest')) {
|
||||
$lines = array_merge(
|
||||
$lines,
|
||||
$this->getCustomValidationMessages($file),
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($functions as $function) {
|
||||
if (
|
||||
preg_match_all(
|
||||
'/(' .
|
||||
$function .
|
||||
')\([\r\n\s]{0,}\h*[\'"](.+)[\'"]\h*[\r\n\s]{0,}[),]/U',
|
||||
$file->getContents(),
|
||||
$matches,
|
||||
)
|
||||
) {
|
||||
$lines[] = $matches[2];
|
||||
}
|
||||
}
|
||||
|
||||
return $lines;
|
||||
})
|
||||
->flatten()
|
||||
->map(fn(string $string) => stripslashes($string))
|
||||
->unique()
|
||||
->filter(function (string $string) {
|
||||
// ignore laravel short translation keys
|
||||
// (pagination.next for example) and only use json keys
|
||||
return !preg_match('/^[^.\s]\S*\.\S*[^.\s]$/', $string);
|
||||
});
|
||||
|
||||
return $lines->combine($lines)->toArray();
|
||||
}
|
||||
|
||||
private function getDefaultMenuLabels(): array
|
||||
{
|
||||
$menus = Arr::first(
|
||||
config('common.default-settings'),
|
||||
fn($setting) => $setting['name'] === 'menus',
|
||||
);
|
||||
|
||||
if ($menus) {
|
||||
return collect(json_decode($menus['value'], true))
|
||||
->pluck('items.*.label')
|
||||
->flatten()
|
||||
->mapWithKeys(fn($key) => [$key => $key])
|
||||
->toArray();
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom validation messages from Laravel Request files.
|
||||
*/
|
||||
private function getCustomValidationMessages(SplFileInfo $file): array
|
||||
{
|
||||
//make namespace from file path
|
||||
$namespace = str_replace(
|
||||
[base_path() . DIRECTORY_SEPARATOR, '.php'],
|
||||
'',
|
||||
$file->getPathname(),
|
||||
);
|
||||
$namespace = ucfirst(str_replace('/', '\\', $namespace));
|
||||
|
||||
$messages = [];
|
||||
try {
|
||||
foreach ((new $namespace())->messages() as $message) {
|
||||
$messages[$message] = $message;
|
||||
}
|
||||
} catch (Error $e) {
|
||||
//
|
||||
}
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
private function getPermissionNamesAndDescriptions(): array
|
||||
{
|
||||
Auth::login(User::findAdmin());
|
||||
$lines = app(ValueLists::class)
|
||||
->permissions()
|
||||
->map(function (Permission $permission) {
|
||||
$restrictionLines = $permission->restrictions
|
||||
->map(function ($restriction) {
|
||||
return [
|
||||
ucfirst(str_replace('_', ' ', $restriction['name'])),
|
||||
$restriction['description'],
|
||||
];
|
||||
})
|
||||
->flatten()
|
||||
->toArray();
|
||||
|
||||
return [
|
||||
$permission['display_name'],
|
||||
$permission['group'],
|
||||
$permission['description'],
|
||||
...$restrictionLines,
|
||||
];
|
||||
})
|
||||
->flatten(1)
|
||||
->unique()
|
||||
->toArray();
|
||||
|
||||
return array_combine($lines, $lines);
|
||||
}
|
||||
}
|
||||
65
common/Localizations/Commands/GenerateFooTranslations.php
Executable file
65
common/Localizations/Commands/GenerateFooTranslations.php
Executable file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Localizations\Commands;
|
||||
|
||||
use Common\Localizations\LocalizationsRepository;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class GenerateFooTranslations extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'translations:foo';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Command description';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (
|
||||
$existing = app(LocalizationsRepository::class)->getByNameOrCode(
|
||||
'foo',
|
||||
)
|
||||
) {
|
||||
app(LocalizationsRepository::class)->delete($existing->id);
|
||||
}
|
||||
$localization = app(LocalizationsRepository::class)->create([
|
||||
'name' => 'Foo',
|
||||
'language' => 'foo',
|
||||
]);
|
||||
$localization->loadLines();
|
||||
|
||||
$translatedLines = [];
|
||||
$count = 1;
|
||||
foreach ($localization->lines as $key => $line) {
|
||||
$translatedLines[$key] = "Foo Bar $count";
|
||||
$count++;
|
||||
}
|
||||
|
||||
app(LocalizationsRepository::class)->update($localization['id'], [
|
||||
'lines' => $translatedLines,
|
||||
]);
|
||||
|
||||
$this->info('Localization created');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user