Missing setlocale languages

Hey,

I can’t use setlocale php function on my native language so the dates are still in english language.

February instead of Február

If I use the followed command I get the die message which means it’s not installed yet.

setlocale(LC_ALL, 'hu_HU') or die('Locale not installed');

How can I resolve this in this environment?

Kind regards,
Krisztian

Hi @chrisdark86

As a workaround for now, you could try adding this to the start of your theme’s functions.php file. After the change you’ll want to Stop and Restart your Local site:

setlocale(LC_ALL, 'hu_HU.UTF-8');
putenv('LC_ALL=hu_HU.UTF-8');

Hey @Nick-B

I tried it as you wrote, but unfortunately it doesn’t works yet.

I’ve never worked with setlocale() before taking a look here. It looks like PHP and locale are kind of finicky and are greatly dependent on the system’s locale settings and how the various environment variables interact with each other.

Additionally, some of the user contributed notes of the setlocale function mention that on Windows, you need to use different locale strings:

When I was tinkering, I was able to get things working.

Here’s the PHP file I was working with:

<?php
var_dump(setlocale(LC_ALL, 0));

var_dump(localeconv());
?>

<h2>setlocale</h2>

<p>
<?php
    setlocale(LC_ALL, 'hu_HU');
    echo strftime("%A %B %Y", time());
?>
</p>

<h2>IntlDateFormatter</h2>

<p>
<?php
    $formatter = new IntlDateFormatter('hu_HU', IntlDateFormatter::LONG, IntlDateFormatter::NONE, null, null, 'EEEE MMMM yyyy');
    echo $formatter->format(time());
?>
</p>

I added a section for IntlDateFormatter::format because it looks like using things like setlocale+strftime is deprecated in PHP 8.1 and the strftime functions will be removed in PHP 9.0:

So, if you can, I’d recommend migrating to using the intl extension (ie, the IntlDateFormatter) because that is better supported going forward, and Local should have everything that it needs for that extension to work properly.

If you can’t migrate to a different implementation, then I’d recommend taking a look at what OS you are using and review the setlocal manual entry for nuances related to different OSes.

3 Likes

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.