Skip to content

Instantly share code, notes, and snippets.

@frankdejonge
Created May 28, 2018 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankdejonge/0de01c7354cd969ad576c8a639ba4cc9 to your computer and use it in GitHub Desktop.
Save frankdejonge/0de01c7354cd969ad576c8a639ba4cc9 to your computer and use it in GitHub Desktop.
<?php
namespace App\Translator;
use MessageFormatter;
use Symfony\Component\Translation\MessageSelector;
class IcuMessageSelector extends MessageSelector
{
/**
* @var MessageSelector
*/
private $selector;
public function __construct(MessageSelector $selector = null)
{
$this->selector = $selector ?: new MessageSelector();
}
public function choose($message, $number, $locale)
{
if (\preg_match_all('~\{([a-zA-Z_\-\.]+), plural, .*\}~', $message, $matches) === false) {
return $this->selector->choose($message, $number, $locale);
}
$variable = \str_replace('.', '_', $matches[1][0]);
$message = \strtr($message, [$matches[1][0] => $variable]);
return (new MessageFormatter($locale, $message))->format([$variable => $number]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment