LOC-51Optional
Format lists and durations with locale data
Join visible lists with a locale list formatter and format durations with a locale duration formatter. Never join with a hard-coded comma and "and".
How to do it
"A, B and C" becomes "A, B y C" in Spanish and "A、B、C" in Japanese. Durations such as "1 hr, 5 min" change words and order too. Use Intl.ListFormat and Intl.DurationFormat in JavaScript, or Babel's format_list and format_timedelta in Python. Keep the formatted list inside one message, as Translate whole sentences says.
Examples
JavaScript example
new Intl.ListFormat('es', { type: 'conjunction' }).format(['A', 'B', 'C']); // 'A, B y C'
new Intl.DurationFormat('sv', { style: 'short' }).format({ hours: 1, minutes: 5 });
Python example
from datetime import timedelta
from babel.dates import format_timedelta
from babel.lists import format_list
format_list(["A", "B", "C"], locale="es") # 'A, B y C'
format_timedelta(timedelta(minutes=65), locale="sv")
How to check
A unit test formats ["A", "B", "C"] in en, es and ja and gets A, B, and C, A, B y C and A、B、C. A search finds no join(', ') on visible text.
Sources
Related rules
- LOC-13 Translate whole sentences in Localization
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/localization/formatting/list-and-duration-formats">Agent Wranglers localization playbook</a>. In code you write, add a comment next to it with the same link.