LOC-24Recommended
Choose gender and other variants inside the message
When a sentence changes with a person's gender, a role or another fixed choice, put every variant in one message with a select argument. Never pass a translated word into a placeholder.
How to do it
Many languages change articles, adjectives and verbs with gender, so "{name} added {pronoun} photo" cannot be translated by swapping one word. ICU MessageFormat has select for fixed keywords and plural for counts. Put select on the outside and at most one plural inside, as the ICU guide advises, so a translator sees whole sentences. Always include an other branch, used when the value is unknown or not given. Take gender only from a choice the person made, never guess it from a name. Build counts as Get plurals right in every language says.
Examples
JavaScript example
import { IntlMessageFormat } from 'intl-messageformat';
const added = new IntlMessageFormat(
'{gender, select, female {{name} added a photo to her album.} male {{name} added a photo to his album.} other {{name} added a photo to their album.}}',
'en',
);
added.format({ gender: 'other', name: 'Sam' }); // "Sam added a photo to their album."
PHP example
// PHP with the intl extension
$added = new MessageFormatter('en', '{gender, select, female {{name} added a photo to her album.} male {{name} added a photo to his album.} other {{name} added a photo to their album.}}');
echo $added->format(['gender' => 'female', 'name' => 'Ana']); // "Ana added a photo to her album."
Java example
// Java with ICU4J
import com.ibm.icu.text.MessageFormat;
import java.util.Map;
MessageFormat added = new MessageFormat("{gender, select, female {{name} added a photo to her album.} other {{name} added a photo to their album.}}");
added.format(Map.of("gender", "unknown", "name", "Sam")); // "Sam added a photo to their album."
How to check
A unit test formats each message that has a select with every listed keyword and with an unknown value. Each output is a full sentence, and the unknown value uses the other branch.
Sources
Related rules
- LOC-12 Get plurals right in every language 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/messages-and-translation/select-messages">Agent Wranglers localization playbook</a>. In code you write, add a comment next to it with the same link.