Aucune description

I18n.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace MailPoet\Twig;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Config\Localizer;
  5. use MailPoet\InvalidStateException;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. use MailPoetVendor\Twig\Extension\AbstractExtension;
  8. use MailPoetVendor\Twig\TwigFunction;
  9. class I18n extends AbstractExtension {
  10. private $textDomains;
  11. public function __construct(
  12. $textDomain
  13. ) {
  14. // set text domain
  15. $this->textDomains = [$textDomain, 'woocommerce'];
  16. }
  17. public function getFunctions() {
  18. // twig custom functions
  19. $twigFunctions = [];
  20. // list of WP functions to map
  21. $functions = [
  22. 'localize' => 'localize',
  23. '__' => 'translate',
  24. '_n' => 'pluralize',
  25. '_x' => 'translateWithContext',
  26. 'get_locale' => 'getLocale',
  27. 'date' => 'date',
  28. ];
  29. foreach ($functions as $twigFunction => $function) {
  30. $callable = [$this, $function];
  31. if (!is_callable($callable)) {
  32. throw new InvalidStateException('Trying to register non-existing function to Twig.');
  33. }
  34. $twigFunctions[] = new TwigFunction(
  35. $twigFunction,
  36. $callable,
  37. ['is_safe' => ['all']]
  38. );
  39. }
  40. return $twigFunctions;
  41. }
  42. public function localize() {
  43. $args = func_get_args();
  44. $translations = array_shift($args);
  45. $output = [];
  46. $output[] = '<script type="text/javascript">';
  47. foreach ($translations as $key => $translation) {
  48. $output[] =
  49. 'MailPoet.I18n.add("' . $key . '", "' . str_replace('"', '\"', $translation) . '");';
  50. }
  51. $output[] = '</script>';
  52. return join("\n", $output);
  53. }
  54. public function translate() {
  55. $args = func_get_args();
  56. return call_user_func_array('__', $this->setTextDomain($args));
  57. }
  58. public function pluralize() {
  59. $args = func_get_args();
  60. return call_user_func_array('_n', $this->setTextDomain($args));
  61. }
  62. public function translateWithContext() {
  63. $args = func_get_args();
  64. return call_user_func_array('_x', $this->setTextDomain($args));
  65. }
  66. public function getLocale() {
  67. $localizer = new Localizer;
  68. return $localizer->locale();
  69. }
  70. public function date() {
  71. $args = func_get_args();
  72. $date = (isset($args[0])) ? $args[0] : null;
  73. $dateFormat = (isset($args[1])) ? $args[1] : WPFunctions::get()->getOption('date_format');
  74. if (empty($date)) return;
  75. // check if it's an int passed as a string
  76. if ((string)(int)$date === $date) {
  77. $date = (int)$date;
  78. } else if (!is_int($date)) {
  79. $date = strtotime($date);
  80. }
  81. return WPFunctions::get()->getDateFromGmt(date('Y-m-d H:i:s', (int)$date), $dateFormat);
  82. }
  83. private function setTextDomain($args = []) {
  84. // make sure that the last argument is our text domain
  85. if (!in_array($args[count($args) - 1], $this->textDomains)) {
  86. // otherwise add it to the list of arguments
  87. $args[] = $this->textDomains[0];
  88. }
  89. return $args;
  90. }
  91. }