stable

Clone or download

Read-only

Introduce gettext helpers

Part of request #10790: Use gettext directly in mustache templates We shouldn't have to pollute our presenters with i18n strings. Those strings should be directly enclosed in the mustache files. To do that we introduce gettext helpers in the mustache engine so that we can use: <h1>{{# gettext }}Access Logs{{/ gettext }}</h1> Instead of: # .mustache <h1>{{ access_logs_title }}</h1> # .php class XxxPresenter { public $access_logs_title; public function __construct() { $this->access_logs_title = gettext('Access Logs'); } } This will also decrease usage of unescaped mustache + manually purified variables. To test you can create the following files: plugins/tracker/test.php ``` <?php require_once 'pre.php'; TemplateRendererFactory::build() ->getRenderer(__DIR__) ->renderToPage( 'i18n', array( 'members' => array(0, 1, 2), 'groups' => array( array('names' => 'Devs', 'nb' => 1), array('names' => 'Devs, Admins', 'nb' => 2), array('names' => 'I <b>am</b> nasty <img src=a onerror=alert(1) />', 'nb' => 1), ), ) ); ``` plugins/tracker/i18n.mustache ``` {{# gettext }}Access Logs{{/ gettext }} <ul> {{# members }}<li> {{# ngettext }} %d member | %d members | {{ . }} {{/ ngettext }} </li> {{/ members }} </ul> {{# dgettext }} tuleap-tracker | Error during parsing expert query {{/ dgettext }} <ul> {{# groups }} <li> {{# dngettext }} tuleap-tracker | Group '%s' couldn't be added. | Groups '%s' couldn't be added. | {{ nb }} | {{ names }} {{/ dngettext }} </li> {{/ groups }} </ul> Bye bye triple mustache + manually escaped variables \o/:<br> {{# gettext }}Beyond 3 columns, each column<br>will have the same size.{{/ gettext }} ``` Go to /plugins/tracker/test.php?group_id=1, if the language of your tuleap user is french, then you should get: ``` Journal des accès * 0 membre * 1 membre * 2 membres Erreur pendant l'analyse de la requête experte * Le groupe 'Devs' n'a pas pu être ajouté * Les groupes 'Devs, Admins' n'ont pas pu être ajoutés * Le groupe 'I <b>am</b> nasty <img src=a onerror=alert(1) />' n'a pas pu être ajouté Bye bye triple mustache + manually escaped variables \o/: Au-delà de 3 colonnes, chaque colonne aura la même taille. ``` This test cover gettext, ngettext, dgettext, and dngettext usage. Extraction of strings in .pot files for each domain will be done in a dedicated commit Change-Id: I95f0236d175f1639f709d660d1a1ddf2b7fbfda4

Modified Files

Name
M src/common/autoload.php +4 −2 Go to diff View file
A src/common/templating/mustache/GettextHelper.php +110 −0 Go to diff View file
A src/common/templating/mustache/InvalidGettextStringException.php +29 −0 Go to diff View file
M src/common/templating/mustache/MustacheEngine.php +17 −1 Go to diff View file
A tests/simpletest/common/templating/Mustache/GettextHelperTest.php +136 −0 Go to diff View file
A tests/simpletest/common/templating/Mustache/MustacheEngineTest.php +41 −0 Go to diff View file