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

Adds HasTranslations to Eloquent models to store translations in JSON columns—no extra tables. Define translatable attributes via PHP 8 attribute or $translatable property, then set/get per-locale values while model accessors return the current app locale.

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.
codraw/entity-migrator
codraw/doctrine-extra
codraw/aws-tool-kit
codraw/validator
codraw/workflow
codraw/open-api
codraw/cron-job
codraw/process
codraw/log
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony