Problem with setlocale on the web server

Asked

Viewed 344 times

-1

I have an online site that is set the time zone for America/Sao_paulo and using setlocale for Portuguese, but even so the site where I have functions strtime, give the text in English, I tried to put in the code the function set time zone but it still didn’t work. Someone can help me. I don’t know if it’s a common problem or not, ps: no phpinfo() esta setado para são paulo.

  • the date is displayed only with numbers? ex: 14/03/2019

  • do not display texts in English, as ex(Thursday)

  • If I’m not mistaken, date always returns the texts in English. The Timezone only serves to know the current date and time to be considered (since it varies according to the time zone), and the language has nothing to do with Timezone. If you want texts in another language, use strftime along with setlocale

  • I am using this setlocale setlocale {(LC_TIME, "English-Brazilian");} yet strtime ignores

  • I don’t know if PHP recognizes "Portuguese-Brazilian", it has to be "pt_BR". Also see if the locale is installed on the server: https://stackoverflow.com/q/10909911

  • 1
  • I will see if it is installed, but "Portuguese-Brazilian" works, because at least on the localhost it recognizes, I only have this problem in the hosting server

  • @Lipespry is not duplicated, I looked but this is not this problem

  • 1

    Solved, the server does not accept Portuguese-Brazilian, even in the php manual that accepts

  • 1

    In fact setlocale returns false when the locale is not recognized (and if I’m not mistaken, it shows no error message). And then the locale is not changed and it uses what was already configured before (and the localhost was probably Portuguese, and the English server). Check the return of setlocale just to be sure... and in the manual I only saw that you accept Portuguese_Brazilian, but only in Windows, in other systems is pt_BR same

Show 5 more comments

1 answer

1

"- the server does not accept Portuguese-Brazilian, even in the php manual that accepts"

Done some research on the setlocale, I can conclude that he has no direct connection to timezone or the version of your PHP server. But it is directly dependent on the operating system on which your PHP server is running!

As an example, my development environment, running Windows 10 and PHP through Uniserver Zero XIII, brought the following results:

// locale string atual:
echo setlocale(LC_TIME, 0); // C
echo setlocale(LC_ALL, 0); // LC_COLLATE=C;LC_CTYPE=Portuguese_Brazil.1252;LC_MONETARY=C;LC_NUMERIC=C;LC_TIME=C

setlocale(LC_ALL, 'portuguese-brazilian');
echo strftime('%A, %d de %B de %Y'); // domingo, 17 de mar�o de 2019

setlocale(LC_ALL, 'portuguese-brazilian.utf8');
echo strftime('%A, %d de %B de %Y'); // domingo, 17 de março de 2019

setlocale(LC_ALL, 'pt_BR');
echo strftime('%A, %d de %B de %Y'); // domingo, 17 de março de 2019

setlocale(LC_ALL, 'pt_BR.utf8');
echo strftime('%A, %d de %B de %Y'); // domingo, 17 de março de 2019

setlocale(LC_ALL, 'pt_BR.iso-8859-1');
echo strftime('%A, %d de %B de %Y'); // Sunday, 17 de March de 2019

setlocale(LC_ALL, 'portuguese');
echo strftime('%A, %d de %B de %Y'); // domingo, 17 de mar�o de 2019
  • Note: test one at a time, since it considers the last valid if not found!

  • HTML Charset: utf8


The PHP documentation itself recommends that Windows users search for local string (string locale) in the Microsoft MSDN website:

Tip Windows users found useful information about locale strings on Microsoft’s MSDN site. Supported language strings can be found » here the supported country/region strings » here. Windows systems support three-letter country/region codes specified by ISO 3166-Alpha-3, which can be found on Unicode website. - PHP: setlocale

But I found it more convenient through Table of Moodle site locations. That’s good: there’s no way to add new locations in Windows. Already on Linux it is possible to install new locations and some distributions already have all by default. Moodle, inclusive, explains this.

Another issue is that you can define several locale strings at once and PHP takes care of defining "what to find first".

setlocale(LC_ALL, "pt_BR", "pt_BR.iso-8859-1", "pt_BR.utf-8", "portuguese", ...);

Note: this does not mean that it will define the best!

Another recommendation is, if feasible, to always return to the default after displaying your date with the language in question. This will prevent possible incompatibilities related to several other PHP functions. See reports in User Contributed Notes in setlocale documentation.

Overall, if you have root access to your server, it won’t be hard to find a valid option. If not, you will have to surrender to the support of the company that hosts your site - which does not always meet expectations, where everything they don’t want to do is turned into "violates security policy".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.