-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathModule.php
More file actions
73 lines (57 loc) · 2.6 KB
/
Module.php
File metadata and controls
73 lines (57 loc) · 2.6 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace verbb\base\base;
use Craft;
use craft\events\RegisterTemplateRootsEvent;
use craft\helpers\ArrayHelper;
use craft\i18n\PhpMessageSource;
use craft\web\View;
use yii\base\Event;
use yii\base\Module as YiiModule;
class Module extends YiiModule
{
// Properties
// =========================================================================
public ?string $t9nCategory = null;
public string $sourceLanguage = 'en-AU';
// Public Methods
// =========================================================================
public function __construct($id, $parent = null, array $config = [])
{
$this->t9nCategory = ArrayHelper::remove($config, 't9nCategory', $this->t9nCategory ?? $id);
$this->sourceLanguage = ArrayHelper::remove($config, 'sourceLanguage', $this->sourceLanguage);
if (($basePath = ArrayHelper::remove($config, 'basePath')) !== null) {
$this->setBasePath($basePath);
}
// Translation category
$i18n = Craft::$app->getI18n();
if (!isset($i18n->translations[$this->t9nCategory]) && !isset($i18n->translations[$this->t9nCategory . '*'])) {
$i18n->translations[$this->t9nCategory] = [
'class' => PhpMessageSource::class,
'sourceLanguage' => $this->sourceLanguage,
'basePath' => $this->getBasePath() . DIRECTORY_SEPARATOR . 'translations',
'forceTranslation' => true,
'allowOverrides' => true,
];
}
// Base template directory
Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function(RegisterTemplateRootsEvent $event) {
if (is_dir($baseDir = $this->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) {
$event->roots[$this->id] = $baseDir;
}
});
// Set this as the global instance of this plugin class
static::setInstance($this);
// Set the default controller namespace
if ($this->controllerNamespace === null && ($pos = strrpos(static::class, '\\')) !== false) {
$namespace = substr(static::class, 0, $pos);
if (Craft::$app->getRequest()->getIsConsoleRequest()) {
$this->controllerNamespace = $namespace . '\\console\\controllers';
} else {
$this->controllerNamespace = $namespace . '\\controllers';
}
// Define a custom alias named after the namespace
Craft::setAlias('@' . str_replace('\\', '/', $namespace), $this->getBasePath());
}
parent::__construct($id, $parent, $config);
}
}