ResourceBundle example


class IntlBundleReader implements BundleReaderInterface
{
    public function read(string $path, string $locale): mixed
    {
        // Point for future extension: Modify this class so that it works also         // if the \ResourceBundle class is not available.         try {
            // Never enable fallback. We want to know if a bundle cannot be found             $bundle = new \ResourceBundle($locale$path, false);
        } catch (\Exception) {
            $bundle = null;
        }

        // The bundle is NULL if the path does not look like a resource bundle         // (i.e. contain a bunch of *.res files)         if (null === $bundle) {
            throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s/%s.res" could not be found.', $path$locale));
        }

        // Other possible errors are U_USING_FALLBACK_WARNING and U_ZERO_ERROR,

        ]);

        $this->assertFileEquals(__DIR__.'/Fixtures/en.json', $this->directory.'/en.json');
    }

    /** * @requires extension intl */
    public function testWriteResourceBundle()
    {
        $bundle = new \ResourceBundle('rb', __DIR__.'/Fixtures', false);

        $this->writer->write($this->directory, 'en', $bundle);

        $this->assertFileEquals(__DIR__.'/Fixtures/rb.json', $this->directory.'/en.json');
    }
}
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
    {
        if (!stream_is_local($resource.'.dat')) {
            throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
        }

        if (!file_exists($resource.'.dat')) {
            throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
        }

        try {
            $rb = new \ResourceBundle($locale$resource);
        } catch (\Exception) {
            $rb = null;
        }

        if (!$rb) {
            throw new InvalidResourceException(sprintf('Cannot load resource "%s".', $resource));
        } elseif (intl_is_failure($rb->getErrorCode())) {
            throw new InvalidResourceException($rb->getErrorMessage()$rb->getErrorCode());
        }

        $messages = $this->flatten($rb);
        

        ]);

        $this->assertFileEquals(__DIR__.'/Fixtures/en.php', $this->directory.'/en.php');
    }

    /** * @requires extension intl */
    public function testWriteResourceBundle()
    {
        $bundle = new \ResourceBundle('rb', __DIR__.'/Fixtures', false);

        $this->writer->write($this->directory, 'en', $bundle);

        $this->assertFileEquals(__DIR__.'/Fixtures/rb.php', $this->directory.'/en.php');
    }
}
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
    {
        if (!stream_is_local($resource)) {
            throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
        }

        if (!is_dir($resource)) {
            throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
        }

        try {
            $rb = new \ResourceBundle($locale$resource);
        } catch (\Exception) {
            $rb = null;
        }

        if (!$rb) {
            throw new InvalidResourceException(sprintf('Cannot load resource "%s".', $resource));
        } elseif (intl_is_failure($rb->getErrorCode())) {
            throw new InvalidResourceException($rb->getErrorMessage()$rb->getErrorCode());
        }

        $messages = $this->flatten($rb);
        
Home | Imprint | This part of the site doesn't use cookies.