Permalink
Browse files

Add an i18n example.

  • Loading branch information...
bobthecow committed Jan 3, 2012
1 parent 3731898 commit f77cf3729de0e46d6187da2353a4ab438657b6fe
Showing with 22 additions and 0 deletions.
  1. +20 −0 examples/i18n/I18n.php
  2. +1 −0 examples/i18n/i18n.mustache
  3. +1 −0 examples/i18n/i18n.txt
View
@@ -0,0 +1,20 @@
<?php
class I18n extends Mustache {
// Variable to be interpolated
public $name = 'Bob';
// Add a {{#__}} lambda for i18n
public $__ = array(self, '__trans');
// A *very* small i18n dictionary :)
private $dictionary = array(
'Hello.' => 'Hola.',
'My name is {{ name }}.' => 'Me llamo {{ name }}.',
);
public function __trans($text) {
return isset($this->dictionary[$text]) ? $this->dictionary[$text] : $text;
}
}
@@ -0,0 +1 @@
{{#__}}Hello.{{/__}} {{#__}}My name is {{ name }}.{{/__}}
View
@@ -0,0 +1 @@
Hola. Me llamo Bob.

5 comments on commit f77cf37

@mattdeclaire

This comment has been minimized.

Show comment
Hide comment
@mattdeclaire

mattdeclaire Apr 18, 2013

Contributor

I'm using helpers for I18N.

$mustache = new Mustache_Engine(array(
    'loader' => new Mustache_Loader_FilesystemLoader('/path/to/templates'),
    'cache' => '/path/to/cache',
    'helpers' => array(
        'i18n' => function($text) {
            return my_i18n($text);
        },
    ),
));

I was surprised (though, I shouldn't have been) to find the cache directory growing faster than I expected, as the engine cached individual translations as templates. It now makes sense. The return value of a helper needs to be rendered just like any other template.

<p>{{#i18n}}Hello there, {{name}}.{{/i18n}}</p>

Many of my translation strings need not be templates, though.

<label>{{#i18n}}remember me{{/i18n}}</label>

And I end up with hundreds of cache files with classes whose sole purpose is to output a bit of text.

class __Mustache_61f04c543a33602abb15e4b05c605219 extends Mustache_Template
{
    private $lambdaHelper;

    public function renderInternal(Mustache_Context $context, $indent = '', $escape = false)
    {
        $this->lambdaHelper = new Mustache_LambdaHelper($this->mustache, $context);
        $buffer = '';

        $buffer .= $indent . 'remember me';

        if ($escape) {
            return call_user_func($this->mustache->getEscape(), $buffer);
        } else {
            return $buffer;
        }
    }
}

That's a lot of work for effectively an echo: md5 the string, instantiating the class (and the Mustache_LambdaHelper), and several layers of function calls. Not to mention that a full site's worth of translation, the permutations of translation strings to number of languages, it going to generate a hefty cache directory.

Am I worrying over nothing?

Originally, I wanted to precache all my templates on deploy, until I realized that helpers effectively make templates on the fly.

Contributor

mattdeclaire replied Apr 18, 2013

I'm using helpers for I18N.

$mustache = new Mustache_Engine(array(
    'loader' => new Mustache_Loader_FilesystemLoader('/path/to/templates'),
    'cache' => '/path/to/cache',
    'helpers' => array(
        'i18n' => function($text) {
            return my_i18n($text);
        },
    ),
));

I was surprised (though, I shouldn't have been) to find the cache directory growing faster than I expected, as the engine cached individual translations as templates. It now makes sense. The return value of a helper needs to be rendered just like any other template.

<p>{{#i18n}}Hello there, {{name}}.{{/i18n}}</p>

Many of my translation strings need not be templates, though.

<label>{{#i18n}}remember me{{/i18n}}</label>

And I end up with hundreds of cache files with classes whose sole purpose is to output a bit of text.

class __Mustache_61f04c543a33602abb15e4b05c605219 extends Mustache_Template
{
    private $lambdaHelper;

    public function renderInternal(Mustache_Context $context, $indent = '', $escape = false)
    {
        $this->lambdaHelper = new Mustache_LambdaHelper($this->mustache, $context);
        $buffer = '';

        $buffer .= $indent . 'remember me';

        if ($escape) {
            return call_user_func($this->mustache->getEscape(), $buffer);
        } else {
            return $buffer;
        }
    }
}

That's a lot of work for effectively an echo: md5 the string, instantiating the class (and the Mustache_LambdaHelper), and several layers of function calls. Not to mention that a full site's worth of translation, the permutations of translation strings to number of languages, it going to generate a hefty cache directory.

Am I worrying over nothing?

Originally, I wanted to precache all my templates on deploy, until I realized that helpers effectively make templates on the fly.

@bobthecow

This comment has been minimized.

Show comment
Hide comment
@bobthecow

bobthecow Apr 18, 2013

Owner

As of v2.3.0 there's a lambda-less version of the compiled template:

05e527f

... so that helps with the complexity, but isn't exactly what you were worried about :)

Perhaps we could add an optimization to essentially do a string passthrough (i.e. return a string rather than a template instance) for lambdas which return a string with no mustaches in it? That would at least save you from extra templates and instantiation for things that couldn't possibly be anything other than a string output?

Owner

bobthecow replied Apr 18, 2013

As of v2.3.0 there's a lambda-less version of the compiled template:

05e527f

... so that helps with the complexity, but isn't exactly what you were worried about :)

Perhaps we could add an optimization to essentially do a string passthrough (i.e. return a string rather than a template instance) for lambdas which return a string with no mustaches in it? That would at least save you from extra templates and instantiation for things that couldn't possibly be anything other than a string output?

@sroussey

This comment has been minimized.

Show comment
Hide comment
@sroussey

sroussey Apr 18, 2013

+1 on the passthrough

+1 on the passthrough

@bobthecow

This comment has been minimized.

Show comment
Hide comment
@bobthecow

bobthecow Aug 14, 2014

Owner

(in case anyone stumbles onto the end of this thread at some point, the passthrough optimization was implemented in 6b7f33c)

Owner

bobthecow replied Aug 14, 2014

(in case anyone stumbles onto the end of this thread at some point, the passthrough optimization was implemented in 6b7f33c)

@mattdeclaire

This comment has been minimized.

Show comment
Hide comment
@mattdeclaire

mattdeclaire Aug 14, 2014

Contributor

👍

Contributor

mattdeclaire replied Aug 14, 2014

👍

Please sign in to comment.