本文整理汇总了PHP中Symfony\Component\Translation\TranslatorInterface::setLocale方法的典型用法代码示例。如果您正苦于以下问题:PHP TranslatorInterface::setLocale方法的具体用法?PHP TranslatorInterface::setLocale怎么用?PHP TranslatorInterface::setLocale使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Translation\TranslatorInterface
的用法示例。
在下文中一共展示了TranslatorInterface::setLocale方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onKernelRequest
/**
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
if ($this->session->has('_locale')) {
$event->getRequest()->setLocale($this->session->get('_locale'));
$this->translator->setLocale($this->session->get('_locale'));
}
}
示例2: setUp
/**
* Set up the tests.
*/
public function setUp()
{
parent::setUp();
$this->translator = $this->app['translator'];
$this->detector = new LanguageDetector($this->translator);
$this->translator->setLocale('fr');
}
示例3: setLocale
/**
* @param HttpRequest $request
* @param string $locale
*/
public function setLocale(HttpRequest $request, $locale)
{
setlocale(LC_ALL, $locale);
$locale = substr($locale, 0, 2);
$request->setLocale($locale);
$this->translator->setLocale($locale);
$this->translatable->setTranslatableLocale($locale);
}
示例4: initialize
private function initialize()
{
if ($this->initialized || $this->requestAnalyzer->getCurrentLocalization() === null) {
return;
}
$this->translator->setLocale($this->requestAnalyzer->getCurrentLocalization()->getLocale(Localization::LCID));
$this->initialized = true;
}
示例5: sendEmail
public function sendEmail($type, $emailAddress, $arguments, $locale = 'en')
{
$this->eventDispatcher->dispatch(EmailEvent::BEFORE_SEND, new EmailEvent($type, $emailAddress, $arguments));
$this->translator->setLocale($locale);
$message = $this->constructEmailMessage($type, $emailAddress, $arguments);
$status = $this->emailService->send($message);
$this->dispatchEmailSendingEvent($type, $arguments, $message, $status);
return $status;
}
示例6: onPostUpdate
/**
* @param GenericEvent $event
*/
public function onPostUpdate(GenericEvent $event)
{
$user = $event->getSubject();
if ($user === $event->getArgument('current_user')) {
$request = $this->requestStack->getMasterRequest();
$request->getSession()->set('_locale', $user->getUiLocale()->getCode());
$this->translator->setLocale($user->getUiLocale()->getCode());
}
}
示例7: onKernelRequest
/**
* onKernelRequest
*
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
$url = $event->getRequest()->getRequestUri();
$token = $this->context->getToken();
if ($token && $this->isAdminToken($this->providerKey, $token) && $this->isAdminRoute($url)) {
$locale = $token->getUser()->getAdminLocale();
if (!$locale) {
$locale = $this->defaultAdminLocale;
}
$this->translator->setLocale($locale);
}
}
示例8: setLocale
/**
* Set new locale.
*
* @param string $locale
* @throws LanguageNotFoundException
*/
public function setLocale($locale)
{
if (!in_array($locale, $this->getAvailableLanguages())) {
throw new LanguageNotFoundException("Language {$locale} not found.");
}
$this->translator->setLocale($locale);
}
示例9: setRequestLocale
public function setRequestLocale(GetResponseEvent $event)
{
$token = $this->tokenStorage->getToken();
if (!$token) {
return;
}
/** @var Identity $identity */
$identity = $token->getUser();
$request = $event->getRequest();
$request->setLocale($identity->preferredLocale);
// As per \Symfony\Component\HttpKernel\EventListener\TranslatorListener::setLocale()
try {
$this->translator->setLocale($request->getLocale());
} catch (\InvalidArgumentException $e) {
$this->translator->setLocale($request->getDefaultLocale());
}
}
示例10: onKernelRequest
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$this->isApiRequest($request)) {
return;
}
// request language
if ($request->headers->has('Language')) {
$this->translator->setLocale($request->headers->get('Language'));
$request->setLocale($request->headers->get('Language'));
}
// request content type
if ($type = $request->getContentType()) {
switch ($type) {
case 'json':
$request->setRequestFormat('json');
break;
default:
$mime = $request->headers->get('Content-Type');
throw new HttpException(406, "The content type: \"{$type}\" specified as mime \"{$mime}\" - is not supported.");
}
} else {
// default format is JSON
$request->setRequestFormat('json');
}
// request accept content type, currently only JSON
$accepts = $request->getAcceptableContentTypes();
$types = array_filter(array_unique(array_map([$request, 'getFormat'], $accepts)));
if ($types && !in_array('json', $types, true)) {
$acceptable = implode(',', $accepts);
throw new HttpException(406, "None of acceptable content types: {$acceptable} are supported.");
}
// if there is a body, decode it currently as JSON only
if ($content = $request->getContent()) {
$data = @json_decode($content, true);
if (null === $data) {
// the error may be important, log it
if (null !== $this->logger) {
$this->logger->error("Failed to parse json request content, err: " . json_last_error_msg());
}
throw new HttpException(400, "The given content is not a valid json.");
}
$request->request = new ParameterBag($data);
}
}
示例11: sendBatchMail
/**
* @param $fromEmail
* @param BatchEntryMail $batchMail
* @param $doSend
*/
private function sendBatchMail($fromEmail, BatchEntryMail $batchMail, $doSend)
{
$receivers = $batchMail->getReceiverEntries($this->em);
$this->writeOutput('Sending "' . $batchMail->getName() . '" mail to ' . count($receivers) . ' people from ' . $fromEmail);
$spool = $this->mailer->getTransport()->getSpool();
foreach ($receivers as $receiver) {
$this->writeOutput(' -> ' . $receiver->getEmail());
$htmlTemplate = $batchMail->getHtmlTemplate($receiver);
$plainTextTemplate = $batchMail->getPlainTextTemplate($receiver);
$templateData = $batchMail->getTemplateData($receiver, $this->em);
$this->translator->setLocale($receiver->getPool()->getLocale());
$plainTextBody = $this->twig->render($plainTextTemplate, $templateData);
$htmlBody = $this->twig->render($htmlTemplate, $templateData);
$message = \Swift_Message::newInstance()->setSubject($batchMail->getSubject($receiver, $this->translator))->setFrom($fromEmail, $batchMail->getFrom($receiver, $this->translator))->setTo($receiver->getEmail())->setBody($plainTextBody)->addPart($htmlBody, 'text/html');
if ($doSend) {
$this->mailer->send($message);
$spool->flushQueue($this->transport);
$batchMail->handleMailSent($receiver, $this->em);
}
}
}
示例12: render
/**
* renders content with the real website controller.
*
* @param PageBridge $content
* @param bool $partial
*
* @return string
*/
public function render(PageBridge $content, $partial = false)
{
// set active theme
$webspace = $this->webspaceManager->findWebspaceByKey($content->getWebspaceKey());
$this->activeTheme->setName($webspace->getTheme()->getKey());
// get controller and invoke action
$request = new Request();
$request->attributes->set('_controller', $content->getController());
$controller = $this->controllerResolver->getController($request);
// prepare locale for translator and request
$request->setLocale($content->getLanguageCode());
$localeBefore = $this->translator->getLocale();
$this->translator->setLocale($content->getLanguageCode());
$this->requestStack->push($request);
/** @var Response $response */
$response = $controller[0]->{$controller[1]}($content, true, $partial);
// roll back
$this->requestStack->pop();
$this->translator->setLocale($localeBefore);
return $response->getContent();
}
示例13: onKernelController
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
if (!is_array($controller)) {
// not a object but a different kind of callable. Do nothing
return;
}
$controllerObject = $controller[0];
if ($controllerObject instanceof CantigaController) {
// set the time formatting settings
$this->translator->setLocale($event->getRequest()->getLocale());
$this->timeFormatter->configure($this->translator, $event->getRequest()->getLocale(), $event->getRequest()->getSession()->get('timezone'));
$this->localeProvider->setLocale($event->getRequest()->getLocale());
// initialize the controller
$potentialResponse = $controllerObject->initialize($event->getRequest(), $this->authChecker);
if (!empty($potentialResponse)) {
$event->setController(function () use($potentialResponse) {
return $potentialResponse;
});
}
}
}
示例14: onConsoleCommand
public function onConsoleCommand()
{
setlocale(LC_ALL, $this->locale);
$this->translator->setLocale($this->locale);
$this->translatable->setTranslatableLocale(substr($this->locale, 0, 2));
}
示例15:
function it_proxies_setting_the_locale_to_the_decorated_translator(TranslatorInterface $translator)
{
$translator->setLocale('pl_PL')->shouldBeCalled();
$this->setLocale('pl_PL');
}