Symfony Translation

[Solved] Symfony Translation | Php - Code Explorer | yomemimo.com
Question : symfony translate

Answered by : doubtful-dog-pmdcbwwgpxjj

# config/packages/translation.yaml
framework: default_locale: 'en' translator: default_path: '%kernel.project_dir%/translations'

Source : https://symfony.com/doc/current/translation.html | Last Update : Wed, 19 Jan 22

Question : symfony translation

Answered by : murat-akmak

//For symfony version >=4.4
//Configuration Languages
framework: default_locale: en translator: default_path: '%kernel.project_dir%/translations' fallbacks: #doesn't find and searching other languages. - en
//translations/messages.en.yaml
test translation: test translation en
a: b: c: abc en
//translations/messages.tr.yaml
test translation: test translation tr
a: b: c: abc tr
#######################################
//Using
private TranslatorInterface $translator;
public function __construct(TranslatorInterface $translator)
{ $this->translator = $translator;
}
//dd($this->translator->trans('test translation'));
dd($this->translator->trans('a.b.c'));
//How to change locale
//Way 1
use Symfony\Component\HttpFoundation\Request;
public function index(Request $request)
{ $locale = $request->getLocale();
}
//Way 2
public function onKernelRequest(RequestEvent $event)
{ $request = $event->getRequest(); // some logic to determine the $locale $request->setLocale($locale);
}

Source : https://symfony.com/doc/4.4/translation.html#translation-resource-file-names-and-locations | Last Update : Thu, 15 Sep 22

Answers related to symfony translation

Code Explorer Popular Question For Php