Question

I'm having different languages on my web application.

Now i would like to do write: About us in the mustache file.

but then depending on the language the user has chosen (logic in the view/controller), it should display the correct translation for About us.

The translation would be something i have stored for exactly the words: About us

I have seen another webapplication that does it this way:

{{#lang}}About us{{/lang}}

But I dont understand how this is working? How can the lang() method in the view model grab the data within #lang, "About us" - and then replace it with something else, if exists.

(the procedure grabbing the translation from database or file, that matches "About us" do i not need to know)

I didn't know it is possible to reverse like that, sending the "About us" to the lang() method in view model?

Hope someone can explain and with example. Thanks

This is what I tried, in my view:

public function lang($input)
{
    return "test" . $input;
}

But this does not work. (No argument passed to lang() )

I am using Mustache (Kostache) together with PHP in a MVC framework (kohana)

Was it helpful?

Solution 2

After alot of headache, very bad google results, here is the correct solution if you use Kostache 2 and Kohana 3.2/3.0 :

SIMPLY, in the Kohana_Kostache class at the factory() method where Mustache_Engine intializes, you add a helper function:

'helpers' => array(
                    'i18n' => function($text) {
                        return __($text);
                    }),

Since I use the Kohana Translation system, I called it i18n and it returns __($text), which is the $text translated if exists.

OTHER TIPS

Assuming you have mustache defined as $m the following would add the function lang when the template was being parsed.

The key here is passing the function to mustache when rendering.

$data = new StdClass;
$data->lang = function($text) {
    return "Requested lang: $text";
}

$m.render($template, $data);

This template

{{#lang}}About us{{/lang}}

Would become

Requested lang: About us
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top