Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling grammatical gender with Gettext

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!

like image 932
dgilperez Avatar asked May 26 '11 18:05

dgilperez


2 Answers

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.

like image 194
sbasalaev Avatar answered Nov 01 '22 23:11

sbasalaev


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})
}
like image 35
DougW Avatar answered Nov 01 '22 21:11

DougW