Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
LanguageTranslator | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
getInstance | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setJsonFileLoader | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
setPoFileLoader | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace BO\Slim; |
4 | |
5 | use Psr\Http\Message\RequestInterface; |
6 | // Symfony Translation Classes |
7 | use Symfony\Component\Translation\Loader\JsonFileLoader; |
8 | use Symfony\Component\Translation\Loader\PoFileLoader; |
9 | use Symfony\Component\Translation\Translator; |
10 | |
11 | class LanguageTranslator |
12 | { |
13 | protected $translator = null; |
14 | |
15 | protected $defaultLang; |
16 | |
17 | protected $loaderTypes = [ |
18 | 'pofile' => 'setPoFileLoader', |
19 | 'json' => 'setJsonFileLoader' |
20 | ]; |
21 | |
22 | public function __construct($fallbackLocale, $defaultLocale, $defaultLang) |
23 | { |
24 | $translatorType = (\App::$languagesource) ? \App::$languagesource : 'pofile'; |
25 | |
26 | $this->defaultLang = $defaultLang; |
27 | |
28 | $translatorClass = \APP::TRANSLATOR_CLASS; |
29 | // First param is the "default language" to use. |
30 | $this->translator = new $translatorClass($defaultLocale); |
31 | // Set a fallback language incase you don't have a translation in the default language |
32 | $this->translator->setFallbackLocales([$fallbackLocale]); |
33 | // Add a loader that will get the php files we are going to store our translations in |
34 | $initLoader = $this->loaderTypes[$translatorType]; |
35 | $this->$initLoader(); |
36 | } |
37 | |
38 | public function getInstance() |
39 | { |
40 | return $this->translator; |
41 | } |
42 | |
43 | protected function setJsonFileLoader() |
44 | { |
45 | $this->translator->addLoader('json', new JsonFileLoader()); |
46 | foreach (\App::$supportedLanguages as $language) { |
47 | $this->translator->addResource( |
48 | 'json', |
49 | \App::APP_PATH . '/lang/' . $language['locale'] . '.json', |
50 | $language['locale'] |
51 | ); |
52 | } |
53 | } |
54 | |
55 | protected function setPoFileLoader() |
56 | { |
57 | $this->translator->addLoader('pofile', new PoFileLoader()); |
58 | foreach (\App::$supportedLanguages as $locale => $language) { |
59 | if ($locale != $this->defaultLang) { |
60 | $this->translator->addResource( |
61 | 'pofile', |
62 | \App::APP_PATH . '/lang/' . $language['locale'] . '.po', |
63 | $language['locale'] |
64 | ); |
65 | } |
66 | } |
67 | } |
68 | } |