Completed
Pull Request — master (#35)
by Michal
51:56 queued 48:38
created

Translator::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/**
4
 * Defines the localization helper infrastructure of the library.
5
 *
6
 * @package SqlParser
7
 */
8
namespace SqlParser;
9
10
use MoTranslator;
11
12
class Translator
13
{
14
    /**
15
     * Translator instance
16
     *
17
     * @access private
18
     * @static
19
     * @var Translator
20
     */
21
    private static $_instance;
22
23
    /**
24
     * The MoTranslator loader object.
25
     *
26
     * @var MoTranslator\Loader
27
     */
28
    private $loader;
29
30
    /**
31
     * The MoTranslator translator object.
32
     *
33
     * @var MoTranslator\Translator
34
     */
35
    private $translator;
36
37
    /**
38
     * Constructor.
39
     */
40
    public function __construct()
41
    {
42
        // Create loader object
43
        $this->loader = new MoTranslator\Loader();
44
45
        // Set locale
46
        $this->loader->setlocale(
47
            $this->loader->detectlocale()
0 ignored issues
show
Bug introduced by
The method detectlocale() does not seem to exist on object<MoTranslator\Loader>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
        );
49
50
        // Set default text domain
51
        $this->loader->textdomain('sqlparser');
52
53
        // Set path where to look for a domain
54
        $this->loader->bindtextdomain('sqlparser', __DIR__ . '/../locale/');
55
56
        // Get translator
57
        $this->translator = $this->loader->get_translator();
58
    }
59
60
    /**
61
     * Translates a string
62
     *
63
     * @param string $msgid String to be translated
64
     *
65
     * @return string translated string (or original, if not found)
66
     */
67
    public function gettext($msgid)
68
    {
69
        return $this->translator->gettext($msgid);
70
    }
71
72
    /**
73
     * Returns the singleton Translator object
74
     *
75
     * @return Translator object
76
     */
77
    public static function getInstance()
78
    {
79
        if (empty(self::$_instance)) {
80
            self::$_instance = new Translator();
81
        }
82
        return self::$_instance;
83
    }
84
}
85