localization - Replacement fields inside a multi-language application -
i developing project supports multiple languages. 1 of functions have support replacement parameters. here simplified example of mean:
a string "{custname} has 10 customers" defined somewhere. includes 1 parameter {custname}, defined within hierarchy string used. when item string opened up, {custname} resolves defined value.
since in languages, single word or phrase can change previous or following character(s) in sentence, how implemented replacement field functionality in situation?
you'll need few things.
(1). set functions return different translations based on quantity , rules of language.
aside customer name replacement part says 10 customers need replacement , need built function call looks more like:
ngettext( 'customer', 'customers', 10 ) this along lines of how gettext works.
(2). set translation source strings such they're aware of pluralization rules.
you haven't said technology you're working with, gettext has built in , many languages including php can interact system gettext.
(3). organize text replacement 2 stages. possibly using sprintf instead of token replacement, part you.
because you're using stored translations plus own customer name replacement i'd follows:
set translation strings full template in each language, perhaps in gettext po file:
# .... msgid "%1$s has 1 customer" msgid_plural "%1$s has %2$u customers" msgstr[0] "%1$s un client" msgstr[1] "%2$u clients pour %1$s" you fetch required template based on quantity , perform replacement afterwards. example in php:
$n = 10; $name = "pierre"; $template = ngettext( '%1$s has 1 customer', '%1$s has %2$u customers', $n ); $rendered = sprintf( $template, $name, $n ); there lots of gotchas here, , not language pack formats support plurals. if can't use gettext in system have @ loco way manage rules of plurals , export file format can work with.
Comments
Post a Comment