astrotomic/laravel-translatable
Laravel package for translatable Eloquent models. Store model translations in the database and automatically fetch/save multilingual attributes based on locale, reducing boilerplate when working with multi-language content.
locale is set to defallback_locale is set to en$post = Post::first()en, de and frAlias of: getTranslation(?string $locale = null, bool $withFallback = null)
This returns an instance of PostTranslation using the default or given locale. It can also use the configured fallback locale if first locale isn't present.
$post->translate(); // returns the german translation model
$post->translate('fr'); // returns the french translation model
$post->translate('it'); // returns null
$post->translate('it', true); // returns the english translation model
Alias of: getTranslation(?string $locale = null, bool $withFallback = null)
This returns an instance of PostTranslation using the default or given locale and will always use fallback if needed.
$post->translateOrDefault(); // returns the german translation model
$post->translateOrDefault('fr'); // returns the french translation model
$post->translateOrDefault('it'); // returns the english translation model
Alias of: getTranslationOrNew(?string $locale = null)
This returns an instance of PostTranslation using the default or given locale and will create a new instance if needed.
$post->translateOrNew(); // returns the german translation model
$post->translateOrNew('fr'); // returns the french translation model
$post->translateOrNew('it'); // returns the new italian translation model
Check if the post has a translation in default or given locale.
$post->hasTranslation(); // true
$post->hasTranslation('fr'); // true
$post->hasTranslation('it'); // false
Is the eloquent relation method for the HasMany relation to the translation model.
Deletes all translations for the given locale(s).
$post->deleteTranslations(); // delete all translations
$post->deleteTranslations('de'); // delete german translation
$post->deleteTranslations(['de', 'en']); // delete german and english translation
Returns all the translations as array - the structure is the same as it's accepted by the fill(array $data) method.
$post->getTranslationsArray();
// Returns
[
'en' => ['title' => 'My first post'],
'de' => ['title' => 'Mein erster Beitrag'],
'fr' => ['title' => 'Mon premier post'],
];
Creates a clone and clones the translations.
$replicate = $post->replicateWithTranslations();
Returns the current default locale for the current model or null if no default locale is set.
$post->getDefaultLocale(); // null
Sets the default locale for the current model.
$post->setDefaultLocale('fr');
$post->getDefaultLocale(); // 'fr'
If the toArray() method is called it's possible to autoload all translations. To control this feature the package comes with a config value to_array_always_loads_translations and three static methods in the trait:
forces to load all translations
disables autoload and returns parent attributes
does not change the default behavior logic
How can I help you explore Laravel packages today?