/** * {@inheritdoc} */ public function transChoice(string $id, $number, array $parameters = [], string $domain = 'messages') : string { if (is_array($number) || $number instanceof Countable) { $number = count($number); } if (preg_match("/^(.*?)\\[(.*?)\\]\$/", $id, $match)) { $id = $match[1]; } $trans = strtr($this->selector->choose($this->catalogue->get($id, $domain), $number, $this->locale), $parameters); $trans = $this->applyFilters($trans); $trans = $this->applyHelpers(empty($match) ? $trans : $trans . '[' . $match[2] . ']'); if ($this->logger !== null) { $this->log($id, $domain); } return $trans; }
/** * Add message catalogue. * * @param \Viserio\Contracts\Translation\MessageCatalogue $messageCatalogue * * @return $this */ public function addMessageCatalogue(MessageCatalogueContract $messageCatalogue) : TranslationManager { $locale = $messageCatalogue->getLocale(); if ($fallback = $this->getLanguageFallback($messageCatalogue->getLocale())) { $messageCatalogue->addFallbackCatalogue($fallback); } elseif ($fallback = $this->defaultFallback) { $messageCatalogue->addFallbackCatalogue($fallback); } $translation = new Translator($messageCatalogue, $this->messageSelector); if ($this->logger !== null) { $translation->setLogger($this->logger); } $this->translations[$locale] = $translation; return $this; }
/** * {@inheritdoc} */ public function addFallbackCatalogue(MessageCatalogueContract $catalogue) { // detect circular references $circular = $this; do { if ($circular->getLocale() === $catalogue->getLocale()) { throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale())); } } while ($circular = $circular->parent); $catalogue->setParent($this); $this->fallbackCatalogue = $catalogue; }