Adding translations | laravel-tags-go | Spatie       

 SPATIE  

laravel-tags-go
===============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-tags-go](https://spatie.be/docs/laravel-tags-go/v1)  Advanced-usage  Adding translations

 Version   v1      

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

  Adding translations    
- [ Introduction ](https://spatie.be/docs/laravel-tags-go/v1/introduction)
- [ Postcardware ](https://spatie.be/docs/laravel-tags-go/v1/postcardware)
- [ Requirements ](https://spatie.be/docs/laravel-tags-go/v1/requirements)
- [ Installation and setup ](https://spatie.be/docs/laravel-tags-go/v1/installation-and-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-tags-go/v1/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/laravel-tags-go/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-tags-go/v1/about-us)
- [ Testing ](https://spatie.be/docs/laravel-tags-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-tags-go/v1/releasing)
- [ Support us ](https://spatie.be/docs/laravel-tags-go/v1/support-us)

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

- [ Using tags ](https://spatie.be/docs/laravel-tags-go/v1/basic-usage/using-tags)
- [ Retrieving tagged models ](https://spatie.be/docs/laravel-tags-go/v1/basic-usage/retrieving-tagged-models)

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

- [ Adding translations ](https://spatie.be/docs/laravel-tags-go/v1/advanced-usage/adding-translations)
- [ Using types ](https://spatie.be/docs/laravel-tags-go/v1/advanced-usage/using-types)
- [ Sorting tags ](https://spatie.be/docs/laravel-tags-go/v1/advanced-usage/sorting-tags)
- [ Using your own tag model ](https://spatie.be/docs/laravel-tags-go/v1/advanced-usage/using-your-own-tag-model)
- [ Using another default locale ](https://spatie.be/docs/laravel-tags-go/v1/advanced-usage/using-another-default-locale)

 Adding translations
===================

When using the package as described in the basic usage section, all tags are stored in the configured tag locale. If you're creating a multilingual app, you can translate the tags:

```
tag, err := manager.Find(ctx, "my tag", tags.Selection{})
if err != nil {
    return err
}
translations := manager.Translations(&tag)
if err := translations.SetTranslation(ctx, "name", "fr", "mon tag"); err != nil {
    return err
}
if err := translations.SetTranslation(ctx, "name", "nl", "mijn tag"); err != nil {
    return err
}

if err := manager.Save(ctx, &tag); err != nil {
    return err
}
name := tag.Name.V["fr"]
```

The translations are stored in the `name` JSON column. Saving regenerates slugs for every name locale. Existing translations in other locales are preserved. The `slug` column is also a locale-indexed JSON object.

The package uses [Translatable Go](https://github.com/spatie/laravel-translatable-go) v0.1.0. `Tag` implements its model contract while keeping typed `map[string]string` storage. Tag translations must be strings, not nested objects or null locale values. Translation setters and generated slugs dispatch Translatable's Laravel events. Inside a transaction, Laravel defers listeners until commit. A listener failure cannot roll back committed rows.

`manager.Translations(&tag)` returns a Translatable view with the tag locale and `Config.Translations` settings. Use `GetAttribute(ctx, "name")` for current-locale presentation with configured fallback, or `GetTranslation(ctx, "name", "fr", false)` for an exact locale. The view changes only the model in memory, so save it with the manager afterward. A separate Translatable package registration is not required.

Custom tag models can implement Translatable's mutator, accessor and fallback interfaces. The view uses the complete custom model, not just its embedded tag fields. Models that do not implement the translation model contract use the adapter's tag fields instead. Direct edits to `tag.Name.V` are also supported, but bypass translation setter events and mutators.

To find a tag with a specific translation, use Laravel's JSON query support:

```
tag, err := manager.Query(tags.Selection{AnyType: true}).
    WhereEq("name->fr", "mon tag").First(ctx)
```

Or use `manager.Find(ctx, "mon-tag", tags.Selection{Locale: "fr"})` to match either a translated name or slug. Lookup uses exactly that locale, without fallback. Locale keys may contain letters, digits, hyphens and underscores.

Load attached tags with translated display values beside each complete model:

```
translated, err := manager.Translated(ctx, owner, tags.Selection{Locale: "fr", AnyType: true})
```

Each result contains `Tag`, `NameTranslated` and `SlugTranslated`. Missing translations are nil. The stored JSON is not changed.

 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)
