Add Import and Export Functionality for Site Translations: Implemented SiteTranslationImporter and SiteTranslationExporter classes to facilitate the import and export of site translations. Enhanced the SiteTranslationsTable with import and export actions, allowing for efficient management of translation data. Updated the migration files to create necessary tables for handling imports and exports, improving the overall localization workflow in the admin panel.
This commit is contained in:
@@ -8,6 +8,14 @@ use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use App\Models\Language;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Schemas\Components\Group;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Actions\ImportAction;
|
||||
use Filament\Actions\ExportAction;
|
||||
use App\Filament\Imports\SiteTranslationImporter;
|
||||
use App\Filament\Exports\SiteTranslationExporter;
|
||||
|
||||
class SiteTranslationsTable
|
||||
{
|
||||
@@ -16,6 +24,12 @@ class SiteTranslationsTable
|
||||
$defaultLocale = app()->getLocale();
|
||||
|
||||
return $table
|
||||
->headerActions([
|
||||
ImportAction::make()
|
||||
->importer(SiteTranslationImporter::class),
|
||||
ExportAction::make()
|
||||
->exporter(SiteTranslationExporter::class)
|
||||
])
|
||||
->columns([
|
||||
TextColumn::make('key')
|
||||
->searchable()
|
||||
@@ -39,7 +53,34 @@ class SiteTranslationsTable
|
||||
//
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
EditAction::make()
|
||||
->modalWidth('xl')
|
||||
->form(function () {
|
||||
$languages = Language::where('is_active', true)->get();
|
||||
|
||||
$translationFields = [];
|
||||
foreach ($languages as $language) {
|
||||
$translationFields[] = Textarea::make($language->code)
|
||||
->label($language->name . " ({$language->code})")
|
||||
->rows(2);
|
||||
}
|
||||
|
||||
return [
|
||||
Section::make()
|
||||
->schema([
|
||||
Textarea::make('key')
|
||||
->label('Key / Original Text')
|
||||
->required()
|
||||
->columnSpanFull()
|
||||
->helperText('The original text to be translated. Use this exact text in t() helper.'),
|
||||
|
||||
Group::make()
|
||||
->schema($translationFields)
|
||||
->statePath('translations')
|
||||
->columnSpanFull(),
|
||||
])
|
||||
];
|
||||
}),
|
||||
DeleteAction::make(),
|
||||
])
|
||||
->toolbarActions([
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Exports;
|
||||
|
||||
use App\Models\SiteTranslation;
|
||||
use App\Models\Language;
|
||||
use Filament\Actions\Exports\ExportColumn;
|
||||
use Filament\Actions\Exports\Exporter;
|
||||
use Filament\Actions\Exports\Models\Export;
|
||||
|
||||
class SiteTranslationExporter extends Exporter
|
||||
{
|
||||
protected static ?string $model = SiteTranslation::class;
|
||||
|
||||
public static function getColumns(): array
|
||||
{
|
||||
$columns = [
|
||||
ExportColumn::make('key')->label('Key'),
|
||||
];
|
||||
|
||||
// Veritabanı bağlantısı yoksa veya tablo yoksa hata vermemesi için try-catch veya kontrol
|
||||
try {
|
||||
$languages = Language::where('is_active', true)->get();
|
||||
foreach ($languages as $language) {
|
||||
$columns[] = ExportColumn::make("translations.{$language->code}")
|
||||
->label($language->name . " ({$language->code})");
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// Migration henüz çalışmadıysa
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
public static function getCompletedNotificationBody(Export $export): string
|
||||
{
|
||||
$body = 'Your site translation export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.';
|
||||
|
||||
if ($failedRowsCount = $export->getFailedRowsCount()) {
|
||||
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.';
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Imports;
|
||||
|
||||
use App\Models\SiteTranslation;
|
||||
use App\Models\Language;
|
||||
use Filament\Actions\Imports\ImportColumn;
|
||||
use Filament\Actions\Imports\Importer;
|
||||
use Filament\Actions\Imports\Models\Import;
|
||||
|
||||
class SiteTranslationImporter extends Importer
|
||||
{
|
||||
protected static ?string $model = SiteTranslation::class;
|
||||
|
||||
public static function getColumns(): array
|
||||
{
|
||||
$columns = [
|
||||
ImportColumn::make('key')
|
||||
->requiredMapping()
|
||||
->rules(['required', 'max:255']),
|
||||
];
|
||||
|
||||
try {
|
||||
$languages = Language::where('is_active', true)->get();
|
||||
foreach ($languages as $language) {
|
||||
$columns[] = ImportColumn::make($language->code)
|
||||
->label($language->name . " ({$language->code})");
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
//
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
public function resolveRecord(): ?SiteTranslation
|
||||
{
|
||||
return SiteTranslation::firstOrNew([
|
||||
'key' => $this->data['key'],
|
||||
]);
|
||||
}
|
||||
|
||||
protected function beforeSave(): void
|
||||
{
|
||||
$translations = $this->record->translations ?? [];
|
||||
|
||||
// CSV'den gelen verileri al (data array içinde mapped column adları ile gelir)
|
||||
// getColumns'da column adlarını dil kodu olarak verdik: $language->code
|
||||
|
||||
$languages = Language::where('is_active', true)->pluck('code')->toArray();
|
||||
|
||||
foreach ($languages as $code) {
|
||||
if (isset($this->data[$code])) {
|
||||
// Boş string gelse bile güncelleyelim mi? Evet, çeviri silinmiş olabilir.
|
||||
// Ancak null gelirse (CSV'de sütun yoksa) dokunmayalım.
|
||||
// ImportColumn boş hücreleri null veya boş string olarak getirebilir.
|
||||
|
||||
$value = $this->data[$code];
|
||||
|
||||
if ($value !== null) {
|
||||
$translations[$code] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->record->translations = $translations;
|
||||
|
||||
if (!$this->record->exists && empty($this->record->hash)) {
|
||||
$this->record->hash = md5($this->record->key);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getCompletedNotificationBody(Import $import): string
|
||||
{
|
||||
$body = 'Your site translation import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.';
|
||||
|
||||
if ($failedRowsCount = $import->getFailedRowsCount()) {
|
||||
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.';
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user