This repository has been archived by the owner on May 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranslatorModule.php
50 lines (40 loc) · 1.54 KB
/
TranslatorModule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Grout\Cyantree\TranslatorModule;
use Cyantree\Grout\App\Module;
use Grout\Cyantree\TranslatorModule\Types\TranslatorConfig;
use Zend\Cache\Storage\Adapter\Filesystem;
use Zend\Cache\Storage\Adapter\FilesystemOptions;
use Zend\Cache\Storage\Plugin\Serializer;
use Zend\I18n\Translator\Translator;
class TranslatorModule extends Module
{
/** @var Translator */
public $translator;
/** @var TranslatorConfig */
public $moduleConfig;
public function init()
{
$this->app->configs->setDefaultConfig($this->id, new TranslatorConfig(), $this);
$this->translator = $translator = new Translator();
/** @var TranslatorConfig $config */
$this->moduleConfig = $config = $this->app->configs->getConfig($this->id);
if(!$this->app->getConfig()->developmentMode){
$c = new Filesystem();
$o = new FilesystemOptions();
$o->setCacheDir($this->app->cacheStorage->createStorage($this->id));
$c->setOptions($o);
$c->addPlugin(new Serializer());
$translator->setCache($c);
}
$translator->setLocale($config->defaultLanguage);
$folder = $this->app->parseUri($config->translationsDirectory);
foreach($config->contexts as $context => $file){
if(is_int($context)){
$context = $file;
$file .= '.mo';
}
$translator->addTranslationFilePattern('gettext', $folder, '%s/'.$file, $context);
}
$this->translator = $translator;
}
}