Setting the Translation Message Selector

Problem

You want to change how the message selector logic works.

Solution

Use the Lang::setSelector() to set your own message selector.

$selector = new MyMessageSelector;
Lang::setSelector($selector);

The message select is what handles pulling the correct message from a pipe (|) delimited string based on a number, locale, and the pluralizaion rules in the message itself.

Your class must extend the Symfony\Component\Translation\MessageSelector class. It should then override the choose() method to implement your custom logic.

See also the Getting the Translation Message Selector recipe.

Discussion

Where to call this method?

It should be called earlier than the Lang facade is used. Since this is very low level coding, the best place is a service provider.

comments powered by Disqus