Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Translations, embedded values

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Synchro
Smarty Regular


Joined: 27 Apr 2008
Posts: 43

PostPosted: Wed Jul 30, 2014 8:14 am    Post subject: Translations, embedded values Reply with quote

I'm adding multilingual support to a Smarty site and having trouble finding a decent solution. There are many ideas in this thread, but nearly all are overly simplistic. This doesn't look too bad, but I really don't like gettext.

One basic thing that appears to be missing from all the suggested solutions is support for embedded values, often implemented in an sprintf-style:

echo sprintf("Your name is %s, you work for %s", $name, $company);

Without embedded variables, you can end up with large numbers of tiny fragments which become difficult to manage, don't allow reordering, are error prone and reduce translation context even further.

One approach that has promise for this is to implement translation in a modifier, like this:

{"TRANSLATION_KEY"|t:$var1:$var2}

So the plain text is used by the t modifier to look up a translation, and the variable number of unnamed additional params are passed to it for embedding. The problem I'm running into with this is that modifiers don't get given a smarty instance (which could have locale and translation strings preloaded), which means they have to do excessive work to look up the translation.

Function plugins do get a smarty context, but don't allow unnamed variables, though you could do it with a single array var:

{t key="TRANSLATION_KEY" vars=array($var1, $var2)}

Both of these also allow use of other modifiers, and both can make use of string substitution codes other than %s to allow inline escaping (for example so you could have a URL with an embedded value and use URL rather than HTML escaping), or numbered params to allow reordering or repeated use.

All that said, you really want translation to be implemented as a prefilter for maximum efficiency, so perhaps some double-processing would work, where language string lookups are done in the prefilter and rendered into function tags for the substitutions, so you get the performance of a prefilter with the flexibility of a function.

Of all the translation systems I've looked at, the one that looks the most practical to integrate with Smarty is Zend Framework's i18n module.

Any comments or suggestions?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Jul 30, 2014 1:50 pm    Post subject: Reply with quote

You are right, there are lots of ways to to i8n, and your mileage may vary. As for accessing the Smarty instance inside a modifier, you can use a method like so:

http://www.smarty.net/forums/viewtopic.php?t=16604
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP