Doc #81383 [Com]: Locale::lookup / locale_lookup incorrectly returns null instead of default

From: Date: Fri, 27 Aug 2021 05:56:01 +0000
Subject: Doc #81383 [Com]: Locale::lookup / locale_lookup incorrectly returns null instead of default
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-19119@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81383&edit=1

 ID:                 81383
 Comment by:         adrien dot crivelli at gmail dot com
 Reported by:        adrien dot crivelli at gmail dot com
 Summary:            Locale::lookup / locale_lookup incorrectly returns
                     null instead of default
 Status:             Verified
 Type:               Documentation Problem
 Package:            intl
 Operating System:   Ubuntu 20.04.3 LTS
 PHP Version:        8.0.9
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2021-08-26 04:37:20] 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-26 02:16:03] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=81383&edit=1


Thread (3 messages)

« previous php.doc.bugs (#19119) next »