public function parse(Twig_Token $token) { $i18n = I18n::getInstance(); $lineno = $token->getLine(); $stream = $this->parser->getStream(); $choices = $counter = $arguments = $lang = null; $args = array(); if (!$stream->test(Twig_Token::BLOCK_END_TYPE)) { while (!$stream->test(Twig_Token::BLOCK_END_TYPE)) { $args[] = $this->parser->getExpressionParser()->parsePrimaryExpression(); } foreach ($args as $i => $node) { if ($node instanceof Twig_Node_Expression_Array) { $arguments = $node; } elseif ($node instanceof Twig_Node_Expression_Constant) { if (is_numeric($node->getAttribute('value'))) { $counter = $node; } elseif ($i18n->isAvailableLanguage($node->getAttribute('value'))) { $lang = $node; } else { $body = $node; } } else { throw new Twig_Error_Syntax(sprintf('Unexpected argument of type "%s" to the "translate" tag.', get_class($node)), $stream->getCurrent()->getLine(), $stream->getFilename()); } } if (empty($body)) { $stream->expect(Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'isEndTag'), true); } } else { $stream->expect(Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'isEndTag'), true); } if (empty($choices)) { $value = $body->hasAttribute('value') ? $body->getAttribute('value') : $body->getAttribute('data'); $value = trim($value, "\n"); $choices_values = explode('|', $value); $choices_table = array(); foreach ($choices_values as $i => $val) { $choices_table[] = new Twig_Node_Expression_Constant($i, $lineno); $choices_table[] = new Twig_Node_Expression_Constant(trim($val), $lineno); } $choices = new Twig_Node_Expression_Array($choices_table, $lineno); } if (empty($lang)) { $lang = new Twig_Node_Expression_Constant($i18n->getLanguage(), $lineno); } if (empty($arguments)) { $arguments = new Twig_Node_Expression_Array(array(), $lineno); } $stream->expect(Twig_Token::BLOCK_END_TYPE); /* var_export($choices); var_export($counter); var_export($arguments); exit('yo'); */ return new PluralizeNode($choices, $counter, $arguments, $lang, $lineno, $this->getTag()); }
/** * Parse an option value replacing `%s` by the actual language code * * @param string $name The option name * @param string|array $lang The language code to use or an array to pass to * the 'sprintf()' method * @param mixed $default The value to return if the option can't be found * @return mixed The value of the option if found, with replacement if so */ public function getParsedOption($name, $lang = null, $default = null) { $val = $this->getOption($name, $default); if (false !== strpos($val, '%s')) { if (is_null($lang)) { $i18n = I18n::getInstance(); $lang = $i18n->getLanguage(); } if (is_array($lang)) { array_unshift($lang, $val); $val = call_user_func_array('sprintf', $lang); } else { $val = sprintf($val, $lang); } } return $val; }
public function parse(Twig_Token $token) { $i18n = I18n::getInstance(); $lineno = $token->getLine(); $stream = $this->parser->getStream(); $body = $arguments = $lang = null; $args = array(); if (!$stream->test(Twig_Token::BLOCK_END_TYPE)) { while (!$stream->test(Twig_Token::BLOCK_END_TYPE)) { $args[] = $this->parser->getExpressionParser()->parsePrimaryExpression(); } foreach ($args as $i => $node) { if ($node instanceof Twig_Node_Expression_Array) { $arguments = $node; } elseif ($node instanceof Twig_Node_Expression_Constant) { if ($i18n->isAvailableLanguage($node->getAttribute('value'))) { $lang = $node; } else { $body = $node; } } else { throw new Twig_Error_Syntax(sprintf('Unexpected argument of type "%s" to the "translate" tag.', get_class($node)), $stream->getCurrent()->getLine(), $stream->getFilename()); } } if (empty($body)) { $stream->expect(Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'isEndTag'), true); } } else { $stream->expect(Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'isEndTag'), true); } if (empty($lang)) { $lang = new Twig_Node_Expression_Constant($i18n->getLanguage(), $lineno); } if (empty($arguments)) { $arguments = new Twig_Node_Expression_Array(array(), $lineno); } $stream->expect(Twig_Token::BLOCK_END_TYPE); return new TranslateNode($body, $arguments, $lang, $lineno, $this->getTag()); }
<p>First, you need to setup your I18n instance:</p> <pre class="code" data-language="php"> <?php echo 'require_once "vendor/autoload.php";'; echo "\n\n\tor\n\n"; echo 'require_once ".../src/SplClassLoader.php"; // if required, a copy is proposed in the package' . "\n"; echo '$classLoader = new SplClassLoader("I18n", "/path/to/package/src");' . "\n"; echo '$classLoader->register();'; echo "\n\n"; echo '$i18n_loader = new \\I18n\\Loader(array(' . "\n" . "\t" . '"language_strings_db_directory" => __DIR__."/i18n",' . "\n" . "\t" . '"language_directory" => __DIR__."/i18n",' . "\n" . "\t" . '"force_rebuild" => true,' . "\n" . '));' . "\n"; echo '$translator = \\I18n\\I18n::getInstance($i18n_loader);' . "\n"; require_once '../vendor/autoload.php'; $i18n_loader = new \I18n\Loader(array('language_strings_db_directory' => __DIR__ . '/i18n', 'language_directory' => __DIR__ . '/i18n/%s', 'force_rebuild' => true)); $translator = \I18n\I18n::getInstance($i18n_loader); ?> </pre> <p>Then, you need to add the I18n extension:</p> <pre class="code" data-language="php"> <?php if (!class_exists('Twig_Loader_String')) { echo 'You need to include Twig in your autoloader to visualize the tests ...' . "\n\n"; echo 'You can run:' . "\n\t" . '~$ composer update --dev'; } echo '$loader = new Twig_Loader_String();' . "\n"; echo '$twig = new Twig_Environment($loader);' . "\n"; echo '$twig->addExtension(new \\I18n\\Twig\\Extension($translator));' . "\n"; $loader = new Twig_Loader_String();
public function getGlobals() { return array('i18n' => I18n::getInstance()); }
public function getMarkdownParser() { if (empty($this->markdown_parser)) { // creating the Markdown parser $emd_config = $this->registry->getConfig('markdown', array(), 'docbook'); $emd_config_strs = $this->registry->getConfig('markdown_i18n', array(), 'docbook'); if (!empty($emd_config_strs) && is_array($emd_config_strs) && count($emd_config_strs) == 1 && isset($emd_config_strs['markdown_i18n'])) { $emd_config_strs = $emd_config_strs['markdown_i18n']; } if (empty($emd_config)) { $emd_config = array(); } $translator = I18n::getInstance(); foreach ($emd_config_strs as $_str) { $emd_config[$_str] = $translator->translate($_str); } $this->setMarkdownParser(MarkdownExtended::create($emd_config)); } return $this->markdown_parser; }