Usage with factories | laravel-translatable-go | Spatie       

 SPATIE  

laravel-translatable-go
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-translatable-go](https://spatie.be/docs/laravel-translatable-go/v1)  Advanced-usage  Usage with factories

 Version   v1      

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

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

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

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

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

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

 Usage with factories
====================

###  On this page 

1. [ In a factory ](#content-in-a-factory)

A small helper for making translations is available for factories and ordinary application code:

```
english, err := translatable.FactoryTranslations([]string{"en"}, "English")
repeated, err := translatable.FactoryTranslations([]string{"en", "nl"}, "English")
bilingual, err := translatable.FactoryTranslations([]string{"en", "nl"}, []any{"English", "Dutch"})
```

The helper repeats scalar or object values for every locale. A `[]any` supplies one value per locale. Different list lengths return an error. Duplicate locales retain the last supplied value. Repeated objects share their Go map value, so copy before mutating one locale independently.

In a factory
--------------------------------------------------------------------------------------------

Use the helper in a native factory definition or state:

```
var NewsItems = factory.New(func(f *factory.Faker) models.NewsItem {
    names, err := translatable.FactoryTranslations([]string{"en", "nl"}, "News")
    if err != nil {
        panic(err)
    }
    return models.NewsItem{Name: db.NewJSON[any](names)}
})
```

Import `go.laravel.com/factory` and your own models package. The fixed scalar example cannot have a length mismatch. For dynamic lists, validate the returned error before creating a factory state. The helper prepares raw storage and does not dispatch translation-set events or invoke translation mutators. Native model creation hooks still run when you persist the model.

 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)
