huge Laravel 10 upgrade

This commit is contained in:
HolgerHatGarKeineNode
2023-02-19 20:13:20 +01:00
parent 5c74f77beb
commit 12847f95f6
440 changed files with 46336 additions and 682 deletions

View File

@@ -0,0 +1,40 @@
<?php
namespace JoeDixon\Translation\Console\Commands;
class AddLanguageCommand extends BaseCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'translation:add-language';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Add a new language to the application';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// ask the user for the language they wish to add
$language = $this->ask(__('translation::translation.prompt_language'));
$name = $this->ask(__('translation::translation.prompt_name'));
// attempt to add the key and fail gracefully if exception thrown
try {
$this->translation->addLanguage($language, $name);
$this->info(__('translation::translation.language_added'));
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace JoeDixon\Translation\Console\Commands;
class AddTranslationKeyCommand extends BaseCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'translation:add-translation-key';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Add a new language key for the application';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$language = $this->ask(__('translation::translation.prompt_language_for_key'));
// we know this should be single or group so we can use the `anticipate`
// method to give our users a helping hand
$type = $this->anticipate(__('translation::translation.prompt_type'), ['single', 'group']);
// if the group type is selected, prompt for the group key
if ($type === 'group') {
$file = $this->ask(__('translation::translation.prompt_group'));
}
$key = $this->ask(__('translation::translation.prompt_key'));
$value = $this->ask(__('translation::translation.prompt_value'));
// attempt to add the key for single or group and fail gracefully if
// exception is thrown
if ($type === 'single') {
try {
$this->translation->addSingleTranslation($language, 'single', $key, $value);
return $this->info(__('translation::translation.language_key_added'));
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
} elseif ($type === 'group') {
try {
$file = str_replace('.php', '', $file);
$this->translation->addGroupTranslation($language, $file, $key, $value);
return $this->info(__('translation::translation.language_key_added'));
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
} else {
return $this->error(__('translation::translation.type_error'));
}
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace JoeDixon\Translation\Console\Commands;
use Illuminate\Console\Command;
use JoeDixon\Translation\Drivers\Translation;
class BaseCommand extends Command
{
protected $translation;
public function __construct(Translation $translation)
{
parent::__construct();
$this->translation = $translation;
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace JoeDixon\Translation\Console\Commands;
class ListLanguagesCommand extends BaseCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'translation:list-languages';
/**
* The console command description.
*
* @var string
*/
protected $description = 'List all of the available languages in the application';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$headers = [__('translation::translation.language_name'), __('translation::translation.language')];
$languages = $this->translation->allLanguages()->toArray();
$mappedLanguages = [];
foreach ($languages as $language => $name) {
$mappedLanguages[] = [$name, $language];
}
// return a table of results
$this->table($headers, $mappedLanguages);
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace JoeDixon\Translation\Console\Commands;
class ListMissingTranslationKeys extends BaseCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'translation:list-missing-translation-keys';
/**
* The console command description.
*
* @var string
*/
protected $description = 'List all of the translation keys in the app which don\'t have a corresponding translation';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$missingTranslations = [];
$rows = [];
foreach ($this->translation->allLanguages() as $language => $name) {
$missingTranslations[$language] = $this->translation->findMissingTranslations($language);
}
// check whether or not there are any missing translations
$empty = true;
foreach ($missingTranslations as $language => $values) {
if (! empty($values)) {
$empty = false;
}
}
// if no missing translations, inform the user and move on with your day
if ($empty) {
return $this->info(__('translation::translation.no_missing_keys'));
}
// set some headers for the table of results
$headers = [__('translation::translation.language'), __('translation::translation.type'), __('translation::translation.group'), __('translation::translation.key')];
// iterate over each of the missing languages
foreach ($missingTranslations as $language => $types) {
// iterate over each of the file types (json or array)
foreach ($types as $type => $keys) {
// iterate over each of the keys
foreach ($keys as $key => $value) {
// populate the array with the relevant data to fill the table
foreach ($value as $k => $v) {
$rows[] = [$language, $type, $key, $k];
}
}
}
}
// render the table of results
$this->table($headers, $rows);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace JoeDixon\Translation\Console\Commands;
class SynchroniseMissingTranslationKeys extends BaseCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'translation:sync-missing-translation-keys {language?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Add all of the missing translation keys for all languages or a single language';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$language = $this->argument('language') ?: false;
try {
// if we have a language, pass it in, if not the method will
// automagically sync all languages
$this->translation->saveMissingTranslations($language);
return $this->info(__('translation::translation.keys_synced'));
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
}

View File

@@ -0,0 +1,194 @@
<?php
namespace JoeDixon\Translation\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use JoeDixon\Translation\Drivers\Database;
use JoeDixon\Translation\Drivers\File;
use JoeDixon\Translation\Drivers\Translation;
use JoeDixon\Translation\Scanner;
class SynchroniseTranslationsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'translation:sync-translations {from?} {to?} {language?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Synchronise translations between drivers';
/**
* File scanner.
*
* @var Scanner
*/
private $scanner;
/**
* Translation.
*
* @var Translation
*/
private $translation;
/**
* From driver.
*/
private $fromDriver;
/**
* To driver.
*/
private $toDriver;
/**
* Translation drivers.
*
* @var array
*/
private $drivers = ['file', 'database'];
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(Scanner $scanner, Translation $translation)
{
parent::__construct();
$this->scanner = $scanner;
$this->translation = $translation;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$languages = array_keys($this->translation->allLanguages()->toArray());
// If a valid from driver has been specified as an argument.
if ($this->argument('from') && in_array($this->argument('from'), $this->drivers)) {
$this->fromDriver = $this->argument('from');
}
// When the from driver will be entered manually or if the argument is invalid.
else {
$this->fromDriver = $this->anticipate('Which driver would you like to take translations from?', $this->drivers);
if (! in_array($this->fromDriver, $this->drivers)) {
return $this->error('Invalid driver');
}
}
// Create the driver.
$this->fromDriver = $this->createDriver($this->fromDriver);
// When the to driver has been specified.
if ($this->argument('to') && in_array($this->argument('to'), $this->drivers)) {
$this->toDriver = $this->argument('to');
}
// When the to driver will be entered manually.
else {
$this->toDriver = $this->anticipate('Which driver would you like to add the translations to?', $this->drivers);
if (! in_array($this->toDriver, $this->drivers)) {
return $this->error('Invalid driver');
}
}
// Create the driver.
$this->toDriver = $this->createDriver($this->toDriver);
// If the language argument is set.
if ($this->argument('language')) {
// If all languages should be synced.
if ($this->argument('language') == 'all') {
$language = false;
}
// When a specific language is set and is valid.
elseif (in_array($this->argument('language'), $languages)) {
$language = $this->argument('language');
} else {
return $this->error('Invalid language');
}
} // When the language will be entered manually or if the argument is invalid.
else {
$language = $this->anticipate('Which language? (leave blank for all)', $languages);
if ($language && ! in_array($language, $languages)) {
return $this->error('Invalid language');
}
}
$this->line('Syncing translations');
// If a specific language is set.
if ($language) {
$this->mergeTranslations($this->toDriver, $language, $this->fromDriver->allTranslationsFor($language));
} // Else process all languages.
else {
$translations = $this->mergeLanguages($this->toDriver, $this->fromDriver->allTranslations());
}
$this->info('Translations have been synced');
}
private function createDriver($driver)
{
if ($driver === 'file') {
return new File(new Filesystem, app('path.lang'), config('app.locale'), $this->scanner);
}
return new Database(config('app.locale'), $this->scanner);
}
private function mergeLanguages($driver, $languages)
{
foreach ($languages as $language => $translations) {
$this->mergeTranslations($driver, $language, $translations);
}
}
private function mergeTranslations($driver, $language, $translations)
{
$this->mergeGroupTranslations($driver, $language, $translations['group']);
$this->mergeSingleTranslations($driver, $language, $translations['single']);
}
private function mergeGroupTranslations($driver, $language, $groups)
{
foreach ($groups as $group => $translations) {
foreach ($translations as $key => $value) {
if (is_array($value)) {
continue;
}
$driver->addGroupTranslation($language, $group, $key, $value);
}
}
}
private function mergeSingleTranslations($driver, $language, $vendors)
{
foreach ($vendors as $vendor => $translations) {
foreach ($translations as $key => $value) {
if (is_array($value)) {
continue;
}
$driver->addSingleTranslation($language, $vendor, $key, $value);
}
}
}
}