A couple of weeks ago we talked to Caspar Hübinger about WordPress internationalization (i18n). I assume you already know what i18n is, but let me briefly remind you the concept:
Internationalization is the process of developing your plugin so it can easily be translated into other languages. Localization describes the subsequent process of translating an internationalized plugin. (…)
Because WordPress is used all over the world, it is a good idea to prepare a WordPress plugin so that it can be easily translated into whatever language is needed. As a developer, you may not have an easy time providing localizations for all your users; You may not speak their language after all. However, any developer can successfully internationalize a theme to allow others to create a localization without the need to modify the source code itself.
If you’re running a business, one of your goals should be to get the maximum number of customers possible. Internationalization is one of the best tools you have to grow your user base. That’s why you should take special care in making your code translatable. It doesn’t really matter how simple or silly your current plugin is—don’t postpone its internationalization and get it ready now. An internationalized plugin will benefit both you and your users—you’ll get more users and they’ll get a plugin that better suits their needs.
But let’s stop messing around—I don’t want to persuade you to make your plugins i18n-friendly; I’ll just assume you already do. Today I want to talk about plural i18n and propose an alternative solution on how to deal with them.
Let’s assume we want to write on screen something like “x files deleted”. Such a sentence has two possible forms in English:
- Singular. When there’s only one element (in this case, one file), we use the singular form: 1 file deleted.
- Plural. When there’s more than one element (or zero), we use the plural form: 3 files deleted.
Both sentences are pretty similar, but not exactly the same. On the one hand, there’s the “number of elements” we’re talking about (1 or 3 elements in our example). On the other hand, some nouns in the sentence change, so that they match the number of files (file vs files). Obvious, right?
Proper internationalization takes into account two things: (a) one must use the proper phrasing and (b) the sentence should include the actual count of elements. These two things are obvious when you inspect the code and the functions we use to get this result:
_n()is a gettext function that selects the proper sentence based on the number of elements we have. The function has three arguments (because that’s all we need in English):
- The singular version of the sentence.
- The plural version of the sentence.
- The number of elements we have (which will tell the function the result we expect is the singular or the plural version).
printf()is the function that will replace a placeholder within the sentence with the actual value of the number of elements.
The previous example would look like this in PHP:
printf( _n( '%d file deleted.', '%d files deleted.', $count ), $count );
$count appears twice. The first time as an argument of
_n, which will help the function decide which variant we want. The second time as an argument of
printf, so that we can replace the placeholder (
%d) with the actual value stored in
$count. This might help you better understand what I’m talking about:
$sentence = _n( '%d file deleted.', '%d files deleted.', $count ); printf( $sentence, $count );
If you’re an English speaker (or Spanish, Italian, German, French…), what’s about to come might sound confusing—but bear with me, for it’s very important. When internationalizing plurals, remember you’re not translating “two sentences”; you’re translating “a concept”. In our example, the concept we want to translate is “a certain number of files has been deleted”. We don’t know how many (we don’t care how many)—we just know some files were deleted. In English (and Spanish, Italian, German, French, and many other languages in the world), this concept can be verbalized with two (and only two) sentences: one “singular” and one “plural”. But other languages might have multiple plural forms, and therefore
$sentence might have 3, 4, 5 or even 6 different variants!
What I’m trying to say is—translators (in general) don’t translate the singular sentence first and the plural sentence second. Instead, they translate the whole concept into as many sentences as their language requires.
I Don’t Like Singular Sentences That Use the Number 1
In our last interview, Caspar share this test in which you can check how well you internationalize your plugins/themes. The test is well designed, with multiple answers per question and detailed explanations teaching you why a certain solution is right or wrong. One of the questions in the test was this:
Which of these is the correct way to use the single/plural
printf( _n( 'One person has seen this post.', '%d people have seen this post.', $view_count ), $view_count );
printf( _n( '%d person has seen this post.', '%d people have seen this post.', $view_count ), $view_count );
Both options would work perfectly fine, but the test discourages using the first one:
The hardcoded “One” in the singular string is problematic. We always want to use a placeholder in both singular and plural strings. Some languages (such as Russian) have multiple plurals which require the flexibility provided by using the placeholder in the singular string.
So, what’s wrong with it? Apparently, in Russian “1 person” (1 человек) looks exactly the same as “321 people” (321 человек). As a consequence, the sentence we use for “1” can’t be written with the word “One”—it must use a placeholder so that it works with both “1” and “321”. The official, recommended solution? Always use placeholders!
Well, I’d rather not.
Let’s forget for one second about internationalization. In my humble opinion, “One person has seen this post” (or its Spanish equivalent, if you ask me, “Una persona ha visto esta entrada”) it’s way better than the alternative “1 person has seen this post” (“1 persona ha visto esta entrada”). And I think I’m not the only one—if this question exists in the first place is probably because there’s plenty of plugin developers that prefer the “One” version.
So, if we like “One” better, is it a problem if we use it or not? The test tells us that “One” is problematic… but is it? Really? I don’t think so.
The Problem that Doesn’t Exist
Let’s consider the “problematic” strings:
- One person has seen this post.
- %d people have seen this post.
If I were to translate them into Spanish, I’d probably follow the same pattern:
- Una persona ha visto esta entrada.
- %d personas han visto esta entrada.
and everything would obviously work as expected. “Sure, it works because both Spanish and English follow the same singular/plural pattern, so anything (plural-related) that works in English would work in Spanish too”. You’re right. So let’s assume that Spanish uses a different rule: all numbers ending in “1” use the first sentence, the rest use the second one.
In this hypothetical scenario (which is very real in some languages other than English or Spanish), our first sentence can’t include the word “Una”. This first sentence can be used with numbers as diverse as 1, 21, or 581—we need to use a placeholder! Well, then use it. Let’s translate the original English strings as follows:
- %d persona ha visto esta entrada.
- %d personas han visto esta entrada.
There you have it! We now have two sentences using placeholders. “Wait, but the original singular sentence didn’t include a placeholder”. So what? We’re not translating the singular and the plural sentences—we’re translating the whole concept. Therefore, if my language needs two sentences and both sentences need a placeholder, let’s add it.
Remember that i18n is a two-step process: (a) select the appropriate sentence and (b) replace all placeholders with the actual values. If a sentence doesn’t include a placeholder,
printf will print it as is; if it does have a placeholder, it’ll replace it with the actual value.
It doesn’t matter if the original English version includes a placeholder or not—we can “always” add it if the target language needs it. The solution is not about limiting the style of our phrasing in English; we need to educate translators. We need them to understand that (a) they’re translating a whole concept that will be materialized in more than one sentence and (b) some of these sentences might need a placeholder, if the rules in their language requires it.
I always say the same thing—translators are also interpreters. Adapting a translation to their own language might also involve adapting and tweaking the usage of placeholders. And that’s perfectly fine. I’d say it’s even better!
So, what do you think? Should we use placeholders always or should we go for the solution that looks the best in each language?
Featured Image by Clark Young.