Translatable slugs | laravel-sluggable-rust | Spatie       

 SPATIE  

  Laravel Sluggable 
====================

spatie.be/open-source

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

 Version   v1      

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

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

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

- [ Generating your first slug ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/getting-started)
- [ Using slug options ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/using-slug-options)
- [ Finding models by slug ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/finding-models-by-slug)
- [ Self-healing URLs ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/self-healing-urls)

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

- [ Combining multiple source columns ](https://spatie.be/docs/laravel-sluggable-rust/v1/advanced-usage/source-fields)
- [ Tuning the uniqueness suffix ](https://spatie.be/docs/laravel-sluggable-rust/v1/advanced-usage/uniqueness)
- [ Overriding the underlying actions ](https://spatie.be/docs/laravel-sluggable-rust/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. [ Finding models by slug ](#content-finding-models-by-slug)
3. [ Self-healing URLs ](#content-self-healing-urls)

Use locale-keyed JSON columns to store one slug per locale. The package works with Laravel's `Json<BTreeMap<String, String>>`; applications can implement `SlugSource` and `SlugValue` for other translation types.

```
use std::collections::BTreeMap;
use laravel::{db::Json, model::model};
use spatie_sluggable::{SlugOptions, slug_hooks};

#[model(hooks)]
pub struct Article {
    pub id: i64,
    pub title: Json<BTreeMap<String, String>>,
    pub slug: Json<BTreeMap<String, String>>,
}

impl Article {
    fn slug_options() -> SlugOptions<Self, Json<BTreeMap<String, String>>> {
        SlugOptions::create(Self::slug, |article, slug| article.slug = slug)
            .generate_slugs_from(Self::title)
            .with_locales(["en", "nl"])
    }
}

slug_hooks!(Article, Article::slug_options());
```

Both columns need a JSON type in the migration. Configure at least one locale. Slugs are generated and checked for uniqueness within each locale. Updating the English title does not make the Dutch slug collide with the model's own row. Unconfigured locales already present in the destination are preserved.

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

Use `generate_slugs_using` for computed sources. Its second argument is the current locale. `context.get(column)` reads the pending JSON source on either creation or update.

Explicitly supplied, nonempty translated slugs are preserved per locale and still checked for uniqueness. Missing or cleared locale values regenerate from their sources.

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

`find_by_slug_query(Article::query(), "my-article", Some("nl"), Some("en"))` searches the Dutch slug and then includes the English fallback in the same grouped condition. Add visibility or tenant constraints to the supplied query as usual.

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

`route_key(&article, Some("nl"))` uses the Dutch slug and the native primary key. `resolve_route_binding(query, value, Some("nl"))` resolves that key and reports stale slugs. Neither method changes the model's active locale or any global state.

See [Self-healing URLs](basic-usage/self-healing-urls) for route registration and redirect customization.

 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)
