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

spatie/laravel-translatable

Add multilingual fields to Eloquent models using a simple HasTranslations trait. Store translations as JSON on the model (no extra tables). Set/get translations per locale, switch app locale, fetch all translations, and even translate nested JSON keys via -> notation.

View on GitHub
Deep Wiki
Context7

title: Getting and setting translations weight: 1

First, you must prepare your model as instructed in the installation instructions.

Setting a translation

The easiest way to set a translation for the current locale is to just set the property for a translatable attribute.

Here's an example, given that name is a translatable attribute:

$newsItem->name = 'New translation';

To actually save the translation, don't forget to save your model.

$newsItem->name = 'New translation'
$newsItem->save();

You can immediately set translations when creating a model.

NewsItem::create([
   'name' => [
      'en' => 'Name in English',
      'nl' => 'Naam in het Nederlands'
   ],
]);

To set a translation for a specific locale you can use this method:

public function setTranslation(string $attributeName, string $locale, string $value)

You can set translations for multiple languages with

$translations = ['en' => 'hello', 'es' => 'hola'];
$newsItem->name = $translations;

// alternatively, use the `setTranslations` method

$newsItem->setTranslations('name', $translations);

$newsItem->save();

Getting a translation

The easiest way to get a translation for the current locale is to just get the property for the translated attribute. For example (given that name is a translatable attribute):

$newsItem->name;

You can also use this method:

public function getTranslation(string $attributeName, string $locale, bool $useFallbackLocale = true) : string

This function has an alias named translate.

Getting all translations

You can get all translations by calling getTranslations() without an argument:

$newsItem->getTranslations();

Or you can use the accessor:

$yourModel->translations

The methods above will give you back an array that holds all translations, for example:

$newsItem->getTranslations('name'); 
// returns ['en' => 'Name in English', 'nl' => 'Naam in het Nederlands']

The method above returns, all locales. If you only want specific locales, pass that to the second argument of getTranslations.

public function getTranslations(string $attributeName, array $allowedLocales): array

Here's an example:

$translations = [
    'en' => 'Hello',
    'fr' => 'Bonjour',
    'de' => 'Hallo',
];

$newsItem->setTranslations('hello', $translations);
$newsItem->getTranslations('hello', ['en', 'fr']); // returns ['en' => 'Hello', 'fr' => 'Bonjour']

Get locales that a model has

You can get all locales that a model has by calling locales() without an argument:

   $translations = ['en' => 'hello', 'es' => 'hola'];
   $newsItem->name = $translations;
   $newsItem->save();

   $newsItem->locales(); // returns ['en', 'es']
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport