/** * {@inheritdoc} * * @api */ public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null) { if (null === $locale) { $locale = $this->getLocale(); } if (null === $domain) { $domain = 'messages'; } elseif (in_array($domain, $this->container->getParameter('victoire_i18n.users_locale.domains'))) { $locale = $this->getVictoireLocale(); } if (!isset($this->catalogues[$locale])) { $this->loadCatalogue($locale); } $id = (string) $id; $catalogue = $this->catalogues[$locale]; while (!$catalogue->defines($id, $domain)) { if ($cat = $catalogue->getFallbackCatalogue()) { $catalogue = $cat; $locale = $catalogue->getLocale(); } else { break; } } return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters); }
/** * {@inheritdoc} * * @api */ public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) { if (null === $locale) { $locale = $this->getLocale(); } else { $this->assertValidLocale($locale); } if (null === $domain) { $domain = 'messages'; } if (!isset($this->catalogues[$locale])) { $this->loadCatalogue($locale); } $id = (string) $id; $catalogue = $this->catalogues[$locale]; while (!$catalogue->defines($id, $domain)) { if ($cat = $catalogue->getFallbackCatalogue()) { $catalogue = $cat; $locale = $catalogue->getLocale(); } else { break; } } return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters); }
/** * {@inheritdoc} * * Default symfony pluralizer to be used. Parameters will be embedded into string using { and } * braces. In addition you can use forced parameter {n} which contain formatted number value. * * @throws LocaleException */ public function transChoice($id, $number, array $parameters = [], $domain = self::DEFAULT_DOMAIN, $locale = null) { if (empty($parameters['{n}'])) { $parameters['{n}'] = number_format($number); } //Automatically falls back to default locale $translation = $this->get($domain, $id, $locale); try { $pluralized = $this->selector->choose($translation, $number, $locale); } catch (\InvalidArgumentException $exception) { //Wrapping into more explanatory exception throw new PluralizationException($exception->getMessage(), $exception->getCode(), $exception); } return \Spiral\interpolate($pluralized, $parameters); }
/** * {@inheritdoc} */ public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) { $parameters = array_merge(array('%count%' => $number), $parameters); if (null === $domain) { $domain = 'messages'; } $id = (string) $id; $catalogue = $this->getCatalogue($locale); $locale = $catalogue->getLocale(); while (!$catalogue->defines($id, $domain)) { if ($cat = $catalogue->getFallbackCatalogue()) { $catalogue = $cat; $locale = $catalogue->getLocale(); } else { break; } } return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters); }
/** * @param mixed $id * @param array $parameters * @param string $domain * @param string $locale * @param integer $number * @param ThemeInterface[] $themes * * @return null|MessageCatalogueInterface */ protected function doTranslate($id, array $parameters, $domain, $locale, $number, array $themes = []) { $id = (string) $id; $domain = $domain ?: 'messages'; $catalogue = null; foreach ($themes as $theme) { $themedMessageId = $id . '|' . $theme->getLogicalName(); if ($catalogue = $this->getCatalogueHavingTranslation($themedMessageId, $domain, $locale)) { $id = $themedMessageId; break; } } if (null === $catalogue) { $catalogue = $this->getCatalogueHavingTranslation($id, $domain, $locale); } $translatedMessage = $catalogue ? $catalogue->get($id, $domain) : $id; if (null !== $number) { $locale = $catalogue ? $catalogue->getLocale() : $locale; $translatedMessage = $this->selector->choose($translatedMessage, $number, $locale); } return strtr($translatedMessage, $parameters); }
/** * @expectedException InvalidArgumentException */ public function testChooseWhenNoEnoughChoices() { $selector = new MessageSelector(); $selector->choose('foo', 10, 'en'); }
/** * If using the return method, it will return an array of the variable */ function lang($text = false, $vars = null, $value = null, $group = null, $locale = null, $force_add = false) { $original_locale = Lang::getLocale(); $params['value'] = $value ? $value : 0; $params['vars'] = $vars ? $vars : []; $params['locale'] = $locale ? $locale : $original_locale; $params['group'] = $group ? $group : 'general'; $params['dynamic'] = false; if (preg_match('/dynamic_/', $params['group'])) { $params['group'] = preg_replace('/dynamic_/', '', $params['group']); $params['dynamic'] = true; } $hash = md5($text) . sha1($text); $file_namespace = 'dbtranslator'; if (preg_match('/\\|/', $text)) { $choose = true; } else { $choose = false; } $config = config('db-translator'); /** * Before anything lets change the locale to the intl locale */ App::setLocale($params['locale']); /** * This will check the existence of the translation on the locale given, not the default */ if (!$force_add) { if (Lang::has($file_namespace . '::' . $params['group'] . '.' . $hash)) { if ($choose) { $text = trans_choice($file_namespace . '::' . $params['group'] . '.' . $hash, $params['value'], $params['vars']); App::setLocale($original_locale); return $text; } else { $text = trans($file_namespace . '::' . $params['group'] . '.' . $hash, $params['vars']); App::setLocale($original_locale); return $text; } } else { /** * If we are using the database translations */ if ($config['use_database']) { if (!Intl::whereText($text)->whereGroup($params['group'])->get()->count()) { $intl = new Intl(); $intl->text = $text; $intl->group = $params['group']; $intl->md5sha1 = $hash; $intl->dynamic = $params['dynamic']; $intl->save(); } } else { /** * first we will check if the translation exists under the fallback language */ App::setLocale($config['default_locale']); if (!Lang::has($file_namespace . '::' . $params['group'] . '.' . $hash)) { // if we don't find it, we should include the file for the original locale // change it with the new array, and save it. /** * Determine if the file exists */ if (Storage::disk($config['storage_driver'])->exists($config['default_locale'] . '/' . $params['group'] . '.php')) { $lang_array = (require base_path('resources/lang/vendor/dbtranslator/' . $config['default_locale'] . '/' . $params['group'] . '.php')); if (!isset($lang_array[$hash])) { $lang_array[$hash] = $text; $file = "<?php\n return [\r\n"; foreach ($lang_array as $k => $v) { $file .= " \"" . $k . "\" => \"" . str_replace('"', '\\"', $v) . "\",\r\n"; } $file .= "];"; Storage::disk($config['storage_driver'])->put($config['default_locale'] . '/' . $params['group'] . '.php', $file); } } else { $file = "<?php\n return [\r\n"; $file .= " \"" . $hash . "\" => \"" . str_replace('"', '\\"', $text) . "\",\r\n"; $file .= "];"; Storage::disk($config['storage_driver'])->put($config['default_locale'] . '/' . $params['group'] . '.php', $file); } } App::setLocale($params['locale']); } // now we will process the string if ($choose) { $params['vars']['count'] = $params['value']; $ms = new MessageSelector(); $text = $ms->choose($text, $params['value'], $params['locale']); $params['vars'] = (new Collection($params['vars']))->sortBy(function ($value, $key) { return mb_strlen($key) * -1; }); foreach ($params['vars'] as $key => $value) { $text = str_replace(':' . $key, $value, $text); } App::setLocale($original_locale); return $text; } else { if (count($params['vars'])) { $params['vars'] = (new Collection($params['vars']))->sortBy(function ($value, $key) { return mb_strlen($key) * -1; }); foreach ($params['vars'] as $key => $value) { $text = str_replace(':' . $key, $value, $text); } } App::setLocale($original_locale); return $text; } } } else { if ($config['use_database']) { if (!Intl::whereText($text)->whereGroup($params['group'])->get()->count()) { $intl = new Intl(); $intl->text = $text; $intl->group = $params['group']; $intl->md5sha1 = $hash; $intl->dynamic = $params['dynamic']; $intl->save(); } } } }
/** * @dataProvider getNonMatchingMessages * @expectedException \InvalidArgumentException */ public function testThrowExceptionIfMatchingMessageCannotBeFound($id, $number) { $selector = new MessageSelector(); $selector->choose($id, $number, 'en'); }
/** * Returns a translated item with a proper form for pluralization. * * @param string $item The item to translate. * @param integer $number Number of items for pluralization. * @param array $replace An replace array. * * @return string * * @since 2.0.0 */ public function choice($item, $number, array $replace = array()) { $lines = $this->get($item, $replace); $replace['count'] = $number; return $this->makeReplacements($this->selector->choose($lines, $number, $this->locale), $replace); }
/** * @param string $key * @param int $number * @param array|null $data * @param bool $warmIfMissing * * @return string */ public function transChoice(string $key, int $number, array $data = null, bool $warmIfMissing = true) { if (!$data) { $data = [$number]; } $text = self::trans($key, null, $warmIfMissing); if (!$this->messageChoice) { $this->messageChoice = new MessageSelector(); } return $this->replace($this->messageChoice->choose($text, $number, $this->locale), $data); }
/** * Translates the given choice message by choosing a * translation according to a number. * * If multiple parameters are set, these values will be sprintf'ed on the msg * * @param $messageTemplate * @param int $number * * @return string * @throws \RuntimeException */ public function choice($messageTemplate, $number = 0) { try { $result = $this->getTranslator()->transChoice($messageTemplate, $number); } catch (\Exception $e) { $selector = new MessageSelector(); $result = $selector->choose($messageTemplate, $number, 'en_EN'); } if (func_num_args() > 2) { $args = func_get_args(); array_shift($args); array_shift($args); $result = vsprintf($result, $args); } return $result; }