Okay, but how do I...

The hidden feature of pluralization in CSV in Godot

During the development of Tens! (out now on Poki, check it out!), we decided that it would be cool to localize our game to languages we expect to be popular. We knew that Godot has a built-in i18n API (i18n is short for internationalization) and it was a perfect opportunity to learn how to use it.

Godot supports 2 formats of localization files: gettext (also known as PO files) and CSV (also known as an easy way to store spreadsheets in text). We wanted the simplest solution, so we went with the latter. It was sufficient until we stumbled upon one tiny phrase that needed pluralization.

Until Godot 4.6, there was no way to pluralize phrases if you use CSV. Now there is one, but documentation for this feature is a bit lacking, and it misses the most important piece that will save you a lot of figuring out.

Docs mention a ?plural column that can be used for simple pluralization, like in English you only have either 1 die or many dice. They also mention some elusive ?pluralrule that looks like a piece of code and it's not really obvious how it's constructed and what it affects. Actually, it's a feature of aforementioned gettext that was already used by Godot in PO file processor and now it's available for CSV users too. If you want to understand how it works, read this page.

If you don't want to figure out how ?pluralrule works and what is the correct rule for your language, great news: you don't have to. Godot has built-in pluralization rules for many languages and will use them automatically if you format your CSV right. A pluralized phrase is formatted like this:

keys ?plural en ru
SCORE - %d point %d очко
%d points %d очка
%d очков

Or in CSV:

keys,?plural,en,ru
SCORE,,%d points, %d очко
,,%d points,%d очка
,,,%d очков

Note that:

With a setup like this, you can use tr_n("SCORE", "%d points", score) % score to display score, and Godot will use the correct string automatically.

Translation rules are described in Godot's code here. Below is an explanation in English, use search to find your language.

List of Built-in Pluralization Rules