开发者

Handling grammatical gender with Gettext

开发者 https://www.devze.com 2023-03-08 11:02 出处:网络
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.

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 sp开发者_JAVA百科anish. His / her is a good use case in english. This is really needed when translating into spanish.

An example:

Considering users Pablo (male) and Ana María (female).

_('%{user} is tall') & {:user => user.name}

Should be translated to

'Pablo es alto'
'Ana 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})
}


What about using the plural form mechanism of gettext. Usually a parameter n is used to distinguish between singular and plural forms.

Now imagine to use n to define your gender instead of an amount. Thus p.ex. n=1 means female (and not singular) and n=2 (or n>1) means female (and not plural).

n = user.male? ? 1 : 0
n_('%{user} is tall', '%{user} is tall', n) & {:user => user.name}
0

精彩评论

暂无评论...
验证码 换一张
取 消