I'm looking for a simple-proper-elegant way to handle grammatical gender with Gettext in a Rails application, the same way plurals are handled with n_()
method.
This has no interest in english, since words don't vary with gender, but it does when translating into spanish. His / her is a good use case in english. This is really needed when translating into spanish.
An example:
Considering users Pablo (male) and María (female).
_('%{user} is tall') & {:user => user.name}
Should be translated to
'Pablo es alto'
'María es alta'
Of course, we have access to user.gender
Any ideas?
Cheers!
Using standard gettext features this can be solved using contexts. Like calling appropriate:
p_('Male', '%{user} is tall')
or
p_('Female', '%{user} is tall')
This will generate two separate strings in gettext catalogs marking them with "Male" and "Female" contexts.
Unfortunately no. This is a limitation of the gettext system--aside from number, linguistic features are based on the language you key off of. If you were to key all of your strings in Spanish, it would work.
Another option would be to append a character to the string for translation's sake, and then strip it off.
I'm not familiar with Ruby, but the basic idea in psuedo-code would be:
if (user.sex == male) {
strip_last_char(_('%{user} is tall♂') & {:user => user.name})
} else {
strip_last_char(_('%{user} is tall♀') & {:user => user.name})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With