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());
 }
 /**
  * @param string $from_file The file to read and parse
  * @param \I18n\I18n $i18n The I18n instance
  * @return array\null
  */
 public function generate($from_file, \I18n\I18n $i18n)
 {
     $default = $i18n->getLoader()->getOption('default_language', 'en');
     $all_lang_strings = $headers = array();
     if (false !== ($handle = fopen($from_file, 'r'))) {
         $line_count = 0;
         while (false !== ($data = fgetcsv($handle, 0, ';', '"'))) {
             if ($line_count === 0) {
                 for ($c = 0; $c < count($data); $c++) {
                     $headers[$c] = $data[$c];
                     if ($c !== 0) {
                         $all_lang_strings[$data[$c]] = array();
                     }
                     if ($data[$c] === $default) {
                         $default_index = $c;
                     }
                 }
             } else {
                 $index = $data[0];
                 if (empty($index)) {
                     $index = isset($data[$default_index]) ? $data[$default_index] : uniqid();
                 }
                 for ($d = 1; $d < count($data); $d++) {
                     $lang = $headers[$d];
                     if (!isset($all_lang_strings[$lang][$index])) {
                         $all_lang_strings[$lang][$index] = $data[$d];
                     } else {
                         trigger_error(sprintf('Language string with index "%s" defined more than one time!', $index), E_USER_NOTICE);
                     }
                 }
             }
             $line_count++;
         }
         fclose($handle);
     }
     return $all_lang_strings;
 }
 /**
  * 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;
 }
 /**
  * EMailField constructor
  *
  * @param $args array Field arguments
  */
 function __construct(array $args = null)
 {
     $this->pattern = '/^([a-zA-Z0-9_\\.\\-]+)@([a-zA-Z0-9_\\.\\-]+)\\.([a-zA-Z0-9\\-]{2,8})$/';
     if (isset($args['label'])) {
         $this->setLabel($args['label']);
     }
     if (isset($args['size'])) {
         $this->setSize($this->setSize($args['size']));
     }
     if (isset($args['blank'])) {
         $this->setEmpty($args['blank']);
     }
     if (isset($args['value'])) {
         $this->setValue($args['value']);
     }
     $this->blankMessage = I18n::_('This field can\'t be empty');
     $this->formatMessage = I18n::_('Invalid email address');
 }
 /**
  * PasswordField constructor
  *
  * @param $args array Array that contains the arguments of the field
  */
 function __construct(array $args = null)
 {
     $this->pattern = '/^.*$/';
     if (isset($args['label'])) {
         $this->setLabel($args['label']);
     }
     if (isset($args['size'])) {
         $this->setSize($this->setSize($args['size']));
     }
     if (isset($args['blank'])) {
         $this->setEmpty($args['blank']);
     }
     if (isset($args['value'])) {
         $this->setValue($args['value']);
     }
     $this->blankMessage = I18n::_('This field can\'t be empty');
     $this->formatMessage = I18n::_('Invalid value');
 }
 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());
 }
 /**
  * Starts the framework
  *
  * @param string|null $settings Path to settings file
  * @return int If all is well then it returns zero
  * @throws \Exception
  */
 public static function run(string $settings = null) : int
 {
     $settingsFile = $settings ? $settings : realpath(dirname(__FILE__) . '/../../../' . 'settings.yml');
     # load settings
     if (!Settings::load($settingsFile)) {
         return false;
     }
     if (Settings::getProduction()) {
         ini_set('display_errors', 'Off');
         ini_set('error_log', '/tmp/php-error.log');
         ini_set('log_errors', 1);
     }
     # set timezone
     Timezone::setTimezone(Settings::getTimezone());
     # set locale language
     I18n::setLocale(I18n::getLanguage());
     # generate routes
     if (Settings::getAutoroute()) {
         Routing::generateRoutes();
     }
     # load routes
     $routesFile = Settings::getRoutesFile();
     if (file_exists($routesFile)) {
         self::$routes = YAML::load($routesFile);
     } else {
         printf('[Error] Routes file %s does not exists', $routesFile);
         return false;
     }
     $dbSettings = Settings::getDBSettings();
     $db = new DB($dbSettings['dbm'], $dbSettings['host'], $dbSettings['name'], $dbSettings['user'], $dbSettings['password']);
     DB::setGlobal($db);
     # load actions
     Actions::autoloader(Settings::getActionsPath());
     Session::start();
     $request = isset($_GET[Settings::SETTINGS_ROUTE_PARAM_VALUE]) ? $_GET[Settings::SETTINGS_ROUTE_PARAM_VALUE] : '';
     if (Routing::parseRoute($request, self::$routes) == -1) {
         Response::response(Response::HTTP_FORBIDDEN);
     }
     return 0;
 }
<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();
Beispiel #9
0
    /**
     * Return the form for comments
     *
     * @return string
     */
    public function getFormComments()
    {
        ob_start();
        $placeholderTextarea = I18n::transu('post.share_comment', []);
        $params = ['comment_notes_after' => '', 'author' => '<p class="comment-form-author">' . '<label for="author">' . __('Your Name') . '</label>
					<input id="author" name="author" type="text"  value="Your First and Last Name" size="30" /></p>', 'comment_field' => '
				<div class="form-group comment-form-comment">
		            <label for="comment">' . _x('Comment', 'noun') . '</label>
		            <textarea class="form-control" id="comment" name="comment" cols="45" rows="2"
							maxlength="1000" aria-required="true" placeholder="' . $placeholderTextarea . '"></textarea>
		        </div>'];
        comment_form($params, $this->ID);
        $comment_form = ob_get_clean();
        $comment_form = str_replace('id="submit"', 'class="btn btn-default"', $comment_form);
        return $comment_form;
    }
 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;
 }
 /**
  * Get a localized price value
  * @see     \I18n\I18n::getLocalizedPriceString()
  */
 function currencify($number, $lang = null)
 {
     return \I18n\I18n::getLocalizedPriceString($number, $lang);
 }