Skip to content

Instantly share code, notes, and snippets.

@mauricius
Last active August 29, 2015 14:22
Show Gist options
  • Save mauricius/057d82da3b4122ee65a1 to your computer and use it in GitHub Desktop.
Save mauricius/057d82da3b4122ee65a1 to your computer and use it in GitHub Desktop.
Extend Processwire TemplateEngineTwig.module with the function to enable i18n translations.
protected function ___initTwig(Twig_Environment $twig)
{
$function = new Twig_SimpleFunction("__", function ($text, $filename = '', $context = '') use ($twig) {
if (!empty($filename)) {
if ($twig->getLoader()->exists($filename . '.twig')) {
$textdomain = $twig->getLoader()->getCacheKey($filename . '.twig');
$language = $this->user->language;
$out = htmlspecialchars($language->translator()->getTranslation($textdomain, $text, $context), ENT_QUOTES, 'UTF-8');
} else {
$out = $text;
}
return $out;
}
});
$twig->addFunction($function);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment