Page 1 of 2 12 LastLast
Results 1 to 10 of 15
Like Tree1Likes

Thread: How to change locale in PHP?

  1. #1
    jellby is offline Junior Member
    Join Date
    Nov 2014
    Posts
    9

    Default How to change locale in PHP?

    I tried to use setlocale in php, but it doesn't seem to be working according to the documentation (https://www.php.net/manual/en/function.setlocale.php). What I get is that the locale stays put to "C".

    For example, see: https://jellby.altervista.org/test.php

    code:
    Code:
    <p>
    <?php
    $cl = setlocale(LC_ALL, 'it_IT');
    $cl = setlocale(LC_ALL, 0);
    echo $cl;
    echo '<br/>';
    echo strftime("%e %B %Y");
    ?>
    </p>
    result:
    Code:
    <p>
    C<br/>21 November 2020</p>
    I also added at the end:
    Code:
    echo join('<br/>', resourcebundle_locales(''));
    to check that the locale is actually supposed to be available

  2. #2
    alemoppo is offline AlterVista Staff
    Join Date
    Feb 2010
    Location
    IT
    Posts
    734

    Default

    I just tried:
    PHP Code:
    <?php
    setlocale
    (LC_ALL, 'it_IT');
    echo
    strftime("%e %B %Y");
    ?>
    And here is the result.

    Can you try only this code?

    (if you are italian, you can use the italian forum).

    Bye!

  3. #3
    jellby is offline Junior Member
    Join Date
    Nov 2014
    Posts
    9

    Default

    Hmm... Must be something in the configuration. I created setlocale/index.php with exactly your code and nothing else, and I get: https://jellby.altervista.org/setlocale/

    Have you tried with different locales (in case your account defaults to Italian or something)?

    (I'm not Italian, although I can read it most of the time.)
    laravista likes this.

  4. #4
    alemoppo is offline AlterVista Staff
    Join Date
    Feb 2010
    Location
    IT
    Posts
    734

  5. #5
    jellby is offline Junior Member
    Join Date
    Nov 2014
    Posts
    9

    Default

    Thanks, but that only fixes it for Italian, and I'd like to use other languages too, not necessarily limited to:

    Code:
    <?php
    setlocale(LC_ALL, 'it_IT');
    echo strftime("%A, %e %B %Y<br/>");
    setlocale(LC_ALL, 'de_DE');
    echo strftime("%A, %e %B %Y<br/>");
    setlocale(LC_ALL, 'fr_FR');
    echo strftime("%A, %e %B %Y<br/>");
    setlocale(LC_ALL, 'es_ES');
    echo strftime("%A, %e %B %Y<br/>");
    ?>
    and all I get is:

    Code:
    marted�, 24 novembre 2020
    marted�, 24 novembre 2020
    marted�, 24 novembre 2020
    marted�, 24 novembre 2020
    (nevermind about the encoding)

    Can we get a more complete set of locales (those reported by resourcebundle_locales, maybe)? Is that a per-account setting? How can I change it?

  6. #6
    jellby is offline Junior Member
    Join Date
    Nov 2014
    Posts
    9

    Default

    No solution?

  7. #7
    alemoppo is offline AlterVista Staff
    Join Date
    Feb 2010
    Location
    IT
    Posts
    734

    Default

    I'm sorry but I don't understand the problem: the � characters are because of encoding, did you solve it?
    For a set of locales, did you have some problems? You can use resourcebundle_locales() for a list, there's someting wrong?

    You can't change the settings about that.

    Bye!

  8. #8
    jellby is offline Junior Member
    Join Date
    Nov 2014
    Posts
    9

    Default

    The � characters are not a problem, that's just the encoding which I can solve, I just didn't for this sample file, which is plain text, no HTML wrapper.

    The problem is that I'm setting the locale to Italian, German, French, Spanish (see the code above), but I get all dates in Italian. It looks like only default (C, English) and Italian are available. I use resourcebundle_locales(), and I get a list of locales (see https://jellby.altervista.org/test.php), but they don't work. In this latter file I have:

    * Default (C): works fine, English names
    * Italian (it_IT.utf8): works fine, Italian names
    * German (de_DE): does nothing, Italian remains active
    * French (fr_FR): does nothing, Italian remains active

    Both de_DE and fr_FR are in the list from resourcebundle_locales().

  9. #9
    jellby is offline Junior Member
    Join Date
    Nov 2014
    Posts
    9

    Default

    Is the problem still unclear? Is it only happening on my account?

  10. #10
    alemoppo is offline AlterVista Staff
    Join Date
    Feb 2010
    Location
    IT
    Posts
    734

    Default

    This code:
    PHP Code:
    <?php
    setlocale
    (LC_ALL, 'de_DE');
    echo
    strftime("%e %B %Y");
    ?>
    Produces this:
    5 Dezember 2020
    So it's correct.

    Can you try my code or post your code please?

    Bye!
    Last edited by alemoppo; 12-06-2020 at 12:01 AM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

SEO by vBSEO