Skip to content

Commit

Permalink
Pre-release
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvenga committed Jul 6, 2023
1 parent caf48e9 commit 02d2423
Show file tree
Hide file tree
Showing 14 changed files with 514 additions and 55 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"require": {
"php": "^8.0|^8.1|^8.2",
"laravel/framework": "^9.21|^10.0",
"moonshine/moonshine": "^1.60",
"ext-json": "*"
},
"require-dev": {
Expand Down
23 changes: 23 additions & 0 deletions config/moonshine-laravel-translations.php
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',
],
];
5 changes: 0 additions & 5 deletions config/package_name.php

This file was deleted.

2 changes: 0 additions & 2 deletions database/migrations/.gitignore

This file was deleted.

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');
}
};
2 changes: 0 additions & 2 deletions src/Actions/.gitignore

This file was deleted.

81 changes: 81 additions & 0 deletions src/Actions/ExportTranslationsAction.php
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);
}
}
110 changes: 110 additions & 0 deletions src/Actions/ImportTranslationsAction.php
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,
]);
}

}
2 changes: 0 additions & 2 deletions src/Models/.gitignore

This file was deleted.

50 changes: 50 additions & 0 deletions src/Models/MoonshineLaravelTranslation.php
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 src/Providers/MoonShineLaravelTranslationsServiceProvider.php
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([]);
}
}
Loading

0 comments on commit 02d2423

Please sign in to comment.