-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#__}}Hello.{{/__}} {{#__}}My name is {{ name }}.{{/__}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hola. Me llamo Bob. |
f77cf37
There was a problem hiding this comment.
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.
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.
Many of my translation strings need not be templates, though.
And I end up with hundreds of cache files with classes whose sole purpose is to output a bit of text.
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.
f77cf37
There was a problem hiding this comment.
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?
f77cf37
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on the passthrough
f77cf37
There was a problem hiding this comment.
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)
f77cf37
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