Analytics.php
2 years ago
Assets.php
1 year ago
Filters.php
9 months ago
Functions.php
3 months ago
Handlebars.php
2 years ago
I18n.php
2 months ago
index.php
3 years ago
I18n.php
123 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Twig; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Localizer; |
| 9 | use MailPoet\WP\Functions as WPFunctions; |
| 10 | use MailPoetVendor\Twig\Extension\AbstractExtension; |
| 11 | use MailPoetVendor\Twig\TwigFunction; |
| 12 | |
| 13 | class I18n extends AbstractExtension { |
| 14 | |
| 15 | private $textDomains; |
| 16 | |
| 17 | public function __construct( |
| 18 | $textDomain |
| 19 | ) { |
| 20 | // set text domain |
| 21 | $this->textDomains = [$textDomain, 'woocommerce']; |
| 22 | } |
| 23 | |
| 24 | public function getFunctions() { |
| 25 | // twig custom functions |
| 26 | $twigFunctions = []; |
| 27 | // list of WP functions to map |
| 28 | $functions = [ |
| 29 | 'localize' => 'localize', |
| 30 | '__' => 'translate', |
| 31 | 'esc_html__' => 'translateEscHTML', |
| 32 | 'esc_attr__' => 'translateEscAttr', |
| 33 | '_n' => 'pluralize', |
| 34 | '_x' => 'translateWithContext', |
| 35 | 'get_locale' => 'getLocale', |
| 36 | 'date' => 'date', |
| 37 | ]; |
| 38 | |
| 39 | foreach ($functions as $twigFunction => $function) { |
| 40 | $twigFunctions[] = new TwigFunction( |
| 41 | $twigFunction, |
| 42 | [$this, $function], |
| 43 | ['is_safe' => ['all']] |
| 44 | ); |
| 45 | } |
| 46 | return $twigFunctions; |
| 47 | } |
| 48 | |
| 49 | public function localize() { |
| 50 | $args = func_get_args(); |
| 51 | /** @var array $translations */ |
| 52 | $translations = array_shift($args); |
| 53 | $output = []; |
| 54 | foreach ($translations as $key => $translation) { |
| 55 | $output[] = |
| 56 | 'MailPoet.I18n.add("' . $key . '", "' . str_replace(['"', "\n", "\r"], ['\"', " ", ""], $translation ?? '') . '");'; |
| 57 | } |
| 58 | WPFunctions::get()->wpAddInlineScript('mailpoet_mailpoet', join("\n", $output)); |
| 59 | } |
| 60 | |
| 61 | public function translate() { |
| 62 | $args = func_get_args(); |
| 63 | |
| 64 | return call_user_func_array('__', $this->setTextDomain($args)); |
| 65 | } |
| 66 | |
| 67 | public function translateEscHTML() { |
| 68 | $args = func_get_args(); |
| 69 | |
| 70 | return call_user_func_array('esc_html__', $this->setTextDomain($args)); |
| 71 | } |
| 72 | |
| 73 | public function translateEscAttr() { |
| 74 | $args = func_get_args(); |
| 75 | |
| 76 | return call_user_func_array('esc_attr__', $this->setTextDomain($args)); |
| 77 | } |
| 78 | |
| 79 | public function pluralize() { |
| 80 | $args = func_get_args(); |
| 81 | |
| 82 | return call_user_func_array('_n', $this->setTextDomain($args)); |
| 83 | } |
| 84 | |
| 85 | public function translateWithContext() { |
| 86 | $args = func_get_args(); |
| 87 | |
| 88 | return call_user_func_array('_x', $this->setTextDomain($args)); |
| 89 | } |
| 90 | |
| 91 | public function getLocale() { |
| 92 | $localizer = new Localizer; |
| 93 | return $localizer->locale(); |
| 94 | } |
| 95 | |
| 96 | public function date() { |
| 97 | $args = func_get_args(); |
| 98 | /** @var int|null $date */ |
| 99 | $date = (isset($args[0])) ? $args[0] : null; |
| 100 | $dateFormat = (isset($args[1])) ? $args[1] : WPFunctions::get()->getOption('date_format'); |
| 101 | |
| 102 | if (empty($date)) return; |
| 103 | |
| 104 | // check if it's an int passed as a string |
| 105 | if ((string)(int)$date === $date) { |
| 106 | $date = (int)$date; |
| 107 | } else if (!is_int($date)) { |
| 108 | $date = strtotime($date); |
| 109 | } |
| 110 | |
| 111 | return WPFunctions::get()->getDateFromGmt(date('Y-m-d H:i:s', (int)$date), $dateFormat); |
| 112 | } |
| 113 | |
| 114 | private function setTextDomain($args = []) { |
| 115 | // make sure that the last argument is our text domain |
| 116 | if (!in_array($args[count($args) - 1], $this->textDomains)) { |
| 117 | // otherwise add it to the list of arguments |
| 118 | $args[] = $this->textDomains[0]; |
| 119 | } |
| 120 | return $args; |
| 121 | } |
| 122 | } |
| 123 |