Manaus Posted June 2, 2023 Posted June 2, 2023 I'm using TwigTemplateEngine for an Italian website, but I get always English dates for `{{ page.created }}` code. I installed the Languages Support module, Languages, Italian Language Pack, used `setlocale('LC_TIME', "it_IT")`, `setlocale('LC_TIME', "ita")`, and the various combinations with LC_ALL, but no. How can I output an Italian date on the page? Thanks!
Jan Romero Posted June 2, 2023 Posted June 2, 2023 date - Documentation - Twig - The flexible, fast, and secure PHP template engine Says here you can change Twig’s default date format like this: $twig = new \Twig\Environment($loader); $twig->getExtension(\Twig\Extension\CoreExtension::class)->setDateFormat('d/m/Y', '%d giorni'); And format individual expressions like this: {{ page.created|date("d/m/Y") }} But to get Italian words like ”giugno“ or “venerdi” you’ll probably need something called “Twig Intl Extension”: https://stackoverflow.com/a/75572704 1
Manaus Posted June 11, 2023 Author Posted June 11, 2023 Thanks Jan, but I can't get it to work It returns 'Undefined variable: loader' when I load the page... I think the code you are quoting is suitable for the Symfony context?
poljpocket Posted July 12, 2023 Posted July 12, 2023 Indeed, it assumes you have a $loader as in FileSystemLoader or similar. Anyway, you must use a hook so that your code works. From the docs of the Module you are using, you can find this: wire()->addHookAfter('TemplateEngineTwig::initTwig', function (HookEvent $event) { /** @var \Twig_Environment $twig */ $twig = $event->arguments('twig'); // add your extensions to $twig here }); You should add this to ready.php. Here you have the $twig variable which is exactly what you want according to the snippet provided by @Jan Romero. Sadly, this seems to use Twig version V1 or V2 whereas Jan's example uses V3. Twig V1 and V2 don't use namespaces whereas Twig V3 does. A quick composer.json scan shows me, TwigTemplateEngine still uses Twig V1.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now