Skip to content

Commit

Permalink
feat(new-structure): Translations to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvenga committed Jul 7, 2023
1 parent f5bfbba commit e8e9e57
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 30 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion config/moonshine-laravel-translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'main-locale' => config('app.fallback_locale'),

'locales' => [
// 'en',
config('app.fallback_locale'),
// 'ru',
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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::dropIfExists('moonshine_laravel_translations');

Schema::create('moonshine_laravel_translations', function (Blueprint $table) {
$table->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');
}
};
24 changes: 17 additions & 7 deletions src/Actions/ExportTranslationsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {

Expand All @@ -43,7 +53,8 @@ public function handle(): RedirectResponse
continue;
}

$langDisk->put($locale.'/'.$group.'.php', "<?php\n\ndeclare(strict_types=1);\n\nreturn ".$this->prettyVarExport($groupData).";\n");
$langDisk->put($locale.'/'.$group.'.php',
"<?php\n\ndeclare(strict_types=1);\n\nreturn ".$this->prettyVarExport($groupData).";\n");

}
}
Expand Down Expand Up @@ -74,7 +85,6 @@ protected function prettyVarExport($expression): string
$output = preg_replace(array_keys($patterns), array_values($patterns), $export);
return trim($output);
}
Expand Down
19 changes: 14 additions & 5 deletions src/Actions/ImportTranslationsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}

}
6 changes: 6 additions & 0 deletions src/Models/MoonshineLaravelTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 5 additions & 5 deletions src/Providers/MoonShineLaravelTranslationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ 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'),
]);
], 'translations');

$this->publishes([
__DIR__ . '/../../config' => $this->app->configPath(),
], 'config');

$this->commands([]);
}
Expand Down
41 changes: 30 additions & 11 deletions src/Resources/MoonShineLaravelTranslationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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([
Expand All @@ -72,7 +74,7 @@ public function fields(): array
NoInput::make(
'Ключ',
'key',
fn (
fn(
MoonshineLaravelTranslation $moonshineLaravelTranslation
) => str($moonshineLaravelTranslation->key)
->replaceMatches(
Expand All @@ -95,7 +97,7 @@ public function fields(): array
NoInput::make(
'Ключ',
'key',
fn (
fn(
MoonshineLaravelTranslation $moonshineLaravelTranslation
) => str($moonshineLaravelTranslation->key)
->replaceMatches(
Expand All @@ -110,14 +112,29 @@ public function fields(): array
NoInput::make(
'Значение',
'value',
fn (MoonshineLaravelTranslation $moonshineLaravelTranslation) => str($moonshineLaravelTranslation->value)
->replaceMatches('/:([a-z\_]+)/ui', '<b style="background-color: rgba(100,255,100, 0.3);">$0</b>')
->toString()
function (MoonshineLaravelTranslation $moonshineLaravelTranslation) {
$html = '<table>';
foreach ($moonshineLaravelTranslation->getTranslations('value') as $code => $value) {
$html .= sprintf(
'<tr><td><b>%s</b></td><td width="100%%">%s</td><tr>',
str($code)->upper(),
str($value)->replaceMatches(
'/:([a-z\_]+)/ui',
'<b style="background-color: rgba(100,255,100, 0.3);">$0</b>'
)
);
}

return $html.'</table>';
}
)
->sortable()
->hideOnForm(),
->sortable(),

TextArea::make('Значение', 'value')

Translatable::make(
'Значение',
'value'
)->languages(config('moonshine-laravel-translations.locales'))
->hideOnIndex()
->hideOnDetail(),
];
Expand All @@ -129,25 +146,27 @@ public function fields(): array
public function rules($item): array
{
return [
'value' => ['nullable', 'string', ''],
//'value' => ['nullable', 'string', ''],
];
}

public function search(): array
{
return ['id', 'group', 'key', 'locale', 'value'];
return ['id', 'group', 'key', 'value'];
}

public function filters(): array
{
return [

/*
SelectFilter::make('Локали', 'locale')
->nullable()
->options(array_combine(
MoonshineLaravelTranslation::getLocalesList(),
MoonshineLaravelTranslation::getLocalesList()
)),
*/

SelectFilter::make('Группы', 'group')
->nullable()
Expand Down

0 comments on commit e8e9e57

Please sign in to comment.