Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 12, 2024
1 parent 784e9ed commit 359bcab
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}

Expand Down
51 changes: 22 additions & 29 deletions src/Fields/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand All @@ -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()))]),
];
}
}
23 changes: 22 additions & 1 deletion src/Models/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Translation extends Model implements Contract
* @var array<string>
*/
protected $fillable = [
'language',
'locale',
'values',
];

Expand All @@ -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.
*/
Expand All @@ -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.
*/
Expand Down
11 changes: 6 additions & 5 deletions src/Traits/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
}

0 comments on commit 359bcab

Please sign in to comment.