Passed
Pull Request — master (#332)
by Arman
02:44
created

Translator::get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 4
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.9
13
 */
14
15
namespace Quantum\Libraries\Lang;
16
17
use Quantum\Libraries\Lang\Exceptions\LangException;
18
use Quantum\Config\Exceptions\ConfigException;
19
use Quantum\App\Exceptions\BaseException;
20
use Quantum\Di\Exceptions\DiException;
21
use Dflydev\DotAccessData\Data;
22
use Quantum\Loader\Loader;
23
use Quantum\Loader\Setup;
24
use ReflectionException;
25
use Quantum\Di\Di;
26
27
/**
28
 * Class Translator
29
 * @package Quantum\Libraries\Lang
30
 */
31
class Translator
32
{
33
34
    protected $lang;
35
    
36
    /**
37
     * @var Data|null
38
     */
39
    private $translations = null;
40
41
    /**
42
     * @param string $lang
43
     */
44
    public function __construct(string $lang)
45
    {
46
        $this->lang = $lang;
47
    }
48
49
    /**
50
     * Load translation file
51
     * @throws BaseException
52
     * @throws ConfigException
53
     * @throws DiException
54
     * @throws LangException
55
     * @throws ReflectionException
56
     */
57
    public function loadTranslations(): void
58
    {
59
60
        if ($this->translations !== null) {
61
            return;
62
        }
63
64
        $langDir = modules_dir() . DS . current_module() . DS . 'resources' . DS . 'lang' . DS . $this->lang;
65
        $files = fs()->glob($langDir . DS . "*.php");
66
67
        if (is_array($files) && !count($files)) {
68
            $langDir = base_dir() . DS . 'shared' . DS . 'resources' . DS . 'lang' . DS . $this->lang;
69
            $files = fs()->glob($langDir . DS . "*.php");
70
71
            if (is_array($files) && !count($files)) {
72
                throw LangException::translationsNotFound();
73
            }
74
        }
75
76
        $translations = [];
77
78
        foreach ($files as $file) {
79
            $fileName = fs()->fileName($file);
80
81
            $setup = new Setup();
82
            $setup->setPathPrefix('resources' . DS . 'lang' . DS . $this->lang);
83
            $setup->setFilename($fileName);
84
            $setup->setHierarchy(true);
85
86
            $translations[$fileName] = Di::get(Loader::class)->setup($setup)->load();
87
        }
88
89
        $this->translations = new Data();
90
        $this->translations->import($translations);
91
    }
92
93
    /**
94
     * Get translation by key
95
     * @param string $key
96
     * @param array|string|null $params
97
     * @return string
98
     */
99
    public function get(string $key, $params = null): string
100
    {
101
        if ($this->translations && $this->translations->has($key)) {
102
            $message = $this->translations->get($key);
103
            return $params ? _message($message, $params) : $message;
104
        }
105
106
        return $key;
107
    }
108
109
    /**
110
     * Reset translations
111
     */
112
    public function flush(): void
113
    {
114
        $this->translations = null;
115
    }
116
}