diff --git a/composer.json b/composer.json index df3acb5..8325119 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,9 @@ "php": "^8.0|^8.1|^8.2", "laravel/framework": "^9.21|^10.0", "moonshine/moonshine": "^1.60", - "ext-json": "*" + "ext-json": "*", + "spatie/laravel-translatable": "^6.5", + "visual-ideas/moonshine-spatie-translatable": "^1.1" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.21" diff --git a/config/moonshine-laravel-translations.php b/config/moonshine-laravel-translations.php index b19e805..a662b72 100644 --- a/config/moonshine-laravel-translations.php +++ b/config/moonshine-laravel-translations.php @@ -17,7 +17,7 @@ 'main-locale' => config('app.fallback_locale'), 'locales' => [ - // 'en', + config('app.fallback_locale'), // 'ru', ], ]; diff --git a/database/migrations/2023_07_07_140130_re-create_moonshine_laravel_translations_table.php b/database/migrations/2023_07_07_140130_re-create_moonshine_laravel_translations_table.php new file mode 100644 index 0000000..391c160 --- /dev/null +++ b/database/migrations/2023_07_07_140130_re-create_moonshine_laravel_translations_table.php @@ -0,0 +1,34 @@ +id(); + $table->string('group'); + $table->unsignedSmallInteger('list_order')->default(65535); + $table->text('key'); + $table->json('value')->nullable(); + $table->boolean('is_changed')->default(false)->index(); + $table->boolean('doesnt_have_locales')->default(false)->index(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('moonshine_laravel_translations'); + } +}; diff --git a/src/Actions/ExportTranslationsAction.php b/src/Actions/ExportTranslationsAction.php index 79a6a79..b07f181 100644 --- a/src/Actions/ExportTranslationsAction.php +++ b/src/Actions/ExportTranslationsAction.php @@ -24,14 +24,24 @@ public function handle(): RedirectResponse 'root' => lang_path(), ]); - $translations = MoonshineLaravelTranslation::toBase()->get(); + $translations = MoonshineLaravelTranslation::orderBy('list_order')->get(); - foreach ($translations->groupBy('locale') as $locale => $localeData) { - foreach ($localeData->groupBy('group') as $group => $groupData) { + $translations = $translations->mapWithKeys(function ( + MoonshineLaravelTranslation $moonshineLaravelTranslation, + int $key + ) { - $groupData = $groupData->sortBy('list_order')->pluck('value', 'key')->toArray(); + $tempArray = []; - $groupData = Arr::undot($groupData); + foreach ($moonshineLaravelTranslation->getTranslations('value') as $locale => $translation) { + $tempArray[$locale][$moonshineLaravelTranslation->group][$moonshineLaravelTranslation->key] = $translation; + } + + return $tempArray; + }); + + foreach ($translations as $locale => $localeData) { + foreach ($localeData as $group => $groupData) { if ($group == 'json') { @@ -43,7 +53,8 @@ public function handle(): RedirectResponse continue; } - $langDisk->put($locale.'/'.$group.'.php', "prettyVarExport($groupData).";\n"); + $langDisk->put($locale.'/'.$group.'.php', + "prettyVarExport($groupData).";\n"); } } @@ -74,7 +85,6 @@ protected function prettyVarExport($expression): string $output = preg_replace(array_keys($patterns), array_values($patterns), $export); - return trim($output); } diff --git a/src/Actions/ImportTranslationsAction.php b/src/Actions/ImportTranslationsAction.php index 8bfcefc..9f85a54 100644 --- a/src/Actions/ImportTranslationsAction.php +++ b/src/Actions/ImportTranslationsAction.php @@ -27,6 +27,8 @@ public function handle(): RedirectResponse 'root' => lang_path(), ]); + MoonshineLaravelTranslation::truncate(); + collect($langDisk->allFiles())->each(function (string $fileName) use ($langDisk) { $fileName = str($fileName); @@ -89,23 +91,30 @@ public function handle(): RedirectResponse protected function updateOrCreateTranslation(array $data) { - if (! empty(config('moonshine-laravel-translations.locales')) && ! in_array($data['locale'], config('moonshine-laravel-translations.locales'))) { + 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'))) { + if (! empty(config('moonshine-laravel-translations.ignored')) && in_array( + $data['group'], + config('moonshine-laravel-translations.ignored') + )) { return; } - MoonshineLaravelTranslation::updateOrCreate([ + $moonshineLaravelTranslation = MoonshineLaravelTranslation::updateOrCreate([ 'group' => $data['group'], 'key' => $data['key'], - 'locale' => $data['locale'], ], [ 'list_order' => $data['list_order'] ?? 0, - 'value' => $data['value'], 'is_changed' => false, ]); + + + $moonshineLaravelTranslation->setTranslation('value', $data['locale'], $data['value'])->save(); } } diff --git a/src/Models/MoonshineLaravelTranslation.php b/src/Models/MoonshineLaravelTranslation.php index befbe10..e3a2c1b 100644 --- a/src/Models/MoonshineLaravelTranslation.php +++ b/src/Models/MoonshineLaravelTranslation.php @@ -3,12 +3,18 @@ namespace VI\MoonShineLaravelTranslations\Models; use Illuminate\Database\Eloquent\Model; +use Spatie\Translatable\HasTranslations; class MoonshineLaravelTranslation extends Model { + + use HasTranslations; + public static ?array $localesList = null; public static ?array $groupsList = null; + public $translatable = ['value']; + protected $fillable = [ 'group', 'list_order', diff --git a/src/Providers/MoonShineLaravelTranslationsServiceProvider.php b/src/Providers/MoonShineLaravelTranslationsServiceProvider.php index dd78069..bcbeda7 100644 --- a/src/Providers/MoonShineLaravelTranslationsServiceProvider.php +++ b/src/Providers/MoonShineLaravelTranslationsServiceProvider.php @@ -18,10 +18,6 @@ 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' @@ -29,7 +25,11 @@ public function boot(): void $this->publishes([ __DIR__ . '/../../lang' => $this->app->langPath('vendor/moonshine-laravel-translations'), - ]); + ], 'translations'); + + $this->publishes([ + __DIR__ . '/../../config' => $this->app->configPath(), + ], 'config'); $this->commands([]); } diff --git a/src/Resources/MoonShineLaravelTranslationResource.php b/src/Resources/MoonShineLaravelTranslationResource.php index 7e2ae61..f4d7c7f 100644 --- a/src/Resources/MoonShineLaravelTranslationResource.php +++ b/src/Resources/MoonShineLaravelTranslationResource.php @@ -10,13 +10,13 @@ use MoonShine\Fields\ID; use MoonShine\Fields\NoInput; use MoonShine\Fields\StackFields; -use MoonShine\Fields\Textarea; use MoonShine\Filters\SelectFilter; use MoonShine\QueryTags\QueryTag; use MoonShine\Resources\Resource; use VI\MoonShineLaravelTranslations\Actions\ExportTranslationsAction; use VI\MoonShineLaravelTranslations\Actions\ImportTranslationsAction; use VI\MoonShineLaravelTranslations\Models\MoonshineLaravelTranslation; +use VI\MoonShineSpatieTranslatable\Fields\Translatable; class MoonShineLaravelTranslationResource extends Resource { @@ -57,11 +57,13 @@ public function fields(): array ->sortable() ->hideOnForm(), + /* NoInput::make('Локаль', 'locale', fn ( MoonshineLaravelTranslation $moonshineLaravelTranslation ) => Str::upper($moonshineLaravelTranslation->locale)) ->badge('pink') ->sortable(), + */ StackFields::make('Группа/Ключ', 'group') ->fields([ @@ -72,7 +74,7 @@ public function fields(): array NoInput::make( 'Ключ', 'key', - fn ( + fn( MoonshineLaravelTranslation $moonshineLaravelTranslation ) => str($moonshineLaravelTranslation->key) ->replaceMatches( @@ -95,7 +97,7 @@ public function fields(): array NoInput::make( 'Ключ', 'key', - fn ( + fn( MoonshineLaravelTranslation $moonshineLaravelTranslation ) => str($moonshineLaravelTranslation->key) ->replaceMatches( @@ -110,14 +112,29 @@ public function fields(): array NoInput::make( 'Значение', 'value', - fn (MoonshineLaravelTranslation $moonshineLaravelTranslation) => str($moonshineLaravelTranslation->value) - ->replaceMatches('/:([a-z\_]+)/ui', '$0') - ->toString() + function (MoonshineLaravelTranslation $moonshineLaravelTranslation) { + $html = '
%s | %s |