public function testIndexedArrayInterpolator() { $this->specify("Translator with IndexedArray interpolator", function () { $language = $this->config['ru']; $params = ['content' => $language, 'interpolator' => new IndexedArray()]; $translator = new PhTTranslateAdapterNativeArray($params); $expected = 'Привет, John D. Doe!'; $actual = $translator->_('Hello %s %s %s!', ['John', 'D.', 'Doe']); expect($actual)->equals($expected); }); }
/** * Returns the translation related to the given key * * @param string $index * @param array $placeholders * @return string */ public function query($index, $placeholders = null) { if (!empty($index) and !array_key_exists($index, $this->_translate)) { $transdir = \Phalcon\DI::getDefault()->getConfig()->dirs->translations; $new = [$index => $index . '*']; $this->_translate = $new + $this->_translate; $d = dir($transdir); while (($file = $d->read()) !== false) { if (!preg_match('/^[a-z]{2}\\.php$/', $file)) { continue; } $messages = []; $dict = $transdir . DIRECTORY_SEPARATOR . $file; require $dict; // Check if $index exists in given dictionary: if (!array_key_exists($index, $messages)) { $messages = $new + $messages; $content = sprintf("<?php\n\n// app/config/translations/%s\n\n\$messages = %s;", $file, var_export($messages, true)); file_put_contents($dict, $content); } } $d->close(); } return parent::query($index, $placeholders); }
/** * Render breadcrumb output based on previously set template * * @throws UnderflowException * * @access public * @return void * * @since 1.0 * @author Ole Aass <*****@*****.**> */ public function render() { try { if (empty($this->crumbs)) { throw new UnderflowException('Cannot render an empty array'); } $output = ''; foreach ($this->crumbs as $key => $crumb) { if ($crumb['linked']) { $output .= str_replace(['{{link}}', '{{label}}'], [$crumb['link'], is_null($this->translate) ? $crumb['label'] : $this->translate->_($crumb['label'])], $this->template['linked']); } else { $output .= str_replace('{{label}}', is_null($this->translate) ? $crumb['label'] : $this->translate->_($crumb['label']), $this->template['not-linked']); } $this->remove($key); $output .= !empty($this->crumbs) ? $this->separator : ''; } echo $output; } catch (UnderflowException $e) { $message = '[' . __METHOD__ . '] ' . $e->getMessage(); error_log($message); } }
public function __construct($file) { parent::__construct(['content' => include $file]); }
public function exists($index) { return parent::exists($index); }
/** * Tests variable substitution in string (two variable) - French * * @author Nikos Dimopoulos <*****@*****.**> * @since 2012-10-30 */ public function testVariableSubstitutionTwoFrench() { $language = $this->config['tr']['fr']; $params = array('content' => $language); $translator = new PhTranslateAdapterNativeArray($params); $vars = array('song' => 'Dust in the wind', 'artist' => 'Kansas'); $expected = 'La chanson est Dust in the wind (Kansas)'; $actual = $translator->_('song-key', $vars); $this->assertEquals($expected, $actual, 'Translator does not translate French correctly - many parameters'); }
/** * Tests translator with array access and UTF8 strings * * @author Nikos Dimopoulos <*****@*****.**> * @since 2014-09-12 */ public function testWithArrayAccessAndUTF8Strings() { $this->specify("Translator with array access and UTF8 strings", function () { $language = $this->config['ru']; $params = ['content' => $language]; $translator = new PhTTranslateAdapterNativeArray($params); $expected = 'Привет, John D. Doe!'; $actual = $translator->_('Hello %fname% %mname% %lname%!', ['fname' => 'John', 'lname' => 'Doe', 'mname' => 'D.']); expect($actual)->equals($expected); }); }