public static function ago($time, TranslatorInterface $translator) { $lengths = array("60", "60", "24", "7", "4.35", "12", "10"); $now = time(); $difference = $now - $time; $tense = $translator->translate("ago"); for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) { $difference /= $lengths[$j]; } $difference = round($difference); $periods = array($translator->translatePlural("second", "seconds", $difference), $translator->translatePlural("minute", "minutes", $difference), $translator->translatePlural("hour", "hours", $difference), $translator->translatePlural("day", "days", $difference), $translator->translatePlural("week", "weeks", $difference), $translator->translatePlural("month", "months", $difference), $translator->translatePlural("year", "years", $difference), $translator->translatePlural("decade", "decades", $difference)); return "{$difference} {$periods[$j]} {$tense}"; }
/** * @param string|null $locale * @param string|null $domain * @param string $id * @param string $translation * @param array|null $parameters * @param int|null $number */ private function collectMessage($locale, $domain, $id, $translation, $parameters = array(), $number = null) { if (null === $domain) { $domain = 'messages'; } $id = (string) $id; $catalogue = $this->translator->getCatalogue($locale); $locale = $catalogue->getLocale(); if ($catalogue->defines($id, $domain)) { $state = self::MESSAGE_DEFINED; } elseif ($catalogue->has($id, $domain)) { $state = self::MESSAGE_EQUALS_FALLBACK; $fallbackCatalogue = $catalogue->getFallbackCatalogue(); while ($fallbackCatalogue) { if ($fallbackCatalogue->defines($id, $domain)) { $locale = $fallbackCatalogue->getLocale(); break; } $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue(); } } else { $state = self::MESSAGE_MISSING; } $this->messages[] = array('locale' => $locale, 'domain' => $domain, 'id' => $id, 'translation' => $translation, 'parameters' => $parameters, 'transChoiceNumber' => $number, 'state' => $state); }
/** * Intialize the translator instance if necessary. * * @return TranslatorInterface */ protected static function translator() { if (static::$translator == null) { static::$translator = new Translator('en'); static::$translator->addLoader('array', new ArrayLoader()); static::setLocale('en'); } return static::$translator; }
/** * Translate the given text based on the given number. * * @param string $textSingular The text in its singular form. * @param string $textPlural The text in its plural form. * @param int $number The number. * @param array|null $replacements Values that should be replaced. * @param string $locale The locale or null to use the current locale. * * @return string Translated text or given text if no translation found. */ public function _n($textSingular, $textPlural, $number, $replacements = null, $locale = null) { if (!$locale) { $locale = $this->getLocale(); } if ($this->translator !== null) { $text = $this->translator->translatePlurals($textSingular, $textPlural, $number, $locale); } else { $text = $number == 1 ? $textSingular : $textPlural; } return $this->replace($text, $replacements); }
/** * * Gets the message translation by its key. * * @param string $key The message key. * * @return mixed The message translation string, or false if not found. * */ protected function getMessage($key) { if (isset($this->messages[$key])) { return $this->messages[$key]; } if ($this->fallback) { // get the message from the fallback translator $message = $this->fallback->getMessage($key); // speed optimization: retain locally $this->messages[$key] = $message; // done! return $message; } // no local message, no fallback return false; }
/** * {@inheritdoc} */ public function getCatalogue($locale = null) { return $this->translator->getCatalogue($locale); }
public function translate($word, $toLang, $fromLang = null) { return $this->translator->translate($word, $toLang, $fromLang); }
protected function setUp() { $this->translator = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface'); $this->translator->expects($this->any())->method('trans'); $this->listener = new GmailOAuthSubscriber($this->translator); }