Translation functions

@param Twig $twig
355308
public function addTranslationFunctions($twig)
    {
        $twig->addFunction('_x', new Twig_SimpleFunction('_x',
            function ($text, $context, $domain = 'default') {
                return _x($text, $context, $domain);
            }
        ));

        $twig->addFunction('_n', new Twig_SimpleFunction('_n',
            function ($single, $plural, $number, $domain = 'default') {
                return _n($single, $plural, $number, $domain);
            }
        ));

        $twig->addFunction('_nx', new Twig_SimpleFunction('_nx',
            function ($single, $plural, $number, $context, $domain = 'default') {
                return _nx($single, $plural, $number, $context, $domain);
            }
        ));

        return $twig;
    }
1368965
public function loadAllTranslations($language)
    {
        foreach ($this->loaded as $module) {
            $this->loadModuleTranslation($module, $language);
        }
    }
1364594
private function disableUntranslatableForms(FormBuilderInterface $builder): void
    {
        foreach ($builder as $form) {
            /** @var \Symfony\Component\Form\FormBuilderInterface $form */
            $innerType = $form->getType()->getInnerType();
            $disabled = !$innerType instanceof ParametersType;

            $parameterDefinition = $form->getOption('ngbm_parameter_definition');
            if ($parameterDefinition instanceof ParameterDefinition) {
                $disabled = $parameterDefinition->getOption('translatable') !== true;
            }

            $form->setDisabled($disabled);

            if ($parameterDefinition instanceof ParameterDefinition) {
                continue;
            }

            $this->disableUntranslatableForms($form);
        }
    }
211790
public function transMultiple($messages, $parameters = [], $domain = null, $locale = null)
    {
        foreach ($messages as $message) {
            yield  $this->translator->trans($message, $parameters, $domain, $locale);
        }
    }
780071
protected function createTwigLintCommand() {
    $twig = new Environment(new FilesystemLoader());

    $repo_root = $this->getConfigValue('repo.root');
    $extension_file_contents = file_get_contents($repo_root . '/docroot/core/lib/Drupal/Core/Template/TwigExtension.php');

    // Get any custom defined Twig filters to be ignored by linter.
    $twig_filters = (array) $this->getConfigValue('validate.twig.filters');

    // Add Twig filters from Drupal TwigExtension to be ignored.
    $drupal_filters = [];
    if ($matches_count = preg_match_all("#new \\\\Twig_SimpleFilter\('([^']+)',#", $extension_file_contents, $matches)) {
      $drupal_filters = $matches[1];
    }
    $twig_filters = array_merge($twig_filters, $drupal_filters);
    foreach ($twig_filters as $filter) {
      // Add two default arguments and set to variadic so that filters with
      // named arguments will be whitelisted.
      // @see \TwigNode_Expression_Call::getArguments
      // @see \TwigNode_Expression_Call::call()
      $twig->addFilter(new \Twig_SimpleFilter($filter, function ($node = '', array $args = []) {}, [
        'is_variadic' => TRUE,
      ]));
    }

    // Get any custom defined Twig functions to be ignored by linter.
    $twig_functions = (array) $this->getConfigValue('validate.twig.functions');

    // Add Twig functions from Drupal TwigExtension to be ignored.
    $drupal_functions = [];
    if ($matches_count = preg_match_all("#new \\\\Twig_SimpleFunction\('([^']+)',#", $extension_file_contents, $matches)) {
      $drupal_functions = $matches[1];
    }
    $twig_functions = array_merge($twig_functions, $drupal_functions);
    foreach ($twig_functions as $function) {
      // Add default argument and set to variadic so that functions with named
      // arguments will be whitelisted.
      // @see \TwigNode_Expression_Call::getArguments
      // @see \TwigNode_Expression_Call::call()
      $twig->addFunction(new \Twig_SimpleFunction($function, function (array $args = []) {}, [
        'is_variadic' => TRUE,
      ]));
    }

    // Add Drupal Twig parser to include trans tag.
    $token_parser_filename = $repo_root . '/docroot/core/lib/Drupal/Core/Template/TwigTransTokenParser.php';
    if (file_exists($token_parser_filename)) {
      require_once $token_parser_filename;
      $twig->addTokenParser(new TwigTransTokenParser());
    }

    $command = new TwigLintCommand();
    $command->setTwigEnvironment($twig);

    return $command;
  }
