Sistema de traducción kostache y kohana

How to use kohana i18n __('translation') within kostache templates?

preguntado el 09 de marzo de 12 a las 16:03

2 Respuestas

Make an custom extension method for Kostache and use it like that.

I use somehting like this in the modules/kostache/classes/kohana/kostache.php archivo:

public function i18n(){
    return array('I18n', 'get');
}

You could probably do it a little nicer with PHP 5.3, but this works on all PHP versions

And in the templates, you will do something like:

{{#i18n}}Some translatable text{{/i18n}}

The nice thing about mustache is that you can use it in almost any language (including js, so you can use same templates for client and server side). Extending with custom methods adds a little complexity, so you will have to implement them in all languages where you use mustache. Luckily it is pretty simple to do it in js since everything is an object. For example, something like this would work:

var i18n= function(s){
    var someText = ""; // GET THE TRANSLATE TEXT (HOWEVER YOU LIKE)
    return someText;
}

Now you can use your extension method on the client as well as server.
Nice, isn't it :)

respondido 12 mar '12, 14:03

Hello could you please check this out stackoverflow.com/questions/21204333/… - Karem

The accepted solution wasn't working for me (maybe some version compatibility issues), so i'm writing my solution here, maybe it'll help someone.

My problem was that Mustache was passing a Mustache_LambdaHelper object as the second parameter where I18n::get() was expecting the language and it was throwing an exception.

La solución:

public function i18n() {
    return function($value, $helper) {
        return I18n::get($value);
    };
}

You can also use the array method, if anonymous functions are an issue, but you'd have to create an additional class or function to handle this, like so:

class i18nFilter {
    public function get($value, $helper) {
        return I18n::get($value);
    }
}

In this case, the function would become:

public function i18n() {
    return array('I18nFilter', 'get');
}

Also, you don't have to modify the Kohana_Kostache class in order to get this to work. You can just as well create the function in the view class.

contestado el 29 de mayo de 13 a las 12:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.