From: Reini U. <ru...@x-...> - 2001-12-02 01:44:04
|
Carsten Klapp schrieb: > Today while I was adding a few more German translations to the > template/Edit.html I realized a small problem in using the _("text") > function to translate localized text, has this already been discussed? > > Different languages have different word ordering. When a key word needs to > be added to the end of a sentence in english, it might need to be inserted > into the middle of the sentence in another language. > > For example: > > English: > $_("Author will be logged as") <i>${USERID}</i>. > > German: > $_("Der Autor wird als") <i>${USERID}</i> aufgenommen. > > Is there an existing way around this? Without a code-based solution Wiki > may be stuck with individual localized templates. The standard answer is sprintf. sprintf(_("Der Autor wird als %s aufgenommen"), "<i>$USERID</i>") But this imposes problems to at least my hord of inexperienced translators. (they don't know about %s or %d, they don't know what value will be inserted, which variable, and how many % vars need to be inserted) So I use a limited set of variables which get expanded at run-time from the definitions strings. something like: register_gettext_expansion('USERID'); echo x_('Der Autor wird als $USERID aufgenommen'); writing this from scratch... (totally untested. I use a completely different translation scheme on my current project. translators do it online). I register only uppercase vars for this. function register_gettext_expansion ($var) { if (!is_array($GLOBALS['GETTEXT_VARS'])) $GLOBALS['GETTEXT_VARS'] = array(); $GLOBALS['GETTEXT_VARS'][] = $var; } function x_($msg) { $msg = gettext($msg); // this could be improved to expand on all vars in the msg, // not on all existing vars... if (!(($i = strpos($msg, '$')) === false) and (substr($msg,$i-1,1) != '\\')) { foreach ($GLOBALS['GETTEXT_VARS'] as $s) { if (!empty($GLOBALS[$s])) $msg = str_replace('$' . $s, $GLOBALS[$s], $msg); } } return $msg; } The problem is that $ must be quoted and that I did't check for that in my old version. Luckily I don't have us money in my strings :) Another problem is that the gettext extraction tools only recognize _("") and not _(''). so we must use echo x_("Der Autor wird als \$USERID aufgenommen"); -- Reini Urban http://xarch.tu-graz.ac.at/home/rurban/ |