Translating date formats

Hello,

I can’t find the answer anywhere in the documentation. How can I translate the date formats?

If the user chooses French, i’d want to display it as “15 septembre 2021” (notice september is spelled the french way with no capital on the “s” and also with no comma between “september” and “2021”)

If they choose English, id’ want it displayed as “September 15, 2021” (notice there is a comma after “15”)

This is how I have it at the moment:

<?= $articlesToShow->first()->date()->toDate('j F Y') ?>

How can I achieve this?

Thank you!

You can set the strftime date handler in your config, then use the appropriate strtime date formats:

https://www.php.net/manual/de/function.strftime.php

Ok but how will it change depending on the language chosen by the front end user?

That happens automatically depending on the locale settings. If you additionally want to change the format string, you can do that in your language translation strings.

'translations' => [
  'date.format' => '%Y-%m-%d',
]

In your template:

<?= $articlesToShow->first()->date()->toDate(t('date.format')) ?>

Thanks for your help. We’re almost there. So as fort he format, I got it working for both languages. The only issue is that “September” is written the same way in both languages. From what I gathered, for it to be translated in French, I need to set locale. How can I achieve this (i’m not much of a coder so any code example will help me a lot)?

The locale in also set in your language config files, should look something like this:

site/languages/de.php

<?php

return [
    'code' => 'de',
    'default' => true,
    'direction' => 'ltr',
    'locale' => [
        'LC_ALL' => 'de_DE'
    ],
];

You can also use more detailed locale settings, like explained here: locale | Kirby CMS (but for your multi-language site, you do this in the language files, not in the config)

Also make sure that you are using the right locale settings that are actually installed on your system. You can check this in on the command line with

locale -a

Hmm I seem to already have it set up that way. But it’s not translating. Here’s what I ahve in languages:

return [
    'code' => 'fr',
    'default' => true,
    'direction' => 'ltr',
    'locale' => [
        'LC_ALL' => 'fr_CA'
    ],

And:

'date.format' => 'j F Y',

Does this mean I have no way of knowing whether it is translated or not unless I change my system’s language? There is no other way of doing this?

You do not have to change your system’s language. You just have to make sure that the locale aou want to use is installed. Is fr_CA.utf8 installed on your system?

Yes it is installed on my system.

What is the exact locale string on your system? On my system, if I do a locale -a | grep fr_CA, I get:

fr_CA.ISO8859-1
fr_CA
fr_CA.ISO8859-15
fr_CA.UTF-8

Not sure about the exact string, but it does say French (Canada).

Have you executed the command on the command line?

Command line as in Command Prompt? Because Command Prompt just gives me ‘locale’ is not recognized as an internal command.

In a terminal / shell. What is your OS?

Windows 10.

Have you actually set the date handler in your config like I suggested above:

  'date'  => [
        'handler' => 'strftime'
    ]

I don’t know how to get a list of locales on Windows (what I posted above works on Linux/Mac), but you can certainly google it)

To recap:

  1. In your config, set date handler to strftime
  2. In your language files make sure to set the right locale string
  3. For different formats, use translation strings

Yes I have this in my config file:

'debug' => true,
    'panel' => [
        'install' => true
    ],
    'date'  => [
        'handler' => 'date'
    ],

    'languages' => true,

In my case i’m not using “strftime” but rather “date”

Should be strftime not date!

Does it have to be? Because the formatting in strftime does not have the format I want.