1012703
public function compile(Compiler $compiler): void
    {
        $compiler
            ->raw('$this->env->getExtension(\'Fxp\Component\Mailer\Twig\Extension\TemplaterExtension\')')
            ->raw('->getTranslatedLayout(')
            ->subcompile($this->getAttribute('variables'))
            ->raw(')->getFile()')
        ;
    }
519976
public function translate($x, $y) 
    {
        $this->transform = $this->transform->translate($x, $y);
    }
635762
public function compile(Twig_Compiler $compiler)
    {
        $compiler
            ->addDebugInfo($this)
            // the (array) cast bypasses a PHP 5.2.6 bug
            ->write("\$context['_parent'] = (array) \$context;\n")
            ->write("\$context['_seq'] = twig_ensure_traversable(")
            ->subcompile($this->getNode('seq'))
            ->raw(");\n")
        ;

        if (null !== $this->getNode('else')) {
            $compiler->write("\$context['_iterated'] = false;\n");
        }

        if ($this->getAttribute('with_loop')) {
            $compiler
                ->write("\$context['loop'] = array(\n")
                ->write("  'parent' => \$context['_parent'],\n")
                ->write("  'index0' => 0,\n")
                ->write("  'index'  => 1,\n")
                ->write("  'first'  => true,\n")
                ->write(");\n")
            ;

            if (!$this->getAttribute('ifexpr')) {
                $compiler
                    ->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n")
                    ->indent()
                    ->write("\$length = count(\$context['_seq']);\n")
                    ->write("\$context['loop']['revindex0'] = \$length - 1;\n")
                    ->write("\$context['loop']['revindex'] = \$length;\n")
                    ->write("\$context['loop']['length'] = \$length;\n")
                    ->write("\$context['loop']['last'] = 1 === \$length;\n")
                    ->outdent()
                    ->write("}\n")
                ;
            }
        }

        $this->loop->setAttribute('else', null !== $this->getNode('else'));
        $this->loop->setAttribute('with_loop', $this->getAttribute('with_loop'));
        $this->loop->setAttribute('ifexpr', $this->getAttribute('ifexpr'));

        $compiler
            ->write("foreach (\$context['_seq'] as ")
            ->subcompile($this->getNode('key_target'))
            ->raw(" => ")
            ->subcompile($this->getNode('value_target'))
            ->raw(") {\n")
            ->indent()
            ->subcompile($this->getNode('body'))
            ->outdent()
            ->write("}\n")
        ;

        if (null !== $this->getNode('else')) {
            $compiler
                ->write("if (!\$context['_iterated']) {\n")
                ->indent()
                ->subcompile($this->getNode('else'))
                ->outdent()
                ->write("}\n")
            ;
        }

        $compiler->write("\$_parent = \$context['_parent'];\n");

        // remove some "private" loop variables (needed for nested loops)
        $compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'_parent\'], $context[\'loop\']);'."\n");

        // keep the values set in the inner context for variables defined in the outer context
        $compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n");
    }
462483
public function compile(Twig_Compiler $compiler)
    {
        $compiler
            ->addDebugInfo($this)
            ->write('$restoreStrict = $this->env->isStrictVariables();' . PHP_EOL)
            ->write('$setStrict = (bool)')
            ->subcompile($this->nodes['expr'])
            ->write(';');

        $compiler
            ->write('if ($setStrict) { ' . PHP_EOL)
            ->write('    $this->env->enableStrictVariables();' . PHP_EOL)
            ->write('} else {' . PHP_EOL)
            ->write('    $this->env->disableStrictVariables();' . PHP_EOL)
            ->write('}');

        $compiler->subcompile($this->nodes['body']);

        $compiler
            ->write('if ($restoreStrict) { ' . PHP_EOL)
            ->write('    $this->env->enableStrictVariables();' . PHP_EOL)
            ->write('} else {' . PHP_EOL)
            ->write('    $this->env->disableStrictVariables();' . PHP_EOL)
            ->write('}');
    }
325602
protected function initTwig( \Aimeos\MW\View\Iface $view, \Twig_Environment $twig )
	{
		$fcn = function( $key, $default = null ) use ( $view ) {
			return $view->config( $key, $default );
		};
		$twig->addFunction( new \Twig_SimpleFunction( 'aiconfig', $fcn ) );

		$fcn = function( $singular, array $values = array(), $domain = 'client' ) use ( $view ) {
			return vsprintf( $view->translate( $domain, $singular ), $values );
		};
		$twig->addFunction( new \Twig_SimpleFunction( 'aitrans', $fcn ) );

		$fcn = function( $singular, $plural, $number, array $values = array(), $domain = 'client' ) use ( $view ) {
			return vsprintf( $view->translate( $domain, $singular, $plural, $number ), $values );
		};
		$twig->addFunction( new \Twig_SimpleFunction( 'aitransplural', $fcn ) );
	}
