Translator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 79
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A translate() 0 8 2
A translateContext() 0 9 2
A translatePlural() 0 12 4
1
<?php namespace Fisharebest\Localization;
2
3
use Fisharebest\Localization\PluralRule\PluralRuleInterface;
4
5
/**
6
 * Class Translator - use a translation to translate messages.
7
 *
8
 * @author    Greg Roach <[email protected]>
9
 * @copyright (c) 2018 Greg Roach
10
 * @license   GPLv3+
11
 */
12
class Translator
13
{
14
    /** @var array An association of English -> translated messages */
15
    private $translations;
16
17
    /** @var PluralRuleInterface */
18
    private $plural_rule;
19
20
    /**
21
     * Create a translator
22
     *
23
     * @param array               $translations
24
     * @param PluralRuleInterface $plural_rule
25
     */
26
    public function __construct(array $translations, $plural_rule)
27
    {
28
        $this->translations = $translations;
29
        $this->plural_rule  = $plural_rule;
30
    }
31
32
    /**
33
     * Translate a message into another language.
34
     * Works the same as gettext().
35
     *
36
     * @param string $message English text to translate
37
     *
38
     * @return string Translated text
39
     */
40
    public function translate($message)
41
    {
42
        if (isset($this->translations[$message])) {
43
            return $this->translations[$message];
44
        } else {
45
            return $message;
46
        }
47
    }
48
49
    /**
50
     * Translate a context-sensitive message into another language.
51
     * Works the same as pgettext().
52
     *
53
     * @param string $context Context of the message, e.g. "verb" or "noun"
54
     * @param string $message English text to translate
55
     *
56
     * @return string Translated text
57
     */
58
    public function translateContext($context, $message)
59
    {
60
        $key = $context . chr(4) . $message;
61
        if (isset($this->translations[$key])) {
62
            return $this->translations[$key];
63
        } else {
64
            return $message;
65
        }
66
    }
67
68
    /**
69
     * Translate a plural message into another language.
70
     * Works the same as ngettext().
71
     *
72
     * @param string $message1 English text for singular
73
     * @param string $message2 English text for plural
74
     * @param int    $number   Number of entities
75
     *
76
     * @return string Translated text
77
     */
78
    public function translatePlural($message1, $message2, $number)
79
    {
80
        $key = $message1 . chr(0) . $message2;
81
        if (isset($this->translations[$key])) {
82
            $plurals = explode(chr(0), $this->translations[$key]);
83
            if (count($plurals) === $this->plural_rule->plurals()) {
84
                return $plurals[$this->plural_rule->plural($number)];
85
            }
86
        }
87
88
        return $number === 1 ? $message1 : $message2;
89
    }
90
}
91