Installation &amp; setup | 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)  Installation &amp; setup

 Version   v1      

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

  Installation &amp; setup    
- [ 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)

 Installation &amp; setup
========================

###  On this page 

1. [ Making a model translatable ](#content-making-a-model-translatable)
2. [ Configuration ](#content-configuration)

You can install the package with Go modules:

```
go get github.com/spatie/laravel-translatable-go@v0.1.0
```

Add the package explicitly in `bootstrap/app.go`:

```
Packages: []laravel.Package{
    translatable.New(func(cfg *translatable.Config) {
        cfg.Locale = "en"
        cfg.FallbackLocale = "en"
    }),
},
```

Import `github.com/spatie/laravel-translatable-go` and alias `go.laravel.com` as `laravel`. Installing the module alone does not activate package registration. The package uses Laravel's native registrar, not a package-tools wrapper.

Making a model translatable
-----------------------------------------------------------------------------------------------------------------------------------------

The required steps to make a model translatable are:

- Implement `translatable.Model` on your application-owned struct.
- Declare the translatable column names or nested paths in `TranslatableAttributes`.
- Store these attributes in JSON columns in your database.

Here's an example of a prepared model:

```
package models

import (
    "fmt"
    "go.laravel.com/db"
)

type NewsItem struct {
    ID   int64        `json:"id"`
    Name db.JSON[any] `json:"name"`
    Meta db.JSON[any] `json:"meta"`
}

func (*NewsItem) TranslatableAttributes() []string {
    return []string{"name", "meta->description"}
}

func (n *NewsItem) TranslationAttribute(column string) (any, error) {
    switch column {
    case "name":
        return n.Name.V, nil
    case "meta":
        return n.Meta.V, nil
    default:
        return nil, fmt.Errorf("unknown JSON column %s", column)
    }
}

func (n *NewsItem) SetTranslationAttribute(column string, value any) error {
    switch column {
    case "name":
        n.Name.V = value
    case "meta":
        n.Meta.V = value
    default:
        return fmt.Errorf("unknown JSON column %s", column)
    }
    return nil
}
```

Create the `news_items` table in your application's migrations with `table.ID()`, `table.JSON("name")` and `table.JSON("meta")`. Use `model.Create(ctx, &newsItem)` for a new model and `model.Save(ctx, &newsItem)` for an existing model. No package-owned model or migration is required.

The adapter receives base column names, not nested paths. Values must be JSON-shaped Go values, with `map[string]any` for objects and `[]any` for lists. `db.JSON[any]` decodes these types directly. Use `*db.JSON[any]` and handle nil in your adapter if you need SQL NULL. JSON null and SQL NULL are different database values.

You can use a different storage type or model without changing the package. Adapt its raw values in these methods. `TranslatableAttributes` can merge declarations from embedded types and remove duplicates automatically through the translation view. There is no PHP attribute or trait emulation.

Configuration
-----------------------------------------------------------------------------------------------

Registration reads `APP_LOCALE` and `APP_FALLBACK_LOCALE`, both defaulting to `en`. `TRANSLATABLE_FALLBACK_LOCALE` overrides the application fallback. The boolean settings `TRANSLATABLE_FALLBACK_ANY`, `TRANSLATABLE_ALLOW_NULL` and `TRANSLATABLE_ALLOW_EMPTY_STRING` default to false. `TRANSLATABLE_LOCALES` is a comma-separated search-locale list.

Callbacks passed to `New` run after environment defaults. Invalid environment booleans prevent startup. `From(ctx)` returns the application-scoped configuration. Treat it as immutable after startup. A view can use its own copied `Config` for request-specific defaults. Reusing a package registration across applications does not share settings.

The framework has no PHP-style runtime locale facade. `UsingLocale("nl")` changes one view, not a global locale. `DefaultConfig()` is also available for explicit configuration outside registration.

 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)