1103783
public function compile(\Twig_Compiler $compiler) {

        $compiler->addDebugInfo($this)
            ->write("list(\$context['page'], ")
            ->subcompile($this->getNode('elementsTarget'))
            ->raw(') = \Disco\twig\PageNode::page(')
            ->subcompile($this->getNode('criteria'))
            ->raw(");\n")
            ->subcompile($this->getNode('body'), false)
            ->write('unset($context[\'page\'], ')
            ->subcompile($this->getNode('elementsTarget'))
            ->raw(");\n");

    }
455264
private static function extendTwig (\Twig_Environment $twig_Environment)
    {
        $twig_Environment->addFilter(new \Twig_SimpleFilter('__', function ($text, $domain = 'default') {
            return __($text, $domain);
        }));
        $twig_Environment->addExtension(new SlugifyExtension(Slugify::create()));
    }
122564
public function setTranslations(ArrayCollection $translations)
    {
        foreach ($translations as $translation) {
            $translation->setObject($this);
        }

        $this->translations = $translations;
    }
671204
public function compile(Twig_Compiler $compiler)
    {
        $prio = ($this->hasNode('prio')) ? $this->getNode('prio') : 0;

        $getExtension = sprintf('$this->env->getExtension(\'%s\')', ZichtFrameworkExtraExtension::class);

        $compiler->addDebugInfo($this);
        $compiler->write(sprintf('%s->getAnnotationRegistry()', $getExtension));

        if ($this->hasNode('name')) {
            $compiler->raw('->addAnnotation(')->subcompile($this->getNode('name'))->raw(', ');
        } else {
            $compiler->raw('->addAnnotations(');
        }

        $compiler->subcompile($this->getNode('expr'));

        if ($this->hasNode('prio')) {
            $compiler->raw(', ');
            $compiler->subcompile($prio);
        }

        $compiler->write(');' . "\n");
    }
349489
public function compile(Twig_Compiler $compiler)
    {
        $attrib = $this->getAttribute('name');

        $compiler
            ->write('$_type = isset($context["type"]) ? $context["type"] : null;')
            ->write('$_message = isset($context["message"]) ? $context["message"] : null;')
        ;

        if ($attrib == 'all') {
           $compiler
                ->addDebugInfo($this)
                ->write('foreach (Flash::getMessages() as $type => $messages) {'.PHP_EOL)
                ->indent()
                    ->write('foreach ($messages as $message) {'.PHP_EOL)
                    ->indent()
                        ->write('$context["type"] = $type;')
                        ->write('$context["message"] = $message;')
                        ->subcompile($this->getNode('body'))
                    ->outdent()
                    ->write('}'.PHP_EOL)
                ->outdent()
                ->write('}'.PHP_EOL)
            ;
        }
        else {
            $compiler
                ->addDebugInfo($this)
                ->write('$context["type"] = ')
                ->string($attrib)
                ->write(';')
                ->write('foreach (Flash::')
                ->raw($attrib)
                ->write('() as $message) {'.PHP_EOL)
                ->indent()
                    ->write('$context["message"] = $message;')
                    ->subcompile($this->getNode('body'))
                ->outdent()
                ->write('}'.PHP_EOL)
            ;
        }

        $compiler
            ->write('$context["type"] = $_type;')
            ->write('$context["message"] = $_message;')
        ;
    }
1163920
private function updateBlockTranslations(Block $block, array $translationsData): void
    {
        $mainLocale = $block->getMainLocale();

        foreach ($block->getAvailableLocales() as $locale) {
            if ($locale === $mainLocale) {
                continue;
            }

            if (!array_key_exists($locale, $translationsData)) {
                throw new RuntimeException(
                    sprintf('Could not find locale "%s" in the given block data', $locale)
                );
            }

            $this->updateBlockTranslation($block, $translationsData[$locale], $locale);
        }
    }
1328567
public function evaluateTwigFunc($context, $twig ) {

        $loader = new \Twig_Loader_Filesystem('.');
        $env = new \Twig_Environment($loader);

        $template = $env->createTemplate($twig);

        return $template->render($context);
    }