symfony/symfony — pull request #4884 by sroddy (2012-07-12 20:20:05+00:00)
Title: [Translation] added support for translations depending on gender and number of variables

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
License of the code: MIT

This improvement adds to the translator component the possibility to pass variables specifying the gender and the number. This enables the translators to handle all the specificities of their own language. With an easy to understand but strict syntax, I've managed not to break the BC, while adding a very small footprint in the trans method (due to a single validation regexp which is anyways improvable).

This the syntax ref of a translated message:

%variable% [gender][plural_position]: String

Where
%variable% is any kind of string (all characters except spaces and |),
[gender] is one of the following: m n f (male, female, neuter)
[plural_position] is an integer that follows the same convetions of the pluralization rules that you can find in Symfony\Component\Translation\PluralizationRules
String is any kind of string (except the | sign)

You can specify (and it's highly reccomended) a generic translation as the first position (without using any specific syntax).
This will be returned in case no other translation matches the given parameters.

Some examples follow:
%user% wrote on %friend%'s wall for his/her birthday | %friend% f: %user% wrote on %friend%'s wall for her birthday | %friend% m: %user% wrote on %friend%'s wall for his birthday

You are a very good boy! | %user% m1: You are very good boys! | %user% f0: You are a very good girl! | %user% 1f: You are very good girls!

I'd like to hear some feedbacks about this implementation so I can adjust what you think is wrong and then write the documentation.