generated from moonshine-software/moonshine-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
514 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
return [ | ||
|
||
'ignored' => [ | ||
|
||
//'auth', | ||
//'http-statuses', | ||
//'pagination', | ||
//'passwords', | ||
//'validation', | ||
|
||
//'json', // .json language files | ||
|
||
], | ||
|
||
'main-locale' => config('app.fallback_locale'), | ||
|
||
'locales' => [ | ||
// 'en', | ||
// 'ru', | ||
], | ||
]; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
database/migrations/2023_07_06_140130_create_moonshine_laravel_translations_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('moonshine_laravel_translations', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('group'); | ||
$table->unsignedSmallInteger('list_order')->default(65535); | ||
$table->text('key'); | ||
$table->string('locale'); | ||
$table->text('value')->nullable(); | ||
$table->boolean('is_changed')->default(false)->index(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('moonshine_laravel_translations'); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VI\MoonShineLaravelTranslations\Actions; | ||
|
||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\Storage; | ||
use Illuminate\Support\Str; | ||
use MoonShine\Actions\Action; | ||
use MoonShine\MoonShineUI; | ||
use VI\MoonShineLaravelTranslations\Models\MoonshineLaravelTranslation; | ||
|
||
class ExportTranslationsAction extends Action | ||
{ | ||
|
||
protected ?string $icon = 'heroicons.outline.arrow-up-tray'; | ||
|
||
|
||
public function handle(): RedirectResponse | ||
{ | ||
|
||
$langDisk = Storage::build([ | ||
'driver' => 'local', | ||
'root' => lang_path(), | ||
]); | ||
|
||
$translations = MoonshineLaravelTranslation::toBase()->get(); | ||
|
||
foreach ($translations->groupBy('locale') as $locale => $localeData) { | ||
foreach ($localeData->groupBy('group') as $group => $groupData) { | ||
|
||
$groupData = $groupData->sortBy('list_order')->pluck('value', 'key')->toArray(); | ||
|
||
$groupData = Arr::undot($groupData); | ||
|
||
if ($group == 'json') { | ||
|
||
$langDisk->put($locale.'.json', | ||
json_encode($groupData, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); | ||
|
||
continue; | ||
} | ||
|
||
$langDisk->put($locale.'/'.$group.'.php', "<?php\n\ndeclare(strict_types=1);\n\nreturn ".$this->prettyVarExport($groupData).";\n"); | ||
|
||
} | ||
} | ||
|
||
MoonshineLaravelTranslation::query()->update(['is_changed'=>false]); | ||
|
||
MoonShineUI::toast( | ||
'Экспортировано', | ||
'success' | ||
); | ||
|
||
return back(); | ||
|
||
} | ||
|
||
protected function prettyVarExport($expression): string | ||
{ | ||
$export = var_export($expression, true); | ||
$patterns = [ | ||
"/array \(/" => '[', | ||
"/^([ ]*)\)(,?)$/m" => '$1]$2', | ||
"/([\s]+)\n([\s]+)\[/ui" => ' [', | ||
"/\n([\s ]{8})(['|\]])/ui" => "\n $2", | ||
"/\n([\s ]{6})(['|\]])/ui" => "\n $2", | ||
"/\n([\s ]{4})(['|\]])/ui" => "\n $2", | ||
"/\n([\s ]{2})(['|\]])/ui" => "\n $2", | ||
]; | ||
$output = preg_replace(array_keys($patterns), array_values($patterns), $export); | ||
return trim($output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VI\MoonShineLaravelTranslations\Actions; | ||
|
||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\Storage; | ||
use MoonShine\Actions\Action; | ||
use MoonShine\MoonShineUI; | ||
use MoonShine\Traits\WithConfirmation; | ||
use VI\MoonShineLaravelTranslations\Models\MoonshineLaravelTranslation; | ||
|
||
class ImportTranslationsAction extends Action | ||
{ | ||
|
||
use WithConfirmation; | ||
|
||
protected ?string $icon = 'heroicons.outline.arrow-down-tray'; | ||
|
||
|
||
public function handle(): RedirectResponse | ||
{ | ||
|
||
$langDisk = Storage::build([ | ||
'driver' => 'local', | ||
'root' => lang_path(), | ||
]); | ||
|
||
collect($langDisk->allFiles())->each(function (string $fileName) use ($langDisk) { | ||
|
||
$fileName = str($fileName); | ||
|
||
if ($fileName->startsWith('vendor')) { | ||
return; | ||
} | ||
|
||
if ($fileName->endsWith('.json')) { | ||
$locale = $fileName->replaceLast('.json', '')->toString(); | ||
$groupName = 'json'; | ||
$arrayTranslations = Arr::dot(json_decode($langDisk->get($fileName->toString()), true)); | ||
$i = 0; | ||
foreach ($arrayTranslations as $key => $value) { | ||
$this->updateOrCreateTranslation([ | ||
'group' => $groupName, | ||
'list_order' => $i, | ||
'key' => $key, | ||
'locale' => $locale, | ||
'value' => $value, | ||
]); | ||
$i++; | ||
} | ||
return; | ||
} | ||
|
||
if ($fileName->endsWith('.php')) { | ||
$locale = $fileName->substr(0, 2)->toString(); | ||
$groupName = $fileName->replaceFirst($locale.'/', '')->replaceLast('.php', ''); | ||
$arrayTranslations = include lang_path($fileName->toString()); | ||
$arrayTranslations = Arr::dot($arrayTranslations); | ||
$i = 0; | ||
foreach ($arrayTranslations as $key => $value) { | ||
$this->updateOrCreateTranslation([ | ||
'group' => $groupName, | ||
'list_order' => $i, | ||
'key' => $key, | ||
'locale' => $locale, | ||
'value' => $value, | ||
]); | ||
$i++; | ||
} | ||
return; | ||
} | ||
|
||
//dd($fileName); | ||
}); | ||
|
||
MoonShineUI::toast( | ||
'Импортировано', | ||
'success' | ||
); | ||
|
||
return back(); | ||
|
||
} | ||
|
||
protected function updateOrCreateTranslation(array $data) | ||
{ | ||
|
||
if (!empty(config('moonshine-laravel-translations.locales')) && !in_array($data['locale'],config('moonshine-laravel-translations.locales'))) { | ||
return; | ||
} | ||
|
||
if (!empty(config('moonshine-laravel-translations.ignored')) && in_array($data['group'],config('moonshine-laravel-translations.ignored'))) { | ||
return; | ||
} | ||
|
||
MoonshineLaravelTranslation::updateOrCreate([ | ||
'group' => $data['group'], | ||
'key' => $data['key'], | ||
'locale' => $data['locale'], | ||
], [ | ||
'list_order' => $data['list_order'] ?? 0, | ||
'value' => $data['value'], | ||
'is_changed' => false, | ||
]); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace VI\MoonShineLaravelTranslations\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class MoonshineLaravelTranslation extends Model | ||
{ | ||
|
||
public static ?array $localesList = null; | ||
public static ?array $groupsList = null; | ||
|
||
protected $fillable = [ | ||
'group', | ||
'list_order', | ||
'key', | ||
'locale', | ||
'value', | ||
'is_changed', | ||
]; | ||
|
||
protected $casts = [ | ||
'is_changed' => 'boolean', | ||
]; | ||
|
||
public static function getCountChanged(): int | ||
{ | ||
return static::where('is_changed', true)->count(); | ||
} | ||
|
||
public static function getLocalesList(): array | ||
{ | ||
|
||
if (is_null(static::$localesList)) { | ||
static::$localesList = static::groupBy('locale')->orderBy('locale')->pluck('locale')->toArray(); | ||
} | ||
|
||
return static::$localesList; | ||
} | ||
|
||
public static function getGroupsList(): array | ||
{ | ||
|
||
if (is_null(static::$groupsList)) { | ||
static::$groupsList = static::groupBy('group')->orderBy('group')->pluck('group')->toArray(); | ||
} | ||
|
||
return static::$groupsList; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Providers/MoonShineLaravelTranslationsServiceProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VI\MoonShineLaravelTranslations\Providers; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
final class MoonShineLaravelTranslationsServiceProvider extends ServiceProvider | ||
{ | ||
public function register(): void | ||
{ | ||
// | ||
} | ||
|
||
public function boot(): void | ||
{ | ||
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations'); | ||
$this->loadTranslationsFrom(__DIR__ . '/../../lang', 'moonshine-laravel-translations'); | ||
|
||
$this->publishes([ | ||
__DIR__ . '/../../config/moonshine-laravel-translations.php' => config_path('moonshine-laravel-translations.php'), | ||
]); | ||
|
||
$this->mergeConfigFrom( | ||
__DIR__ . '/../../config/moonshine-laravel-translations.php', | ||
'moonshine-laravel-translations' | ||
); | ||
|
||
$this->publishes([ | ||
__DIR__ . '/../../lang' => $this->app->langPath('vendor/moonshine-laravel-translations'), | ||
]); | ||
|
||
$this->commands([]); | ||
} | ||
} |
Oops, something went wrong.