php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #81383 Locale::lookup / locale_lookup incorrectly returns null instead of default
Submitted: 2021-08-26 02:16 UTC Modified: 2021-08-26 04:37 UTC
From: adrien dot crivelli at gmail dot com Assigned:
Status: Closed Package: intl (PECL)
PHP Version: 8.0.9 OS: Ubuntu 20.04.3 LTS
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: adrien dot crivelli at gmail dot com
New email:
PHP Version: OS:

 

 [2021-08-26 02:16 UTC] adrien dot crivelli at gmail dot com
Description:
------------
According to official documentation on https://www.php.net/manual/en/locale.lookup.php, Locale::lookup will return the given $default if no match is found. This usually works as expected, unless if the given $locale is 157 characters long, or more. Then the $default seems entirely ignored.

I expect that Locale::lookup will always return the $default, whatever the length of $locale might be.

PHP versions were the bug were confirmed:

```
$ php8.0 --version
PHP 8.0.9 (cli) (built: Jul 30 2021 13:03:39) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.9, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.9, Copyright (c), by Zend Technologies
    with Xdebug v3.0.4, Copyright (c) 2002-2021, by Derick Rethans
```

```
$ php7.4  --version
PHP 7.4.22 (cli) (built: Jul 30 2021 13:08:17) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.22, Copyright (c), by Zend Technologies
    with Xdebug v3.0.4, Copyright (c) 2002-2021, by Derick Rethans
```

```
$ php7.2  --version
PHP 7.2.34-23+ubuntu20.04.1+deb.sury.org+1 (cli) (built: Jul  1 2021 16:07:07) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.34-23+ubuntu20.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v3.0.4, Copyright (c) 2002-2021, by Derick Rethans
```

Extract of `php8.0 -i` that might be relevant:

```
intl

Internationalization support => enabled
ICU version => 66.1
ICU Data version => 66.1
ICU Unicode version => 13.0

Directive => Local Value => Master Value
intl.default_locale => no value => no value
intl.error_level => 0 => 0
intl.use_exceptions => Off => Off
```

Test script:
---------------
<?php

// Expect to see "fr", but get null
var_dump(Locale::lookup(["fr"], str_repeat("a", 157), false, "fr"));

Expected result:
----------------
I expect to get the given default locale of "fr"

Actual result:
--------------
I incorrectly get null

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-26 04:37 UTC] requinix@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem -Package: *Languages/Translation +Package: intl
 [2021-08-26 04:37 UTC] requinix@php.net
The function returns NULL as is convention when an invalid argument is passed. Here, the problem is that the locale is too long - 156 characters is the limit, as defined by the ICU library that supports PHP's intl extension.

Unlike most functions in PHP, the error is not reported immediately but stored and accessible through the intl_get_error_code/message functions.
https://www.php.net/manual/en/ref.intl.php

https://3v4l.org/049Fo

I don't see anything in the intl documentation that speaks to this behavior. Documenting it for every function could be a lot of work so perhaps a single "Error Handling"-type page in the intl section could explain instead.
 [2021-08-27 05:56 UTC] adrien dot crivelli at gmail dot com
I created https://github.com/php/doc-en/pull/883 to solve this, since it actually is not a bug in code, but rather a documentation issue about return types.
 [2021-08-27 12:42 UTC] git@php.net
Automatic comment on behalf of PowerKiKi (author) and cmb69 (committer)
Revision: https://github.com/php/doc-en/commit/235297daeda99773f68ee59e2efff048e98c1dc5
Log: Fix #81383: Locale::lookup / locale_lookup incorrectly returns null instead of default
 [2021-08-27 12:42 UTC] git@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC