kip

kip

ex_cldr Core Team

localize_ecto is the forgotten member of the elixir-localize family. When I announced Localize_mcp yesterday I had forgotten that I hadn’t actually published this either!

The most common database text sorting is usually byte or code-point order - Zebra before apple, öl after zebra - which isn’t a great experience for developers or users.

Postgres, however, ships the full set of ICU collations, and localize_ecto connects them to Ecto
with a collate/1,2 query macro that applies a COLLATE clause using the collation that best matches a Localize locale,

Sorting in ORDER BY

import Ecto.Query
import Localize.Ecto

# Sort by the current locale (Localize.get_locale/0)
from p in Product, order_by: collate(p.name)

# Or an explicit locale - runtime values pin as usual
from p in Product, order_by: collate(p.name, "sv")
# SELECT ... ORDER BY p0."name" COLLATE "sv-x-icu"`

The same data sorts differently per locale, as it should. German orders ä with a; Swedish puts it after z:

de: ["Apfel", "Äpfel", "öl", "Zebra"]
sv: ["Apfel", "Zebra", "Äpfel", "öl"]

Any valid locale finds its best available collation — "zh-TW" resolves to Postgres predefined zh-Hant-x-icu, "de-DE" to de-x-icu, "pt-BR" to pt-BR-x-icu.

Collating in SELECT and WHERE

collate/2 works anywhere a query expression is accepted, and it also collates comparisons like Postgres’s a < b COLLATE "de-x-icu" form:

# A collated expression in a select
from p in Product, select: collate(p.name, "de")

# A collated comparison in a select
from p in Product, select: collate(p.name < p.brand, "de")
# SELECT p0."name" < p0."brand" COLLATE "de-x-icu"

# And in a where clause`
from p in Product, where: collate(p.name < "münchen", "de"), select: p.name

German phonebook order, via a migration

Locales can carry a collation type in their -u-co- extension — German phonebook order treats ü as ue. PostgreSQL doesn’t predefine these, so localize_ecto provides reversible migration helpers:

defmodule MyApp.Repo.Migrations.AddPhonebookCollation do
  use Ecto.Migration
  import Localize.Ecto.Migration

  def change do
    create_collation("de-u-co-phonebk")
  end
end

After the migration, the locale just works in queries:

from p in Person, order_by: collate(p.surname, "de-u-co-phonebk")

If collated with the default collator would return ["Mahler", "Mueller", "Muller", "Müller"] but with the phonebook collator returns ["Mahler", "Mueller", "Müller", "Muller"].

More migration examples

# A custom name, used in queries with collate(p.name, collation: "german_phonebook")
create_collation("de-u-co-phonebk", name: "german_phonebook")`

# Natural sort: file1, file2, file10 — instead of file1, file10, file2`
create_collation("und-u-kn", name: "natural_sort")`

# Case-insensitive equality: 'HELLO' = 'hello' without citext or lower()`
create_collation("und-u-ks-level2", name: "case_insensitive", deterministic: false)`

# A collated index, so ORDER BY ... COLLATE is an index scan, not a sort`
create index("products", [collated(:name, "de")], name: :products_name_de)`

Notes

  • Postgres only.
  • The collations are deterministic (PostgreSQL’s default), so normalize your Unicode text (NFC) for expected equality semantics — the README has the details.
  • Two guides ship with the docs: Using Localize Ecto and Collations in PostgreSQL — the latter covers choosing a database default collation (short version: builtin C.UTF-8 on PostgreSQL 17+, C before that, and COLLATE in queries and indexes for linguistic behaviour).

First Post! Switch mode

nathanl

nathanl

Very cool!

— All posts loaded —

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

Other Trending Topics Top

juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New

We're in Beta

About us Mission Statement