Jump to content

Twig - page.created in other languages


Recommended Posts

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?


Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...

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.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...