From 359bcabd843e3d95b72e3771bbce1ae6737fb1d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Tue, 12 Nov 2024 17:04:24 +0100 Subject: [PATCH] wip --- ..._195330_create_root_translations_table.php | 4 +- src/Fields/Translations.php | 51 ++++++++----------- src/Models/Translation.php | 23 ++++++++- src/Traits/Translatable.php | 11 ++-- 4 files changed, 52 insertions(+), 37 deletions(-) diff --git a/database/migrations/2024_11_07_195330_create_root_translations_table.php b/database/migrations/2024_11_07_195330_create_root_translations_table.php index 8d097988..1c56a949 100644 --- a/database/migrations/2024_11_07_195330_create_root_translations_table.php +++ b/database/migrations/2024_11_07_195330_create_root_translations_table.php @@ -14,11 +14,11 @@ public function up(): void Schema::create('root_translations', static function (Blueprint $table): void { $table->id(); $table->morphs('translatable'); - $table->string('language', 8); + $table->string('locale', 8); $table->json('values')->nullable(); $table->timestamps(); - $table->unique(['translatable_id', 'translatable_type', 'language'], 'root_translatable_language'); + $table->unique(['translatable_id', 'translatable_type', 'locale'], 'root_translatable_locale'); }); } diff --git a/src/Fields/Translations.php b/src/Fields/Translations.php index f58a41c6..91eb3315 100644 --- a/src/Fields/Translations.php +++ b/src/Fields/Translations.php @@ -10,27 +10,20 @@ class Translations extends MorphMany { /** - * The default languages. + * The default locales. */ - protected static array $defaultLanguages = []; + protected static array $defaultLocales = []; /** - * The field specific languages. + * The field specific locales. */ - protected array $languages = []; + protected array $locales = []; /** * Indicates whether the relation is a sub resource. */ protected bool $asSubResource = true; - /** - * The relations to eager load on every query. - */ - protected array $with = [ - 'values', - ]; - /** * Create a new relation field instance. */ @@ -42,29 +35,29 @@ public function __construct(?string $label = null, Closure|string|null $modelAtt } /** - * Set the default languages. + * Set the default locales. */ - public static function defaultLanguages(array $languages): void + public static function defaultLocales(array $locales): void { - static::$defaultLanguages = $languages; + static::$defaultLocales = $locales; } /** - * Set the field specific languages. + * Set the field specific locales. */ - public function languages(array $languages): static + public function locales(array $locales): static { - $this->languages = $languages; + $this->locales = $locales; return $this; } /** - * Get the available languages. + * Get the available locales. */ - public function getLanguages(): array + public function getLocales(): array { - return $this->languages ?: static::$defaultLanguages; + return $this->locales ?: static::$defaultLocales; } /** @@ -73,7 +66,7 @@ public function getLanguages(): array public function resolveDisplay(Model $related): ?string { if (is_null($this->displayResolver)) { - $this->display('language'); + $this->display('locale'); } return parent::resolveDisplay($related); @@ -85,23 +78,23 @@ public function resolveDisplay(Model $related): ?string public function fields(Request $request): array { return [ - Select::make(__('Language'), 'language') + Select::make(__('Locale'), 'locale') ->options(function (Request $request, Model $model): array { - $languages = $this->getLanguages(); + $locales = $this->getLocales(); $options = array_diff( - $languages, - $model->related->translations->pluck('language')->all() + $locales, + $model->related->translations->pluck('locale')->all() ); - $options = is_null($model->language) + $options = is_null($model->locale) ? $options - : array_unique(array_merge([$model->language], $options)); + : array_unique(array_merge([$model->locale], $options)); - return array_combine($options, $options); + return array_combine($options, array_map('strtoupper', $options)); }) ->required() - ->rules(['required', 'string', Rule::in($this->getLanguages())]), + ->rules(['required', 'string', Rule::in(array_keys($this->getLocales()))]), ]; } } diff --git a/src/Models/Translation.php b/src/Models/Translation.php index 34e4afed..e399ad7d 100644 --- a/src/Models/Translation.php +++ b/src/Models/Translation.php @@ -30,7 +30,7 @@ class Translation extends Model implements Contract * @var array */ protected $fillable = [ - 'language', + 'locale', 'values', ]; @@ -41,6 +41,11 @@ class Translation extends Model implements Contract */ protected $table = 'root_translations'; + /** + * The translatable model's locale. + */ + protected static string $translatableLocale = 'en'; + /** * Get the proxied interface. */ @@ -57,6 +62,22 @@ protected static function newFactory(): TranslationFactory return TranslationFactory::new(); } + /** + * Set the translatable model's locale. + */ + public static function setTranslatableLocale(string $locale): void + { + static::$translatableLocale = $locale; + } + + /** + * Get the translatable model's locale. + */ + public static function getTranslatableLocale(): string + { + return static::$translatableLocale; + } + /** * Get the translatable model for the translation. */ diff --git a/src/Traits/Translatable.php b/src/Traits/Translatable.php index 952b41b1..d0b24e4f 100644 --- a/src/Traits/Translatable.php +++ b/src/Traits/Translatable.php @@ -19,12 +19,13 @@ public function translations(): MorphMany /** * Translate the value of the given key. */ - public function translate(string $key, ?string $language = null): mixed + public function translate(string $key, ?string $locale = null): mixed { - $language ??= App::getLocale(); + $locale ??= App::getLocale(); - $translation = $this->translations->firstWhere('language', $language); - - return $translation?->values[$key] ?? null; + return match ($locale) { + (Translation::proxy())::getTranslatableLocale() => $this->getAttribute($key), + default => $this->translations->firstWhere('locale', $locale)?->values[$key] ?? null, + }; } }