setlocale and strftime not translating month
Asked Answered
W

7

10

My code:

setlocale(LC_TIME, 'ca_ES');
echo strftime("%#d %B", strtotime($ticket->date_created));

outputs something like:

28 August

instead of my expectation:

28 Agost

I expect "Agost" because that is Catalan language (set through setlocale()).

Is this how setlocale and strftime is supposed to work?

FYI: My local development machine is Windows 7, set to locale: en-PH

Westphal answered 24/9, 2012 at 13:24 Comment(0)
A
22

The locale names are different on Windows. It looks like it's defaulting to English.

Try the full length name (you can try several and it will pick the first found):

setlocale(LC_TIME, 'ca_ES', 'Catalan_Spain', 'Catalan');

You may look at this table, which may be helpful : http://docs.moodle.org/dev/Table_of_locales

Anglaangle answered 24/9, 2012 at 13:37 Comment(3)
@Westphal Don't forget to keep the 'ca_ES' for compatibility with other systemsAnglaangle
i've succefully used this on debian+nginx: setlocale(LC_TIME, 'ca_ES.UTF-8', 'Catalan_Spain', 'Catalan');Warplane
Had same problem in Ubuntu 15.10, solved by adding the ".UTF8" part to es_ESOpener
G
9

You need to install the locale you want in your server.

  1. SSH into your server
  2. Check which locales are installed: locale -a
  3. if your locale is not there, install it: sudo locale-gen ca_ES
  4. Update the locales in your server: sudo update-locale
  5. Restart apache for the changes to take effect: sudo service apache2 restart

That is it!

Geanticline answered 24/8, 2018 at 6:17 Comment(1)
I am using Ubuntu 18 and this way worked for me. I set Turkish date like as "tr_TR.uft8".Weinberg
P
6

On Ubuntu 14.04 setlocale(LC_TIME, 'de_DE.UTF8'); did the trick for me to get a german month format/name :)

Peculation answered 9/12, 2015 at 14:5 Comment(0)
D
3

On Debian 9 setlocale(LC_TIME, 'fr_FR.UTF8'); did the job for me to get nicely french formatted day/month display. Make sure to have the locales installed with the following sequence:

apt-get install locales

dpkg-reconfigure locales

Dessert answered 17/11, 2017 at 13:55 Comment(0)
Z
2

strftime gives localized versions of date formats. If you do get unexpected results, most probably the localized version you are expecting is not on your system.

I have no experience with windows, but on my Debian Linux server, I had to install the localized strings I wanted.

Zip answered 24/9, 2012 at 13:35 Comment(0)
P
1

For all those, like me, who still cannot make strftime function due to server restrictions... Not perfect but it works.

$months[1] = "gennaio";
$months[2] = "febbraio";
$months[3] = "marzo";
$months[4] = "aprile";
$months[5] = "maggio";
$months[6] = "giugno";
$months[7] = "luglio";
$months[8] = "agosto";
$months[9] = "settembre";
$months[10] = "ottobre";
$months[11] = "novembre";
$months[12] = "dicembre";

// giorni
$weekdays[0] = "domenica";
$weekdays[1] = "lunedì";
$weekdays[2] = "martedì";
$weekdays[3] = "mercoledì";
$weekdays[4] = "giovedì";
$weekdays[5] = "venerdì";
$weekdays[6] = "sabato";

function strftimeIta($format, $timestamp){

    global $weekdays, $months;

    preg_match_all('/%([a-zA-Z])/', $format, $results);

    $originals = $results[0];
    $factors = $results[1];


    foreach($factors as $key=>$factor){
        switch($factor){
            case 'a':
                /*** Abbreviated textual representation of the day ***/
                $n = date('w', $timestamp); // number of the weekday (0 for sunday, 6 for saturday);
                $replace = ucfirst($weekdays[$n]);
                $replace = substr($replace, 0, 3);
                break;
            case 'A':
                /*** Full textual representation of the day ***/
                $n = date('w', $timestamp); // number of the weekday (0 for sunday, 6 for saturday);
                $replace = ucfirst($weekdays[$n]);
                break;
            case 'h':
            case 'b':
                /*** Abbreviated month name ***/
                $n = date('n', $timestamp); // Numeric representation of a month, without leading zeros
                $replace = ucfirst($months[$n]);
                $replace = substr($replace, 0, 3);
                break;
            case 'B':
                /*** Full month name ***/
                $n = date('n', $timestamp); // Numeric representation of a month, without leading zeros
                $replace = ucfirst($months[$n]);
                break;
            default:
                /*** Use standard strftime function ***/
                $replace = strftime("%".$factor, $timestamp);
                break;
        }
        $search = $originals[$key];
        $format = str_replace($search, $replace, $format);
    }
    return $format;
}
Pyroelectricity answered 6/4, 2017 at 10:14 Comment(0)
I
0

I know that this is an old question, but I thought I'd contribute anyway.

If you can SSH into the server that you're working on, then write: locale -a and then you can see your available locales. If you're language isn't listed, then it need to be installed.

For me, I saw the one I was looking for: nb_NO.utf8 in the list, so I wrote it exactly how it was written: setlocale( LC_ALL, 'nb_NO.utf8' ) and that worked for me.

Inelegant answered 15/12, 2017 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.