Smarty gettext plugin

1,246 views
Skip to first unread message

Thue Janus Kristensen

unread,
Mar 31, 2011, 1:37:15 PM3/31/11
to smarty-d...@googlegroups.com
Attached is my proposal for a Smarty GNU gettext block, to be included in the Smarty 3 distribution.

GNU gettext is a way to manage webpages in several languages ( http://www.gnu.org/software/gettext/ ). By using gettext to translate content in smarty template, we can reuse the large gettext code base, and people who know gettext in advance will not have to learn a new translation system.

With a native smarty translation block, you would need to store the text in the .php file, assign the result to a variable, and then include the variable inside .tpl . By translating inside the template, the text is maintained where it is used, which makes everything more user-friendly.

Since you tend to have to translate lots of texts, a short name is warrented, so I have reserved the name "t" for this purpose.

An example template:

{assign var="myemail" value="<a href=\"mailto:thu...@gmail.com\">thu...@gmail.com</a>"}
{t e="html" email=$myemail}Mail to %email|none% for support if percentage > 3%%{/t}
Which will output (normally translated, but not in this case)
Mail to <a href="mailto:thu...@gmail.com">thu...@gmail.com</a> for support if percentage &gt; 3%

Points about the above example:
-The e="html" block args means that ">" is escaped to "&gt;" in the output. e=... uses the standard |escape modifier. The e=... argument defaults to html, so is actually not needed in this case.
-%email% is substituted with the email=... argument
-Normally the content of the substituted %email% is escaped, but since there is a "|none" inside %email|none% it is not in this case. This is the reason why I support "|none" inside the string, instead of in the block args.
-"%" is escaped as "%%"

Other reserved arguments to the "t" block:
-charset: Which charset the escape functions should use
-assign: Assign the result to this variable, instead of outputting it

--------------

In the patch I included an smarty_xgettext.php utility, used for extracting texts into a .pot file for translation, similar to the standard xgettext utility.

This utility also has the ability to merge the extracted texts into an existing .pot file. This means that if the same text exists in both a .php and a .tpl file, and you already generated a .pot file from the .php file, then smarty_xgettext.php will merge the occurances, so it only has to be translated once.

Right now, smarty_xgettext.php uses a regexp to extract the strings from the templates. This almost always works, but there will always be cases where it fails. One could consider hooking into the smarty parser to extract the strings to be translated.

--------------

The gettext setup itself is assumed to have already happened in the php code which uses the "t"-template.

--------------

I have been using this code in production for 6 months. I had to make some small changes to make it suitable for the Smarty distribution; these small changes are not be so well tested.

Regards, Thue
smarty_gettext.patch

Thue Janus Kristensen

unread,
Jun 3, 2011, 4:47:24 PM6/3/11
to smarty-d...@googlegroups.com
No response?

If you are worried about supporting it, you are welcome to put a big "unstable" label on it, and forward all support requests to me.

Regards, Thue

2011/3/31 Thue Janus Kristensen <thu...@gmail.com>

globe

unread,
Jun 4, 2011, 4:43:52 AM6/4/11
to Smarty Developers
Hey There,

I beliebe Smarty should put up some sort of plugin repository where
stuff like yours can be published and maintained. Somewhat like
http://plugins.jquery.com/



On Jun 3, 10:47 pm, Thue Janus Kristensen <thu...@gmail.com> wrote:
> No response?
>
> If you are worried about supporting it, you are welcome to put a big
> "unstable" label on it, and forward all support requests to me.
>
> Regards, Thue
>
> 2011/3/31 Thue Janus Kristensen <thu...@gmail.com>
>
>
>
>
>
>
>
> > Attached is my proposal for a Smarty GNU gettext block, to be included in
> > the Smarty 3 distribution.
>
> > GNU gettext is a way to manage webpages in several languages (
> >http://www.gnu.org/software/gettext/). By using gettext to translate
> > -plural: Seehttp://www.php.net/manual/en/function.ngettext.php
Reply all
Reply to author
Forward
0 new messages