Getting and setting translations | laravel-translatable | Spatie

 SPATIE

  Laravel Translatable
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-translatable](https://spatie.be/docs/laravel-translatable/v6)  Basic-usage  Getting and setting translations

 Version   v6

 Other versions for crawler [v6](https://spatie.be/docs/laravel-translatable/v6)

- [ Introduction ](https://spatie.be/docs/laravel-translatable/v6/introduction)
- [ Support us ](https://spatie.be/docs/laravel-translatable/v6/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-translatable/v6/requirements)
- [ Upgrading ](https://spatie.be/docs/laravel-translatable/v6/upgrading)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-translatable/v6/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-translatable/v6/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-translatable/v6/changelog)
- [ About us ](https://spatie.be/docs/laravel-translatable/v6/about-us)

Basic usage
-----------

- [ Getting and setting translations ](https://spatie.be/docs/laravel-translatable/v6/basic-usage/getting-and-settings-translations)
- [ Removing translations ](https://spatie.be/docs/laravel-translatable/v6/basic-usage/removing-translations)
- [ Replacing translations ](https://spatie.be/docs/laravel-translatable/v6/basic-usage/replacing-translations)
- [ Querying translations ](https://spatie.be/docs/laravel-translatable/v6/basic-usage/querying-translations)
- [ Validation translations ](https://spatie.be/docs/laravel-translatable/v6/basic-usage/validating-translations)
- [ Handling missing translations ](https://spatie.be/docs/laravel-translatable/v6/basic-usage/handling-missing-translations)

Advanced usage
--------------

- [ Available events ](https://spatie.be/docs/laravel-translatable/v6/advanced-usage/available-events)
- [ Customize the toArray method ](https://spatie.be/docs/laravel-translatable/v6/advanced-usage/customize-the-toarray-method)
- [ Usage with factories ](https://spatie.be/docs/laravel-translatable/v6/advanced-usage/usage-with-factories)

 Getting and setting translations
================================

###  On this page

1. [ Setting a translation ](#content-setting-a-translation)
2. [ Getting a translation ](#content-getting-a-translation)

First, you must prepare your model as instructed in [the installation instructions](/docs/laravel-translatable/v6/installation-setup).

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'];
$newItem->name = $translations;

// alternatively, use the `setTranslations` method

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

$newItem->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'];
   $newItem->name = $translations;
   $newItem->save();

   $newItem->locales(); // returns ['en', 'es']
```
