functions.php ➔ t()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 12
nop 4
dl 0
loc 33
ccs 0
cts 19
cp 0
crap 56
rs 8.4586
c 0
b 0
f 0
1
<?php
2
3
use Rocket\Translation\Support\Laravel5\Facade as I18N;
4
5
/**
6
 * Language related functions
7
 *
8
 * @author Stéphane Goetz
9
 */
10
11
/**
12
 * Translates a string and returns it
13
 *
14
 * @param string $string
15
 * @param array $args
16
 * @param string $context
17
 * @param string $language
18
 * @return string
19
 */
20
function t($string, array $args = [], $context = null, $language = 'default')
21
{
22
    if ($context === null) {
23
        $context = I18N::getContext();
24
    }
25
26
    //get translation
27
    $translated = I18N::translate($string, $context, $language);
28
29
    //if there are some arguments
30
    if (empty($args)) {
31
        return $translated;
32
    }
33
34
    // Transform arguments before inserting them.
35
    foreach ($args as $key => $value) {
36
        switch ($key[0]) {
37
            case '@':
38
                // Escaped only.
39
                $args[$key] = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
40
                break;
41
            case '%':
42
                // Escaped and placeholder.
43
                $args[$key] = '<em>' . $value . '</em>';
44
                break;
45
            case '!':
46
            default:
47
                //do nothing
48
        }
49
    }
50
51
    return strtr($translated, $args);
52
}
53
54
/**
55
 * Translates with the correct (singular or plural) form
56
 *
57
 * @param string $string
58
 * @param string $plural
59
 * @param array $args
60
 * @param string $context
61
 * @param string $language
62
 * @return string
63
 */
64
function tn($string, $plural, array $args = [], $context = 'default', $language = 'default')
65
{
66
    if ($args['#'] <= 1) {
67
        return t($string, $args, $context, $language);
68
    } else {
69
        return t($plural, $args, $context, $language);
70
    }
71
}
72