| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Translation\Loader;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Config\Resource\FileResource;
|
|
|
15 |
use Symfony\Component\Translation\Exception\InvalidResourceException;
|
|
|
16 |
use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
|
|
17 |
use Symfony\Component\Translation\MessageCatalogue;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* IcuResFileLoader loads translations from a resource bundle.
|
|
|
21 |
*
|
|
|
22 |
* @author stealth35
|
|
|
23 |
*/
|
|
|
24 |
class IcuDatFileLoader extends IcuResFileLoader
|
|
|
25 |
{
|
|
|
26 |
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
|
|
|
27 |
{
|
|
|
28 |
if (!stream_is_local($resource.'.dat')) {
|
|
|
29 |
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
if (!file_exists($resource.'.dat')) {
|
|
|
33 |
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
try {
|
|
|
37 |
$rb = new \ResourceBundle($locale, $resource);
|
|
|
38 |
} catch (\Exception) {
|
|
|
39 |
$rb = null;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
if (!$rb) {
|
|
|
43 |
throw new InvalidResourceException(sprintf('Cannot load resource "%s".', $resource));
|
|
|
44 |
} elseif (intl_is_failure($rb->getErrorCode())) {
|
|
|
45 |
throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
$messages = $this->flatten($rb);
|
|
|
49 |
$catalogue = new MessageCatalogue($locale);
|
|
|
50 |
$catalogue->add($messages, $domain);
|
|
|
51 |
|
|
|
52 |
if (class_exists(FileResource::class)) {
|
|
|
53 |
$catalogue->addResource(new FileResource($resource.'.dat'));
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
return $catalogue;
|
|
|
57 |
}
|
|
|
58 |
}
|