Skip to content

document setlocale(LC_ALL, null) to retrieve current setting #4765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2025

Conversation

remicollet
Copy link
Member

Document how it works (since always)

ping @nielsdos as related to php/php-src#19070

<para>
<example>
<title><function>setlocale</function> Retrieve current setting</title>
<programlisting role="php" annotations="non-interactive">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually non-interactive?

Copy link
Member Author

@remicollet remicollet Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know (1st time I see this attribute)
This is a copy-paste of the other example ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is for enabling running the example code on the docs website. But okay, if the other does not have it, it likely has a reason.

@nielsdos
Copy link
Member

Unfortunately, it seems more complicated than that.

A locale of "0" maps to NULL in the C code:
https://github.com/php/php-src/blob/c4183fba003a8601fcdf4d9530fa2f38e3e0c100/ext/standard/string.c#L4850-L4852

This means that setlocale(..., "0") in PHP does setlocale(..., NULL) in C.
NULL actually means returning the current locale without changing it. So the docs description was correct.

Passing NULL in PHP transforms it to the string "" in the C code, i.e. setlocale(..., NULL) in PHP does setlocale(..., "") in C, which means setting the locale to the user-preferred locale.

The difference between C/PHP should be cleared up in this page.

@remicollet remicollet merged commit 9ff8b11 into php:master Jul 11, 2025
2 checks passed
@remicollet remicollet deleted the issue-setlocale branch July 11, 2025 04:43
@remicollet
Copy link
Member Author

remicollet commented Jul 11, 2025

Sorry, I merged the fix before seing your last comment.

This means that setlocale(..., "0") in PHP does setlocale(..., NULL) in C.

Yes, I noticed this part of the code, But this doesn't work

$ sapi/cli/php -r 'var_dump(setlocale(LC_ALL, null));'
string(11) "fr_FR.UTF-8"

$ sapi/cli/php -r 'var_dump(setlocale(LC_ALL, "0"));'
string(166) "LC_CTYPE=C.UTF-8;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C"

Only null (or empty string) return the current setting

@remicollet
Copy link
Member Author

remicollet commented Jul 11, 2025

glibc documentation seems terribly confusing

If locale is NULL, the current locale is only queried, not modified.

If locale is an empty string, "", each part of the locale that should be modified is set according to the environment variables.

And the example in https://sourceware.org/glibc/manual/latest/html_node/Setting-the-Locale.html

  /* Get the name of the current locale.  */
  old_locale = setlocale (LC_ALL, NULL);

Edit: understood

In C: null return the current setting from a previous call, empty string set and return setting (from env)



$ sapi/cli/php -r 'var_dump(setlocale(LC_ALL, "0"),setlocale(LC_ALL, null),setlocale(LC_ALL, "0"),setlocale(LC_ALL, "de_DE"),setlocale(LC_ALL, "0"));'
string(166) "LC_CTYPE=C.UTF-8;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C"   <=  PHP default value
string(11) "fr_FR.UTF-8"    <= set from env
string(11) "fr_FR.UTF-8"
string(5) "de_DE"    <= set from arg
string(5) "de_DE"

@remicollet
Copy link
Member Author

The difference between C/PHP should be cleared up in this page.

See PR #4770

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants