Getting and setting translations | laravel-translatable-rust | Spatie       

 SPATIE  

laravel-translatable-rust
=========================

spatie.be/open-source

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

 Version   v1      

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

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

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

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

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

- [ Available events ](https://spatie.be/docs/laravel-translatable-rust/v1/advanced-usage/available-events)
- [ Serializing translations ](https://spatie.be/docs/laravel-translatable-rust/v1/advanced-usage/customize-the-toarray-method)
- [ Usage with factories ](https://spatie.be/docs/laravel-translatable-rust/v1/advanced-usage/usage-with-factories)

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

Prepare a real model as shown in [installation](../installation-setup). Run mutations inside a request, command, or `Application::scope`. The setter awaits typed translation listeners but does not save the model.

```
use spatie_translatable::{HasTranslations, Translatable, json};
let settings = Translatable { locale: "nl".into(), ..Default::default() };
let mut draft = item.clone();
draft.translations(&settings)
    .set_attribute("name", "Hallo").await?
    .set_translation("name", "en", "Hello").await?;
item.update().name(draft.name).await?;

let t = item.translations(&settings);
assert_eq!(t.get_attribute("name")?, json!("Hallo"));
assert_eq!(t.get_translation("name", "en", false)?, json!("Hello"));
assert_eq!(t.get_translated_locales("name")?, ["nl", "en"]);
assert!(t.has_translation("name", Some("en"))?);
```

Use a draft clone before the typed `update()` builder. The framework does not track assignments made directly to struct fields; setting a field to its already-mutated value produces no change to persist. For new rows, use `NewsItem::create().name(Json(json!({"en":"Hello"}))).meta(Json(json!({}))).await?`.

- `set_attribute` merges objects as locale maps. Scalars and nonempty lists set the current locale. Empty objects and lists clear the field.
- `set_translation` stores any JSON value, including objects and lists, for one locale.
- `get_translations("name", None)` returns a locale map without accessors. Pass `Some(&["en", "nl"])` to filter it.
- `all_translations(None)` returns all declared fields; `locales()` returns their ordered locale union.
- `using_locale("fr")` overrides only that view; an empty override uses `settings.locale`.

Use typed fields directly for ordinary model attributes. The translation view's mutable column references expose only JSON columns selected by your `HasTranslations` implementation.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
