Files
piratepoet/lib/Twig/I18n.php
2019-03-07 08:30:14 -05:00

105 lines
2.5 KiB
PHP

<?php
namespace MailPoet\Twig;
use MailPoet\Config\Localizer;
use MailPoet\WP\Functions as WPFunctions;
if (!defined('ABSPATH')) exit;
class I18n extends \Twig_Extension {
private $_text_domain;
function __construct($text_domain) {
// set text domain
$this->_text_domain = $text_domain;
}
function getFunctions() {
// twig custom functions
$twig_functions = array();
// list of WP functions to map
$functions = array(
'localize' => 'localize',
'__' => 'translate',
'_n' => 'pluralize',
'_x' => 'translateWithContext',
'get_locale' => 'getLocale',
'date' => 'date'
);
foreach ($functions as $twig_function => $function) {
$twig_functions[] = new \Twig_SimpleFunction(
$twig_function,
array($this, $function),
array('is_safe' => array('all'))
);
}
return $twig_functions;
}
function localize() {
$args = func_get_args();
$translations = array_shift($args);
$output = array();
$output[] = '<script type="text/javascript">';
foreach ($translations as $key => $translation) {
$output[] =
'MailPoet.I18n.add("'.$key.'", "'. str_replace('"', '\"', $translation) . '");';
}
$output[] = '</script>';
return join("\n", $output);
}
function translate() {
$args = func_get_args();
return call_user_func_array('__', $this->setTextDomain($args));
}
function pluralize() {
$args = func_get_args();
return call_user_func_array('_n', $this->setTextDomain($args));
}
function translateWithContext() {
$args = func_get_args();
return call_user_func_array('_x', $this->setTextDomain($args));
}
function getLocale() {
$localizer = new Localizer;
return $localizer->locale();
}
function date() {
$args = func_get_args();
$date = (isset($args[0])) ? $args[0] : null;
$date_format = (isset($args[1])) ? $args[1] : WPFunctions::get()->getOption('date_format');
if (empty($date)) return;
// check if it's an int passed as a string
if ((string)(int)$date === $date) {
$date = (int)$date;
} else if (!is_int($date)) {
$date = strtotime($date);
}
return WPFunctions::get()->getDateFromGmt(date('Y-m-d H:i:s', $date), $date_format);
}
private function setTextDomain($args = array()) {
// make sure that the last argument is our text domain
if ($args[count($args) - 1] !== $this->_text_domain) {
// otherwise add it to the list of arguments
$args[] = $this->_text_domain;
}
return $args;
}
}