Skip to content

Add the sort_localized filter to sort values in the alphabetical order of a locale - #4943

Open
seb-jean wants to merge 1 commit into
twigphp:3.xfrom
seb-jean:intl-sort-localized
Open

seb-jean wants to merge 1 commit into
twigphp:3.xfrom
seb-jean:intl-sort-localized

Conversation

@seb-jean

Copy link
Copy Markdown
Contributor

The sort filter compares strings byte per byte, which sorts uppercase letters before lowercase ones and accented letters after z, so it cannot sort text the way a reader expects:

{# Boxe, Yoga, athlétisme, judo, Échecs, équitation #}
{{ ['Yoga', 'équitation', 'Boxe', 'athlétisme', 'Échecs', 'judo']|sort|join(', ') }}

This adds a sort_localized filter to twig/intl-extra, which sorts sequences and mappings with the collation rules of a locale:

{# fr: athlétisme, Boxe, Échecs, équitation, judo, Yoga #}
{{ ['Yoga', 'équitation', 'Boxe', 'athlétisme', 'Échecs', 'judo']|sort_localized|join(', ') }}

{# cs: hrad, chata, ideal (en: chata, hrad, ideal) #}
{{ ['ideal', 'chata', 'hrad']|sort_localized(locale: 'cs')|join(', ') }}
  • It uses the current locale by default and keeps the keys, like sort.
  • An optional arrow function returns the value to sort each item on, instead of comparing two items: cities|sort_localized(city => city.name).

@fabpot fabpot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not reuse the existing sort filter? Something like values|sort(locale: 'fr').

I understand that sort is in Twig core, while locale-aware collation needs PHP’s
optional Intl extension. But it's peobably manageable if Intl is required only when locale is supplied.

Comment thread extra/intl-extra/IntlExtension.php
Comment thread doc/filters/sort.rst Outdated
Comment thread doc/filters/sort_localized.rst Outdated
Comment thread doc/filters/sort_localized.rst Outdated
Comment thread extra/intl-extra/Tests/Fixtures/TranslatableFruit.php Outdated
Comment thread extra/intl-extra/composer.json Outdated
Comment thread extra/intl-extra/IntlExtension.php Outdated

@fabpot fabpot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not reuse the existing sort filter? Something like values|sort(locale: 'fr').

I understand that sort is in Twig core, while locale-aware collation needs PHP’s
optional Intl extension. But it's peobably manageable if Intl is required only when locale is supplied.

@seb-jean

Copy link
Copy Markdown
Contributor Author

Why not reuse the existing sort filter? Something like values|sort(locale: 'fr').

I understand that sort is in Twig core, while locale-aware collation needs PHP’s optional Intl extension. But it's peobably manageable if Intl is required only when locale is supplied.

I was thinking of something where the locale is optional and thus serves as the locale default.
With the example you provided, does the locale become mandatory? Is that really what we want?

Comment thread extra/intl-extra/IntlExtension.php Outdated
@seb-jean
seb-jean force-pushed the intl-sort-localized branch 2 times, most recently from 1776014 to c10e1ff Compare September 27, 2026 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants