International PHP Conference Berlin 2023

The ResourceBundle class

(PHP 5 >= 5.3.2, PHP 7, PHP 8, PECL intl >= 2.0.0)


Localized software products often require sets of data that are to be customized depending on current locale, e.g.: messages, labels, formatting patterns. ICU resource mechanism allows to define sets of resources that the application can load on locale basis, while accessing them in unified locale-independent fashion.

This class implements access to ICU resource data files. These files are binary data arrays which ICU uses to store the localized data.

ICU resource bundle can hold simple resources and complex resources. Complex resources are containers which can be either integer-indexed or string-indexed (just like PHP arrays). Simple resources can be of the following types: string, integer, binary data field or integer array.

ResourceBundle supports direct access to the data through array access pattern and iteration via foreach, as well as access via class methods. The result will be PHP value for simple resources and ResourceBundle object for complex ones. All resources are read-only.

Sinopse da classe

class ResourceBundle implements IteratorAggregate, Countable {
/* Métodos */
public __construct(string $locale, string $bundlename, bool $fallback = ?)
public count(): int
resourcebundle_count(ResourceBundle $r): int
public static create(string $locale, string $bundlename, bool $fallback = ?): ResourceBundle
resourcebundle_create(string $locale, string $bundlename, bool $fallback = ?): ResourceBundle
public getErrorCode(): int
resourcebundle_get_error_code(ResourceBundle $r): int
public getErrorMessage(): string
resourcebundle_get_error_message(ResourceBundle $r): string
public get(string|int $index): mixed
resourcebundle_get(ResourceBundle $r, string|int $index): mixed
public getLocales(string $bundlename): array
resourcebundle_locales(string $bundlename): array


Versão Descrição
8.0.0 ResourceBundle implements IteratorAggregate now. Previously, Traversable was implemented instead.
7.4.0 ResourceBundle implements Countable now.


add a note

User Contributed Notes 1 note

maiseralves at gmail dot com
11 years ago
* Struct of a Resource Bundle file
* file root.txt
* root:table {
*       usage:string { "Usage: genrb [Options] files" }
*       version:int { 122 }
*       errorcodes:array {
*           :string { "Invalid argument" }
*           :string { "File not found" }
*        }
*   }
* use: $genrb root.txt to generate resource bundle file (root.res)

//recursive function to list a resource bundle file structure using a ResourceBundle Object ( ) reference
function t($rb) {
$rb as $k => $v) {
is_object($v)) {
        } else {
var_dump($k . " " . $v);
//open root.res from folder locale
$rb = new ResourceBundle('root', "./locale");

t($rb);//call the function

/* The output from root table is
*   |- string(34) "usage Usage: genrb [Options] files"
*   |- string(11) "version 122"
*   |- ResourceBundle Object ( ) string(10) "errorcodes"
*         |- string(18) "0 Invalid argument"
*         |- string(16) "1 File not found"
To Top