Translatable slugs | laravel-sluggable-go | Spatie       

 SPATIE  

  Laravel Sluggable Go 
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-sluggable-go](https://spatie.be/docs/laravel-sluggable-go/v1)  Translatable slugs

 Version   v1      

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

  Translatable slugs    
- [ Introduction ](https://spatie.be/docs/laravel-sluggable-go/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-sluggable-go/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-sluggable-go/v1/installation-setup)
- [ Translatable slugs ](https://spatie.be/docs/laravel-sluggable-go/v1/translatable-slugs)
- [ Laravel Boost skill ](https://spatie.be/docs/laravel-sluggable-go/v1/laravel-boost-skill)
- [ Changelog ](https://spatie.be/docs/laravel-sluggable-go/v1/changelog)
- [ Questions and issues ](https://spatie.be/docs/laravel-sluggable-go/v1/questions-issues)
- [ Support us ](https://spatie.be/docs/laravel-sluggable-go/v1/support-us)
- [ Upgrading ](https://spatie.be/docs/laravel-sluggable-go/v1/upgrading)
- [ Testing ](https://spatie.be/docs/laravel-sluggable-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-sluggable-go/v1/releasing)

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

- [ Generating your first slug ](https://spatie.be/docs/laravel-sluggable-go/v1/basic-usage/getting-started)
- [ Using the Sluggable attribute ](https://spatie.be/docs/laravel-sluggable-go/v1/basic-usage/using-the-attribute)
- [ Using the HasSlug trait ](https://spatie.be/docs/laravel-sluggable-go/v1/basic-usage/using-the-has-slug-trait)
- [ Finding models by slug ](https://spatie.be/docs/laravel-sluggable-go/v1/basic-usage/finding-models-by-slug)
- [ Self-healing URLs ](https://spatie.be/docs/laravel-sluggable-go/v1/basic-usage/self-healing-urls)

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

- [ Combining multiple source columns ](https://spatie.be/docs/laravel-sluggable-go/v1/advanced-usage/source-fields)
- [ Tuning the uniqueness suffix ](https://spatie.be/docs/laravel-sluggable-go/v1/advanced-usage/uniqueness)
- [ Overriding the underlying actions ](https://spatie.be/docs/laravel-sluggable-go/v1/advanced-usage/overriding-actions)

 Translatable slugs
==================

###  On this page 

1. [ Generating slugs from a callable per locale ](#content-generating-slugs-from-a-callable-per-locale)
2. [ Routing ](#content-routing)
3. [ Finding models by slug ](#content-finding-models-by-slug)
4. [ Self-healing URLs ](#content-self-healing-urls)
5. [ Getting the route key for a specific locale ](#content-getting-the-route-key-for-a-specific-locale)

Store one slug per locale in a JSON column. The same locale-keyed columns work with `spatie/laravel-translatable-go`, but this package can read and write them directly without a mandatory translation-package dependency.

```
type Article struct {
    ID    int64                      `json:"id"`
    Title db.JSON[map[string]string] `json:"title"`
    Slug  db.JSON[map[string]string] `json:"slug"`
}

func (*Article) SlugOptions() sluggable.Options[Article] {
    options := sluggable.DefaultOptions[Article]("title")
    options.Translatable = true
    options.Locale = "en"
    options.FallbackLocale = "nl"
    return options
}

func (a *Article) Creating(ctx context.Context) error {
    return sluggable.Creating(ctx, a)
}

func (a *Article) Updating(ctx context.Context, values db.Values) error {
    return sluggable.Updating(ctx, a, values)
}
```

Import `context`, `go.laravel.com/db` and the package. The title and slug columns must hold JSON objects of strings. The package infers locales from translated source columns, combines source values per locale, and checks uniqueness within each locale. Plain string source columns can be combined with translated columns. `FallbackLocale` supplies a missing source translation.

Update the whole JSON column through `model.Update`, or edit the model and use `model.Save`. Existing manually chosen translated slugs are preserved. `PreventOverwrite` keeps filled locales while allowing empty locales to be generated.

Generating slugs from a callable per locale
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

When using a callback, provide `Locales`. The callback receives the pending model and the current locale.

```
options := sluggable.DefaultOptions[Article]("title")
options.Locales = []string{"en", "nl"}
options.Source = func(article *Article, locale string) (string, error) {
    return fmt.Sprintf("%s %d", locale, article.ID), nil
}
```

Routing
-----------------------------------------------------------------------------

Use `QueryBySlug` or `FindBySlug` in a handler for locale-aware JSON lookup. Go does not override implicit route binding through a PHP trait.

Finding models by slug
--------------------------------------------------------------------------------------------------------------------------

`FindBySlug` searches both the current `Locale` and `FallbackLocale`. Supply a tenant or relationship-constrained builder to retain those filters.

```
prototype := Article{}
slugs, err := sluggable.For(ctx, &prototype)
if err != nil {
    return err
}
article, err := slugs.FindBySlug(ctx, model.Query[Article](), "my-article")
```

Self-healing URLs
-----------------------------------------------------------------------------------------------------------

`SelfHealing` works with translated slugs. The route key uses the configured locale. Stale slugs trigger the same redirect flow described in [Self-healing URLs](basic-usage/self-healing-urls).

Getting the route key for a specific locale
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Use `RouteKey("nl")` to retrieve a locale's key without changing model state:

```
key, err := slugs.RouteKey("nl")
// "nederlandse-titel-5" when self-healing is enabled
```

The locale is passed to the read operation, not stored temporarily on the model. Nothing needs restoring when the operation returns an error.

 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)
