php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63945 Fallback not used when calling ResourceBundle::get() on a nested table
Submitted: 2013-01-08 19:31 UTC Modified: 2021-11-11 11:16 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: nrawlings at imiassociates dot com Assigned:
Status: Open Package: I18N and L10N related
PHP Version: 5.4.10 OS: Mac OS X
Private report: No CVE-ID: None
 [2013-01-08 19:31 UTC] nrawlings at imiassociates dot com
Description:
------------
Locale inheritance is not used when calling ResourceBundle::get() to access a 
key 
in a table that is nested under the bundle's root table.

For example, suppose that the keys "username" and "password" are both defined in 
the "loginForm" table of the root bundle, but the "loginForm" table in the 
Spanish 
bundle only defines the "password" key, and the Italian bundle does not define 
the 
"loginForm" table at all.  If we do the following:

    $english = new ResourceBundle('en', '/path/to/resource/bundle');
    $italian = new ResourceBundle('it', '/path/to/resource/bundle');
    $spanish = new ResourceBundle('es', '/path/to/resource/bundle');

    echo $english->get('loginForm')->get('username');
    echo $italian->get('loginForm')->get('username');
    echo $spanish->get('loginForm')->get('username');

The English and Italian echo statements print the string stored in the 
"username" 
key of the "loginForm" table of the root bundle, as expected.  The Spanish one, 
however, prints nothing (ResourceBundle::get() actually returns NULL) instead of 
falling back to the root locale.  This behavior seems to be a result of the 
"loginForm" table being defined, but not containing a "username" entry.

This is not a bug per se, but a question of expected results.  The PHP and ICU 
documentation suggests that locale inheritance should take place regardless of 
how 
deeply the key being accessed is nested.  In practice, however, the Intl 
extension 
only allows for locale inheritance when accessing a top-level key.

Calling ures_getByKeyWithFallback() instead of ures_getByKey() 
(resourcebundle_class.c, line 181) should rectify this issue.

Test script:
---------------
// Given the following compiled into a .dat format resource bundle:
//
// root:table {
//     loginForm:table {
//         username:string {"Username"}
//         password:string {"Password"}
//     }
// }
// 
// es:table {
//     loginForm:table {
//         password:string {"Contraseña"}
//     }
// }

$rb = new ResourceBundle('es_MX', '/path/to/resource/bundle');
$loginStrings = $bundle->get('loginForm');
var_dump($loginStrings->get('username'));
var_dump($loginStrings->get('password'));


Expected result:
----------------
string(8) "Username"
string(11) "Contraseña"

Actual result:
--------------
NULL
string(11) "Contraseña"

Patches

intl-resourcebundle-get-locale-inheritance (last revision 2013-01-08 19:31 UTC by nrawlings at imiassociates dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-09 09:31 UTC] cataphract@php.net
ures_getByKeyWithFallback is not a public API, therefore I'd prefer not to use it unless there's a very compelling reason.

This problem is described in http://site.icu-project.org/design/resbund/issues . Since then 4 years have passed.
 [2021-11-11 11:16 UTC] nikic@php.net
-Package: intl +Package: I18N and L10N related
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC