/** Return ICU-formatted string @return string @param $str string @param $args mixed @public **/ static function format($str, $args) { // Format string according to locale rules if (extension_loaded('intl')) { return msgfmt_format_message(Locale::getDefault(), $str, is_array($args) ? $args : array($args)); } $info = localeconv(); return preg_replace_callback('/{(\\d+)(?:,(\\w+)(?:,(\\w+))?)?}/', function ($expr) use($args, $info) { $arg = $args[$expr[1]]; if (!isset($expr[2])) { return $arg; } if ($expr[2] == 'number') { if (isset($expr[3])) { switch ($expr[3]) { case 'integer': return number_format($arg, 0, $info['decimal_point'], $info['thousands_sep']); case 'currency': return $info['currency_symbol'] . ($info['p_sep_by_space'] ? ' ' : '') . number_format($arg, $info['frac_digits'], $info['mon_decimal_point'], $info['mon_thousands_sep']); } } else { return sprintf('%f', $arg); } } elseif ($expr[2] == 'date') { return strftime('%x', $arg); } else { return $arg; } }, $str); }
/** Return ICU-formatted string @return string @param $str string @param $args mixed @public **/ static function format($str, $args) { // Format string according to locale rules if (extension_loaded('intl')) { return msgfmt_format_message(Locale::getDefault(), $str, is_array($args) ? $args : array($args)); } foreach ($args as &$arg) { if (preg_match('/@\\w+\\b/', $arg)) { $arg = self::resolve('{{' . $arg . '}}'); } } self::$locale = setlocale(LC_ALL, NULL); if (preg_match('/^\\w{2}/', self::$vars['LANGUAGE'], $match)) { setlocale(LC_ALL, self::$languages[$match[0]]); } $info = localeconv(); $out = preg_replace_callback('/{(\\d+)(?:,(\\w+)(?:,(\\w+))?)?}/', function ($expr) use($args, $info) { foreach ($info as &$val) { if (is_string($val)) { $val = utf8_decode($val); } } extract($info); $arg = $args[$expr[1]]; if (!isset($expr[2])) { return $arg; } if ($expr[2] == 'number') { if (isset($expr[3])) { switch ($expr[3]) { case 'integer': return number_format($arg, 0, $decimal_point, $thousands_sep); case 'currency': return $currency_symbol . ($p_sep_by_space ? ' ' : '') . number_format($arg, $frac_digits, $mon_decimal_point, $mon_thousands_sep); } } else { return sprintf('%f', $arg); } } elseif ($expr[2] == 'date') { return strftime('%x', $arg); } else { return $arg; } }, $str); setlocale(LC_ALL, NULL); return $out; }
/** * Format the given string using the current system locale * Basically, it's sprintf on i18n steroids. * * @param string $string to parse * @param array $params to insert * @return string */ function __($string, array $params = NULL) { return msgfmt_format_message(setlocale(LC_ALL, 0), $string, $params); }
public function translate($string, $args = []) { $string = $this->getString($string) ?: $string; $string = msgfmt_format_message(static::$locale, $string, $args); return $string; }
function message_list($locale, $format, $elements) { $result = msgfmt_format_message($locale, $format, $elements); return $result; }