/** * * Translates the message indicated by they key, replacing token values * along the way. * * @param string $key The message key. * * @param array $tokens_values Token values to interpolate into the * message. * * @return string The translated message with tokens replaced. * */ public function translate($key, array $tokens_values = []) { // get the message string $message = $this->getMessage($key); // do we have a message string? if (!$message) { // no, return the message key as-is $message = $key; } // are there token replacement values? if (!$tokens_values) { // no, return the message string as-is return $message; } // run message string through formatter to replace tokens with values return $this->formatter->format($this->locale, $message, $tokens_values); }
/** * * Gets a translator from the registry by package for a locale. * * @param string $name The translator package to retrieve. * * @param string $locale The locale to use; if empty, uses the default * locale. * * @return TranslatorInterface A translator object. * */ public function get($name, $locale = null) { if (!$name) { return null; } if (!$locale) { $locale = $this->getLocale(); } if (!isset($this->registry[$name][$locale])) { // get the package descriptor $package = $this->packages->get($name, $locale); // build a translator; note the recursive nature of the // 'fallback' param at the very end. $translator = $this->factory->newInstance($locale, $package->getMessages(), $this->formatters->get($package->getFormatter()), $this->get($package->getFallback(), $locale)); // retain in the registry $this->registry[$name][$locale] = $translator; } return $this->registry[$name][$locale]; }
/** * @param bool $millisecondPrecision * @return string */ public function getDurationFormatted($millisecondPrecision = true) { $duration = $this->getDuration(); return $this->formatter->format($duration, $millisecondPrecision); }
/** * @param int $expected * @param int $size * * @dataProvider dataProviderForMemory */ public function testMemory($expected, $size) { $this->assertEquals($expected, $this->formatter->memory($size)); }