1
|
|
|
<?php namespace Limoncello\Application\Packages\L10n; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Application\Packages\L10n\L10nSettings as S; |
20
|
|
|
use Limoncello\Contracts\Application\ContainerConfiguratorInterface; |
21
|
|
|
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; |
22
|
|
|
use Limoncello\Contracts\L10n\FormatterFactoryInterface; |
23
|
|
|
use Limoncello\Contracts\L10n\FormatterInterface; |
24
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
25
|
|
|
use Limoncello\l10n\Format\Formatter; |
26
|
|
|
use Limoncello\l10n\Format\Translator; |
27
|
|
|
use Limoncello\l10n\Messages\BundleStorage; |
28
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @package Limoncello\Application |
32
|
|
|
*/ |
33
|
|
|
class L10nContainerConfigurator implements ContainerConfiguratorInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
public static function configureContainer(LimoncelloContainerInterface $container): void |
39
|
|
|
{ |
40
|
|
|
$container[FormatterFactoryInterface::class] = function (PsrContainerInterface $container) { |
41
|
|
|
$settingsProvider = $container->get(SettingsProviderInterface::class); |
42
|
|
|
$settings = $settingsProvider->get(S::class); |
43
|
|
|
|
44
|
|
|
$defaultLocale = $settings[S::KEY_DEFAULT_LOCALE]; |
45
|
|
|
$storageData = $settings[S::KEY_LOCALES_DATA]; |
46
|
|
|
|
47
|
|
|
$factory = new class ($defaultLocale, $storageData) implements FormatterFactoryInterface |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $defaultLocale; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $storageData; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $defaultLocale |
61
|
|
|
* @param array $storageData |
62
|
|
|
*/ |
63
|
|
|
public function __construct(string $defaultLocale, array $storageData) |
64
|
|
|
{ |
65
|
|
|
$this->defaultLocale = $defaultLocale; |
66
|
|
|
$this->storageData = $storageData; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
|
|
public function createFormatter(string $namespace): FormatterInterface |
73
|
|
|
{ |
74
|
|
|
return $this->createFormatterForLocale($namespace, $this->defaultLocale); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
|
|
public function createFormatterForLocale(string $namespace, string $locale): FormatterInterface |
81
|
|
|
{ |
82
|
|
|
$translator = new Translator(new BundleStorage($this->storageData)); |
83
|
|
|
$formatter = new Formatter($locale, $namespace, $translator); |
84
|
|
|
|
85
|
|
|
return $formatter; |
86
|
|
|
} |
87
|
|
|
}; |
88
|
|
|
|
89
|
|
|
return $factory; |
90
|
|
|
}; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|