Skip to content

Commit

Permalink
Add an i18n example.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Jan 3, 2012
1 parent 3731898 commit f77cf37
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/i18n/I18n.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions examples/i18n/i18n.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#__}}Hello.{{/__}} {{#__}}My name is {{ name }}.{{/__}}
1 change: 1 addition & 0 deletions examples/i18n/i18n.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hola. Me llamo Bob.

5 comments on commit f77cf37

@mattdeclaire
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on the passthrough

@bobthecow
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@mattdeclaire
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.