|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright (C) 2018-2020 raphael.droz@gmail.com |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + */ |
| 10 | + |
| 11 | +declare(strict_types = 1); |
| 12 | + |
| 13 | +namespace Gettext\Scanner; |
| 14 | + |
| 15 | +use Gettext\Translation; |
| 16 | +use Timber\Twig; |
| 17 | +use Twig\Environment; |
| 18 | + |
| 19 | +/** |
| 20 | + * Class to scan Twig files and get gettext translations |
| 21 | + */ |
| 22 | +class TimberScanner extends TwigScanner |
| 23 | +{ |
| 24 | + protected $functions = [ |
| 25 | + '__' => 'text', |
| 26 | + '_e' => 'text', |
| 27 | + '_x' => 'text_context', |
| 28 | + '_ex' => 'text_context', |
| 29 | + '_n' => 'single_plural_number', |
| 30 | + '_nx' => 'single_plural_number_context', |
| 31 | + '_n_noop' => 'single_plural', |
| 32 | + '_nx_noop' => 'single_plural_context', |
| 33 | + ]; |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns a new Twig environment i18n-enabled with Timber. |
| 37 | + */ |
| 38 | + public static function createTwig(): Environment |
| 39 | + { |
| 40 | + $twig = parent::createTwig(); |
| 41 | + |
| 42 | + $timber = new Twig(); |
| 43 | + $timber->add_timber_functions($twig); |
| 44 | + $timber->add_timber_filters($twig); |
| 45 | + |
| 46 | + return $twig; |
| 47 | + } |
| 48 | + |
| 49 | + protected function text(ParsedFunction $function): ?Translation |
| 50 | + { |
| 51 | + list($original, $domain) = array_pad($function->getArguments(), 2, null); |
| 52 | + |
| 53 | + return $this->addComments( |
| 54 | + $function, |
| 55 | + $this->saveTranslation($domain, null, $original) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + protected function text_context(ParsedFunction $function): ?Translation |
| 60 | + { |
| 61 | + list($original, $context, $domain) = array_pad($function->getArguments(), 3, null); |
| 62 | + |
| 63 | + return $this->addComments( |
| 64 | + $function, |
| 65 | + $this->saveTranslation($domain, $context, $original) |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + protected function single_plural_number(ParsedFunction $function): ?Translation |
| 70 | + { |
| 71 | + list($original, $plural, $number, $domain) = array_pad($function->getArguments(), 4, null); |
| 72 | + |
| 73 | + return $this->addComments( |
| 74 | + $function, |
| 75 | + $this->saveTranslation($domain, null, $original, $plural) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + protected function single_plural_number_context(ParsedFunction $function): ?Translation |
| 80 | + { |
| 81 | + list($original, $plural, $number, $context, $domain) = array_pad($function->getArguments(), 5, null); |
| 82 | + |
| 83 | + return $this->addComments( |
| 84 | + $function, |
| 85 | + $this->saveTranslation($domain, $context, $original, $plural) |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + protected function single_plural(ParsedFunction $function): ?Translation |
| 90 | + { |
| 91 | + list($original, $plural, $domain) = array_pad($function->getArguments(), 3, null); |
| 92 | + |
| 93 | + return $this->addComments( |
| 94 | + $function, |
| 95 | + $this->saveTranslation($domain, null, $original, $plural) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + protected function single_plural_context(ParsedFunction $function): ?Translation |
| 100 | + { |
| 101 | + list($original, $plural, $context, $domain) = array_pad($function->getArguments(), 4, null); |
| 102 | + |
| 103 | + return $this->addComments( |
| 104 | + $function, |
| 105 | + $this->saveTranslation($domain, $context, $original, $plural) |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
0 commit comments