Skip to content

Commit 83f98ab

Browse files
committed
implement per-locale configuration system for localizations
1 parent 9dd8afa commit 83f98ab

File tree

18 files changed

+505
-157
lines changed

18 files changed

+505
-157
lines changed

packages/core/src/Entities/Items/Draft/BaseDraftResource.php

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ public static function getTitleColumn(): TextColumn
189189
$currentLang = $livewire->lang;
190190
$query->whereHas('translations', function ($query) use ($search, $currentLang) {
191191
$query->where('locale', $currentLang)
192-
->where('title', 'like', '%'.$search.'%');
192+
->where('title', 'like', '%' . $search . '%');
193193
});
194194
})
195195
->sortable()
196-
->extraAttributes(fn ($record) => [
196+
->extraAttributes(fn($record) => [
197197
'style' => $record->translations()->where('locale', request()->get('lang', app()->getLocale()))->withTrashed()->whereNotNull('title')->exists()
198198
? ''
199199
: 'color: var(--gray-500);',
@@ -207,10 +207,12 @@ public static function getTitleColumn(): TextColumn
207207
return $translation->title;
208208
}
209209

210-
$fallbackTranslation = $record->translations()->where('locale', app()->getLocale())->first();
210+
$defaultLocalization = \Moox\Localization\Models\Localization::where('is_default', true)->first();
211+
$defaultLang = $defaultLocalization?->locale_variant ?? app()->getLocale();
212+
$fallbackTranslation = $record->translations()->where('locale', $defaultLang)->first();
211213

212214
if ($fallbackTranslation && $fallbackTranslation->title) {
213-
return $fallbackTranslation->title.' ('.app()->getLocale().')';
215+
return $fallbackTranslation->title . ' (' . $defaultLang . ')';
214216
}
215217

216218
return 'No title available';
@@ -222,10 +224,12 @@ public static function getSlugColumn(): TextColumn
222224
return TextColumn::make('slug')
223225
->label('Slug')
224226
->searchable(true, function ($query, $search) {
225-
$currentLang = request()->get('lang', app()->getLocale());
227+
$defaultLocalization = \Moox\Localization\Models\Localization::where('is_default', true)->first();
228+
$defaultLang = $defaultLocalization?->locale_variant ?? app()->getLocale();
229+
$currentLang = request()->get('lang', $defaultLang);
226230
$query->whereHas('translations', function ($query) use ($search, $currentLang) {
227231
$query->where('locale', $currentLang)
228-
->where('slug', 'like', '%'.$search.'%');
232+
->where('slug', 'like', '%' . $search . '%');
229233
});
230234
})
231235
->sortable();
@@ -247,21 +251,21 @@ public static function getTranslationStatusSelect(): Select
247251
public static function getEditableTranslationStatusOptions(): array
248252
{
249253
return collect(TranslationStatus::cases())
250-
->filter(fn ($case) => ! in_array($case, [TranslationStatus::NOT_TRANSLATED, TranslationStatus::DELETED]))
251-
->mapWithKeys(fn ($case) => [$case->value => ucfirst($case->value)])
254+
->filter(fn($case) => !in_array($case, [TranslationStatus::NOT_TRANSLATED, TranslationStatus::DELETED]))
255+
->mapWithKeys(fn($case) => [$case->value => ucfirst($case->value)])
252256
->toArray();
253257
}
254258

255259
protected static function getCurrentTranslationStatus($record): string
256260
{
257-
if (! $record) {
261+
if (!$record) {
258262
return TranslationStatus::DRAFT->value;
259263
}
260264

261265
$currentLang = request()->get('lang', app()->getLocale());
262266
$translation = $record->translations()->where('locale', $currentLang)->first();
263267

264-
if (! $translation) {
268+
if (!$translation) {
265269
return TranslationStatus::NOT_TRANSLATED->value;
266270
}
267271

@@ -283,7 +287,7 @@ protected static function getDefaultStatus(): string
283287
public static function getTranslationStatusOptions(): array
284288
{
285289
return collect(TranslationStatus::cases())
286-
->mapWithKeys(fn ($case) => [$case->value => ucfirst($case->value)])
290+
->mapWithKeys(fn($case) => [$case->value => ucfirst($case->value)])
287291
->toArray();
288292
}
289293

@@ -306,8 +310,8 @@ public static function getPublishDateField(): DateTimePicker
306310
->label(__('core::core.to_publish_at'))
307311
->placeholder(__('core::core.to_publish_at'))
308312
->minDate(now())
309-
->hidden(fn ($get) => $get('translation_status') !== 'scheduled')
310-
->dehydrateStateUsing(fn ($state, $get) => $get('translation_status') === 'scheduled' ? $state : null);
313+
->hidden(fn($get) => $get('translation_status') !== 'scheduled')
314+
->dehydrateStateUsing(fn($state, $get) => $get('translation_status') === 'scheduled' ? $state : null);
311315
}
312316

313317
/**
@@ -319,8 +323,8 @@ public static function getUnpublishDateField(): DateTimePicker
319323
->label(__('core::core.to_unpublish_at'))
320324
->placeholder(__('core::core.to_unpublish_at'))
321325
->minDate(now())
322-
->hidden(fn ($get) => ! in_array($get('translation_status'), ['scheduled', 'published']))
323-
->dehydrateStateUsing(fn ($state, $get) => in_array($get('translation_status'), ['scheduled', 'published']) ? $state : null);
326+
->hidden(fn($get) => !in_array($get('translation_status'), ['scheduled', 'published']))
327+
->dehydrateStateUsing(fn($state, $get) => in_array($get('translation_status'), ['scheduled', 'published']) ? $state : null);
324328
}
325329

326330
/**
@@ -332,19 +336,19 @@ public static function getPublishedAtTextEntry(): TextEntry
332336
->label(__('core::core.published_at'))
333337
->state(function ($record): string {
334338
$translation = $record->translations()->withTrashed()->first();
335-
if (! $translation || ! $translation->published_at) {
339+
if (!$translation || !$translation->published_at) {
336340
return '';
337341
}
338342

339343
$publishedBy = '';
340344
if ($translation->published_by_id && $translation->published_by_type) {
341345
$user = app($translation->published_by_type)->find($translation->published_by_id);
342-
$publishedBy = $user ? ' '.__('core::core.by').' '.$user->name : '';
346+
$publishedBy = $user ? ' ' . __('core::core.by') . ' ' . $user->name : '';
343347
}
344348

345-
return $translation->published_at.' - '.$translation->published_at->diffForHumans().$publishedBy;
349+
return $translation->published_at . ' - ' . $translation->published_at->diffForHumans() . $publishedBy;
346350
})
347-
->hidden(fn ($record) => ! $record->published_at);
351+
->hidden(fn($record) => !$record->published_at);
348352
}
349353

350354
/**
@@ -354,9 +358,9 @@ public static function getToUnpublishAtTextEntry(): TextEntry
354358
{
355359
return TextEntry::make('to_unpublish_at')
356360
->label(__('core::core.to_unpublish_at'))
357-
->state(fn ($record): string => $record->to_unpublish_at ?
358-
$record->to_unpublish_at.' - '.$record->to_unpublish_at->diffForHumans() : '')
359-
->hidden(fn ($record) => ! $record->to_unpublish_at);
361+
->state(fn($record): string => $record->to_unpublish_at ?
362+
$record->to_unpublish_at . ' - ' . $record->to_unpublish_at->diffForHumans() : '')
363+
->hidden(fn($record) => !$record->to_unpublish_at);
360364
}
361365

362366
/**
@@ -381,9 +385,11 @@ public static function getTranslationStatusFilter(): SelectFilter
381385
return $query->when(
382386
$data['value'] ?? null,
383387
function (Builder $query, $value): Builder {
384-
$currentLang = request()->query('lang') ?? request()->get('lang') ?? app()->getLocale();
388+
$defaultLocalization = \Moox\Localization\Models\Localization::where('is_default', true)->first();
389+
$defaultLang = $defaultLocalization?->locale_variant ?? app()->getLocale();
390+
$currentLang = request()->query('lang') ?? request()->get('lang') ?? $defaultLang;
385391

386-
if (! $value) {
392+
if (!$value) {
387393
return $query;
388394
}
389395

@@ -410,6 +416,33 @@ function (Builder $query, $value): Builder {
410416
});
411417
}
412418

419+
public static function getLocaleFilter(): SelectFilter
420+
{
421+
return SelectFilter::make('locale')
422+
->label(__('localization::fields.language'))
423+
->options(function () {
424+
$localizations = \Moox\Localization\Models\Localization::where('is_active_admin', true)
425+
->with('language')
426+
->orderBy('language_id')
427+
->orderBy('locale_variant')
428+
->get();
429+
430+
return $localizations->mapWithKeys(function ($localization) {
431+
return [$localization->locale_variant => $localization->display_name];
432+
})->toArray();
433+
})
434+
->query(function (Builder $query, array $data): Builder {
435+
return $query->when(
436+
$data['value'] ?? null,
437+
function (Builder $query, $value): Builder {
438+
return $query->whereHas('translations', function ($query) use ($value) {
439+
$query->where('locale', $value);
440+
});
441+
}
442+
);
443+
});
444+
}
445+
413446
/**
414447
* Get status badge column for translation status
415448
*/
@@ -433,11 +466,13 @@ public static function getStatusColumn(): TextColumn
433466
return static::getStatusColor(strtolower($value));
434467
})
435468
->getStateUsing(function ($record) {
436-
$currentLang = request()->get('lang', app()->getLocale());
469+
$defaultLocalization = \Moox\Localization\Models\Localization::where('is_default', true)->first();
470+
$defaultLang = $defaultLocalization?->locale_variant ?? app()->getLocale();
471+
$currentLang = request()->get('lang', $defaultLang);
437472

438473
$translation = $record->translations()->withTrashed()->where('locale', $currentLang)->first();
439474

440-
if (! $translation) {
475+
if (!$translation) {
441476
return TranslationStatus::NOT_TRANSLATED;
442477
}
443478

packages/core/src/Entities/Items/Draft/Pages/BaseCreateDraft.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ abstract class BaseCreateDraft extends CreateRecord
2020

2121
public function mount(): void
2222
{
23-
$this->lang = request()->query('lang', app()->getLocale());
23+
$defaultLocalization = \Moox\Localization\Models\Localization::where('is_default', true)->first();
24+
$defaultLang = $defaultLocalization?->locale_variant ?? app()->getLocale();
25+
26+
$this->lang = request()->query('lang', $defaultLang);
2427
parent::mount();
2528
}
2629

packages/core/src/Entities/Items/Draft/Pages/BaseEditDraft.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,24 @@ protected function getFormActions(): array
2727

2828
public function mount($record): void
2929
{
30-
$this->lang = request()->query('lang', app()->getLocale());
30+
$defaultLocalization = \Moox\Localization\Models\Localization::where('is_default', true)->first();
31+
$defaultLang = $defaultLocalization?->locale_variant ?? app()->getLocale();
32+
33+
$this->lang = request()->query('lang', $defaultLang);
3134
parent::mount($record);
3235

3336
if ($this->record && method_exists($this->record, 'translations')) {
3437
$isAdminContext = request()->is('admin/*') || request()->is('filament/*') ||
3538
(isset($this) && method_exists($this, 'getResource'));
3639

3740
if ($isAdminContext) {
38-
$localization = \Moox\Localization\Models\Localization::whereHas('language', function ($q) {
39-
$q->where('alpha2', $this->lang);
40-
})->where('is_active_admin', true)->first();
41+
$localization = \Moox\Localization\Models\Localization::where('locale_variant', $this->lang)
42+
->where('is_active_admin', true)->first();
4143

42-
if (! $localization) {
43-
$defaultLocalization = \Moox\Localization\Models\Localization::where('is_default', true)->first() ?? app()->getLocale();
44+
if (!$localization) {
45+
$defaultLocalization = \Moox\Localization\Models\Localization::where('is_default', true)->first();
4446
if ($defaultLocalization) {
45-
$this->redirect($this->getResource()::getUrl('edit', ['record' => $this->record, 'lang' => $defaultLocalization->language->alpha2]));
47+
$this->redirect($this->getResource()::getUrl('edit', ['record' => $this->record, 'lang' => $defaultLocalization->locale_variant]));
4648
} else {
4749
$this->redirect($this->getResource()::getUrl('index'));
4850
}
@@ -51,10 +53,10 @@ public function mount($record): void
5153

5254
$translation = $this->record->translations()->withTrashed()->where('locale', $this->lang)->first();
5355

54-
if ($this->record->trashed() && ! $translation) {
56+
if ($this->record->trashed() && !$translation) {
5557
$defaultLocalization = \Moox\Localization\Models\Localization::where('is_default', true)->first();
5658
if ($defaultLocalization) {
57-
$this->redirect($this->getResource()::getUrl('edit', ['record' => $this->record, 'lang' => $defaultLocalization->language->alpha2]));
59+
$this->redirect($this->getResource()::getUrl('edit', ['record' => $this->record, 'lang' => $defaultLocalization->locale_variant]));
5860
} else {
5961
$this->redirect($this->getResource()::getUrl('index'));
6062
}
@@ -71,7 +73,7 @@ public function mutateFormDataBeforeFill(array $data): array
7173
$record = $this->getRecord();
7274
$values = $data;
7375

74-
if (! method_exists($record, 'getTranslation') || ! property_exists($record, 'translatedAttributes')) {
76+
if (!method_exists($record, 'getTranslation') || !property_exists($record, 'translatedAttributes')) {
7577
return $values;
7678
}
7779

@@ -90,11 +92,11 @@ public function mutateFormDataBeforeFill(array $data): array
9092
protected function handleRecordUpdate(Model $record, array $data): Model
9193
{
9294
/** @var Model&TranslatableContract $record */
93-
if (! $this->lang) {
95+
if (!$this->lang) {
9496
return parent::handleRecordUpdate($record, $data);
9597
}
9698

97-
if (! property_exists($record, 'translatedAttributes')) {
99+
if (!property_exists($record, 'translatedAttributes')) {
98100
return parent::handleRecordUpdate($record, $data);
99101
}
100102

@@ -104,7 +106,7 @@ protected function handleRecordUpdate(Model $record, array $data): Model
104106

105107
$record->update($data);
106108

107-
if (! empty($translationData)) {
109+
if (!empty($translationData)) {
108110
$relation = $record->translations();
109111
$translationModel = $relation->getRelated();
110112
$foreignKey = $relation->getForeignKeyName();
@@ -113,7 +115,7 @@ protected function handleRecordUpdate(Model $record, array $data): Model
113115
->where('locale', $this->lang)
114116
->first();
115117

116-
if (! $translation) {
118+
if (!$translation) {
117119
$translation = $record->translations()->make([
118120
$relation->getForeignKeyName() => $record->id,
119121
'locale' => $this->lang,
@@ -136,7 +138,7 @@ public function mutateFormDataBeforeSave(array $data): array
136138
/** @var Model&TranslatableContract $model */
137139
$model = $this->getRecord();
138140

139-
if (! property_exists($model, 'translatedAttributes')) {
141+
if (!property_exists($model, 'translatedAttributes')) {
140142
return $data;
141143
}
142144

@@ -190,7 +192,7 @@ public function mutateFormDataBeforeSave(array $data): array
190192
];
191193

192194
foreach ($translatedFields as $field) {
193-
if (! isset($data[$field])) {
195+
if (!isset($data[$field])) {
194196
// Don't set protected fields to null automatically
195197
if (in_array($field, $protectedFields)) {
196198
continue;
@@ -224,10 +226,10 @@ public function getTitle(): string
224226

225227
$translation = $this->record->translations()->where('locale', $this->lang)->first();
226228

227-
if (! $translation) {
228-
return $entity.' - '.__('core::core.create');
229+
if (!$translation) {
230+
return $entity . ' - ' . __('core::core.create');
229231
}
230232

231-
return $entity.' - '.$translation->title;
233+
return $entity . ' - ' . $translation->title;
232234
}
233235
}

packages/core/src/Entities/Items/Draft/Pages/BaseListDrafts.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ abstract class BaseListDrafts extends ListRecords
2323
public function mount(): void
2424
{
2525
parent::mount();
26-
$defaultLang = Localization::where('is_default', true)
27-
->first()?->language?->alpha2 ?? config('app.locale');
26+
$defaultLocalization = Localization::where('is_default', true)->first();
27+
$defaultLang = $defaultLocalization?->locale_variant ?? config('app.locale');
2828

2929
$this->lang = request()->get('lang', $defaultLang);
3030
}
3131

3232
public function getTitle(): string
3333
{
3434
if ($this->activeTab === 'deleted') {
35-
return parent::getTitle().' - '.__('core::core.trash');
35+
return parent::getTitle() . ' - ' . __('core::core.trash');
3636
}
3737

3838
return parent::getTitle();
@@ -45,8 +45,8 @@ protected function getHeaderActions(): array
4545
{
4646
return [
4747
CreateAction::make()
48-
->using(fn (array $data, string $model): Model => $model::create($data))
49-
->hidden(fn (): bool => $this->activeTab === 'deleted'),
48+
->using(fn(array $data, string $model): Model => $model::create($data))
49+
->hidden(fn(): bool => $this->activeTab === 'deleted'),
5050
Action::make('emptyTrash')
5151
->label(__('core::core.empty_trash'))
5252
->icon('heroicon-o-trash')
@@ -64,7 +64,7 @@ protected function getHeaderActions(): array
6464
$this->redirect($this->getResource()::getUrl('index', ['lang' => $this->lang, 'tab' => 'all']));
6565
})
6666
->requiresConfirmation()
67-
->visible(fn (): bool => $this->activeTab === 'deleted' && $this->getModel()::onlyTrashed()->exists()),
67+
->visible(fn(): bool => $this->activeTab === 'deleted' && $this->getModel()::onlyTrashed()->exists()),
6868
];
6969
}
7070

0 commit comments

Comments
 (0)