Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Translatable Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Methods

Preconditions

  • the locale is set to de
  • the fallback_locale is set to en
  • our main model instance is $post = Post::first()
  • translations are available for en, de and fr

Get an instance of the translation model

translate(?string $locale = null, bool $withFallback = false)

Alias 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

translateOrDefault(?string $locale = null)

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

translateOrNew(?string $locale = null)

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

hasTranslation(?string $locale = null)

Check if the post has a translation in default or given locale.

$post->hasTranslation(); // true
$post->hasTranslation('fr'); // true
$post->hasTranslation('it'); // false

translations()

Is the eloquent relation method for the HasMany relation to the translation model.

deleteTranslations(string|array $locales = null)

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

getTranslationsArray()

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'],
];

replicateWithTranslations(array $except = null)

Creates a clone and clones the translations.

$replicate = $post->replicateWithTranslations();

getDefaultLocale()

Returns the current default locale for the current model or null if no default locale is set.

$post->getDefaultLocale(); // null

setDefaultLocale(?string $locale)

Sets the default locale for the current model.

$post->setDefaultLocale('fr');
$post->getDefaultLocale(); // 'fr'

Translation Autoloading

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:

static enableAutoloadTranslations()

forces to load all translations

static disableAutoloadTranslations()

disables autoload and returns parent attributes

static defaultAutoloadTranslations()

does not change the default behavior logic

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope