Querying translations | 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)  Basic-usage  Querying translations

 Version   v1      

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

  Querying translations    
- [ 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)

 Querying translations
=====================

Store translations in JSON columns so the database can query individual locales. Use the framework's JSON selector syntax directly:

```
items, err := model.Query[models.NewsItem]().
    WhereEq("name->en", "Name in English").
    Get(ctx)
```

If you want records based on locales, use `WhereLocale` and `WhereLocales` as native model scopes:

```
items, err := model.Query[models.NewsItem]().
    Scope(translatable.WhereLocales[models.NewsItem]("name", []string{"en", "nl"})).
    Get(ctx)
```

`WhereLocale[models.NewsItem]("name", "en")` selects a non-null English value. `WhereLocales` selects English or Dutch. Raw empty strings count, independently of read-side filtering.

To query records based on a locale's value, use `WhereJSONContainsLocale` or `WhereJSONContainsLocales`:

```
items, err := model.Query[models.NewsItem]().
    Scope(translatable.WhereJSONContainsLocale[models.NewsItem](
        "name", "en", "Name in%", "like",
    )).
    Get(ctx)
```

The last argument is the comparison operator. Empty means `=`. Values are native Go values bound by the query builder. Numeric comparisons stay numeric.

To search for a partial value in one or more translated columns across locales, use `WhereTranslationLike`:

```
items, err := model.Query[models.NewsItem]().
    WhereEq("published", true).
    Scope(translatable.WhereTranslationLike[models.NewsItem](
        []string{"name", "meta->description"}, "news", []string{"en", "nl"},
        *translatable.From(ctx),
    )).
    OrderBy("id").Limit(20).Get(ctx)
```

The scope matches any requested column and locale, grouped so it can be combined with other constraints. It searches translated values, not locale keys. Filters execute in SQL before pagination.

When locales are nil, search uses `Config.Locales`, falling back to `Config.Locale`, then `en`. Explicit locales override these defaults. Passing an empty columns slice or a non-nil empty locales slice matches no records. Locale-presence and comparison scopes also match no records for an empty locale list.

Search uses Laravel's case-insensitive `WhereLike`. Unicode case conversion depends on the database and collation. SQLite's built-in case folding only converts ASCII characters. Non-ASCII values can still be searched, but their case must match on SQLite.

Search values are bound as parameters and wrapped in `%` wildcards. `%` and `_` in the search retain their SQL LIKE wildcard meaning. An empty search matches non-null translated values. Columns, operators and locale selectors use Laravel's grammar. Keep column and locale identifiers application-defined, not arbitrary request input. Ordinary tags such as `pt-BR` work, but path syntax in a locale changes the selector.

 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)
