Skip to content

Instantly share code, notes, and snippets.

@boscho87
Created September 13, 2019 08:38
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 boscho87/b778ae4882d1b673adb6ea6f0d229acf to your computer and use it in GitHub Desktop.
Save boscho87/b778ae4882d1b673adb6ea6f0d229acf to your computer and use it in GitHub Desktop.
class XYTranslator implements TranslatorInterface
{
/**
* @var User
*/
protected $userMsgDomain;
/**
* @var string
*/
protected $systemMsgDomain;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var string
*/
private $locale;
/**
* @var ParameterParserService
*/
private $parameterParserService;
/**
* XYTranslator constructor.
*
* @param TranslatorInterface $translator
* @param UserService $getUserService
* @param SystemCheckerInterface $hostChecker
* @param ParameterParserService $parameterParserService
*/
public function __construct(
TranslatorInterface $translator,
UserService $getUserService,
SystemCheckerInterface $hostChecker,
ParameterParserService $parameterParserService
) {
$this->translator = $translator;
if (($user = $getUserService->getUser())) {
$this->userMsgDomain = $user->getMsgDomain();
}
$this->getUserService = $getUserService;
$this->hostChecker = $hostChecker;
$this->setSystemMsgDomain($hostChecker);
$this->parameterParserService = $parameterParserService;
}
/**
* @param SystemCheckerInterface $hostChecker
*/
private function setSystemMsgDomain(SystemCheckerInterface $hostChecker)
{
if ($hostChecker->isMyKlett()) {
$this->systemMsgDomain = MessageServiceInterface::ANONYMOUS;
return;
}
$this->systemMsgDomain = MessageServiceInterface::PROFILE;
}
/**
* @param string $msgDomain
*/
public function setMsgDomain(string $msgDomain)
{
$this->systemMsgDomain = $msgDomain;
}
/**
* @param string $id
* @param array $parameters
* @param null $domain
* @param null $locale
*
* @return string
*/
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
//force translations with domain
if (isset($domain)) {
$translated = $this->translator->trans($id, $parameters, $domain, $locale);
if ($translated !== $id) {
return $translated;
}
}
// parse the parameters
$parameters = $this->getParameters($parameters);
//check if there is a translation for the user
$translated = $this->translator->trans($id, $parameters, $this->userMsgDomain);
if ($translated !== $id) {
return trim($translated);
}
//check if there is a translation for the current system
$translated = $this->translator->trans($id, $parameters, $this->systemMsgDomain);
if ($translated !== $id) {
return trim($translated);
}
//return the default translation
return trim($this->translator->trans($id, $parameters));
}
/**
* @param $id
* @param array $param
*
* @return string
*/
public function navItem($id, array $param = [])
{
return trim($this->translator->trans($id, $param, 'navigation'));
}
/**
* Translates the given choice message by choosing a translation according to a number.
*
* @param string $id The message id (may also be an object that can be cast to string)
* @param int $number The number to use to find the indice of the message
* @param array $parameters An array of parameters for the message
* @param string|null $domain The domain for the message or null to use the default
* @param string|null $locale The locale or null to use the default
*
* @return string The translated string
*
* @throws InvalidArgumentException If the locale contains invalid characters
*/
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
{
$parameters = $this->getParameters($parameters);
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
}
/**
* Sets the current locale.
*
* @param string $locale The locale
*
* @throws InvalidArgumentException If the locale contains invalid characters
*/
public function setLocale($locale)
{
$this->locale = $locale;
}
/**
* Returns the current locale.
*
* @return string The locale
*/
public function getLocale()
{
return $this->locale;
}
private function getParameters(array $parameters): array
{
$parameters = array_merge([
PageInfo::class,
KlettUser::class,
Link::class,
], $parameters);
$parameters = $this->parameterParserService->parseParameters($parameters);
return $parameters;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment