Reputation: 2566
Consider the following code fragment:
$translator->setLocale('nl');
$translator->trans('You have ordered one cup of coffee');
Many languages have what's called the T–V distinction to make text more polite or familiar: "you" becomes a capitalized "U" or a non-capitalized "je" in Dutch, "Vous" or "tu" in French, etc.
Which one we want to use depends on the target audience (In our case mostly based on domain), one of our sites selling luxury items to older people should use polite messages, while website targeting students needs to use the more familiar forms.
What would be the best way to replace specific messages in the translator? Creating a complete new locale ('nl, nl_polite') seems like overkill, requiring us to translate every message twice for every locale with a T–V distinction.
Ideally I would like to do something like this, inside the existing onKernelRequest listener which already deals with subdomain/domain/customer settings:
if ($translator->getLocale() == 'nl' && $customer->getLanguagePolite() == true)
$translator->replaceWord('you','U');
$translator->replaceWord('your','Uw');
}
I would like to replace every occurrence of the word inside of the messages while translating (maybe using preg_replace?), instead of replacing messages. The grammatical position of the word doesn't change after all.
Could I do this by overriding the symfony translator?
Upvotes: 1
Views: 431
Reputation: 31912
Use placeholders for the parts that will vary.
See http://symfony.com/doc/current/components/translation/usage.html#component-translation-placeholders
e.g.
if ($customer->getLanguagePolite()) {
$placeholders = [
'%YOU%' => 'U',
'%YOUR%' => 'Uw',
etc
]
} else {
$placeholders = [
...
]
}
$yourMessage = '%YOU% blah blah %YOUR% etc';
$translator->trans($yourMessage, $placeholders);
Upvotes: 1