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.
1
$post->translate(); // returns the german translation model
2
$post->translate('fr'); // returns the french translation model
3
$post->translate('it'); // returns null
4
$post->translate('it', true); // returns the english translation model
Copied!

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.
1
$post->translateOrDefault(); // returns the german translation model
2
$post->translateOrDefault('fr'); // returns the french translation model
3
$post->translateOrDefault('it'); // returns the english translation model
Copied!

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.
1
$post->translateOrNew(); // returns the german translation model
2
$post->translateOrNew('fr'); // returns the french translation model
3
$post->translateOrNew('it'); // returns the new italian translation model
Copied!

hasTranslation(?string $locale = null)

Check if the post has a translation in default or given locale.
1
$post->hasTranslation(); // true
2
$post->hasTranslation('fr'); // true
3
$post->hasTranslation('it'); // false
Copied!

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).
1
$post->deleteTranslations(); // delete all translations
2
$post->deleteTranslations('de'); // delete german translation
3
$post->deleteTranslations(['de', 'en']); // delete german and english translation
Copied!

getTranslationsArray()

Returns all the translations as array - the structure is the same as it's accepted by the fill(array $data) method.
1
$post->getTranslationsArray();
2
// Returns
3
[
4
'en' => ['title' => 'My first post'],
5
'de' => ['title' => 'Mein erster Beitrag'],
6
'fr' => ['title' => 'Mon premier post'],
7
];
Copied!

replicateWithTranslations(array $except = null)

Creates a clone and clones the translations.
1
$replicate = $post->replicateWithTranslations();
Copied!

getDefaultLocale()

Returns the current default locale for the current model or null if no default locale is set.
1
$post->getDefaultLocale(); // null
Copied!

setDefaultLocale(?string $locale)

Sets the default locale for the current model.
1
$post->setDefaultLocale('fr');
2
$post->getDefaultLocale(); // 'fr'
Copied!

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
Last modified 2yr ago