Completed
Pull Request — 2.x (#27)
by
unknown
01:55
created

Translator::translate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 7
nc 4
nop 2
crap 3
1
<?php
2
/**
3
 *
4
 * This file is part of the Aura Project for PHP.
5
 *
6
 * @package Aura.Intl
7
 *
8
 * @license http://opensource.org/licenses/MIT MIT
9
 *
10
 */
11
namespace Aura\Intl;
12
13
/**
14
 *
15
 * Translator to translate the message
16
 *
17
 * @package Aura.Intl
18
 *
19
 */
20
class Translator implements TranslatorInterface
21
{
22
    /**
23
     *
24
     * A fallback translator.
25
     *
26
     * @var TranslatorInterface
27
     *
28
     */
29
    protected $fallback;
30
31
    /**
32
     *
33
     * The formatter to use when translating messages.
34
     *
35
     * @var FormatterInterface
36
     *
37
     */
38
    protected $formatter;
39
40
    /**
41
     *
42
     * The locale being used for translations.
43
     *
44
     * @var string
45
     *
46
     */
47
    protected $locale;
48
49
    /**
50
     * The Package containing keys and translations.
51
     *
52
     * @var Package
53
     *
54
     */
55
    protected $package;
56
57
    /**
58
     *
59
     * Constructor
60
     *
61
     * @param string $locale The locale being used.
62
     *
63
     * @param Package $package The Package containing keys and translations.
64
     *
65
     * @param FormatterInterface $formatter A message formatter.
66
     *
67
     * @param TranslatorInterface $fallback A fallback translator.
68
     *
69
     */
70 5
    public function __construct(
71
        $locale,
72
        Package $package,
73
        FormatterInterface $formatter,
74
        TranslatorInterface $fallback = null
75
    ) {
76 5
        $this->locale    = $locale;
77 5
        $this->package   = $package;
78 5
        $this->formatter = $formatter;
79 5
        $this->fallback  = $fallback;
80 5
    }
81
82
    /**
83
     *
84
     * Gets the message translation by its key.
85
     *
86
     * @param string $key The message key.
87
     *
88
     * @return mixed The message translation string, or false if not found.
89
     *
90
     */
91 4
    protected function getMessage($key)
92
    {
93 4
        if ($message = ($this->package->getMessage($key))) {
94 3
            return $message;
95
        }
96
97 3
        if ($this->fallback) {
98
            // get the message from the fallback translator
99 1
            $message = $this->fallback->getMessage($key);
100 1
            if ($message) {
101
                // speed optimization: retain locally
102 1
                $this->package->addMessage($key, $message);
103
                // done!
104 1
                return $message;
105
            }
106
        }
107
108
        // no local message, no fallback
109 2
        return false;
110
    }
111
112
    /**
113
     *
114
     * Translates the message indicated by they key, replacing token values
115
     * along the way.
116
     *
117
     * @param string $key The message key.
118
     *
119
     * @param array $tokens_values Token values to interpolate into the
120
     * message.
121
     *
122
     * @return string The translated message with tokens replaced.
123
     *
124
     */
125 4
    public function translate($key, array $tokens_values = [])
126
    {
127
        // get the message string
128 4
        $message = $this->getMessage($key);
129
130
        // do we have a message string?
131 4
        if (! $message) {
132
            // no, return the message key as-is
133 2
            $message = $key;
134 2
        }
135
136
        // are there token replacement values?
137 4
        if (! $tokens_values) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tokens_values of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
138
            // no, return the message string as-is
139 3
            return $message;
140
        }
141
142
        // run message string through formatter to replace tokens with values
143 2
        return $this->formatter->format($this->locale, $message, $tokens_values);
144
    }
145
146
    /**
147
     *
148
     * @return Package
149
     *
150
     */
151
    public function getPackage()
152
    {
153
        return $this->package;
154
    }
155
}
156