Test Failed
Push — master ( c19a70...4a5f5f )
by Sebastian
05:20
created

ptex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * File containing the global translation functions.
4
 *
5
 * @package AppLocalize
6
 * @see \AppLocalize\t()
7
 * @see \AppLocalize\pt()
8
 * @see \AppLocalize\pts()
9
 */
10
11
declare(strict_types=1);
12
13
namespace AppLocalize;
14
15
/**
16
 * Translates the specified string by looking up the
17
 * translations table. Returns the translated string
18
 * according to the current application locale.
19
 *
20
 * Not to confound with content locales! This function
21
 * serves only for translations within the UI itself,
22
 * not user contents.
23
 *
24
 * If no translation is found, returns the original string.
25
 *
26
 * Use the sister function {@link pt()} to translate
27
 * and echo a string directly.
28
 *
29
 * @package AppLocalize
30
 * @param string $text
31
 * @param mixed ...$args
32
 * @return string
33
 * @see \AppLocalize\pt()
34
 */
35
function t(string $text, ...$args) : string
36
{
37
    return call_user_func(
38
        array(Localization::getTranslator(), 'translate'),
39
        $text,
40
        $args
41
    );
42
}
43
44
/**
45
 * Same as the {@link t()} function, but echos the
46
 * translated string.
47
 *
48
 * @see \AppLocalize\t()
49
 */
50
function pt(string $text, ...$args) : void
51
{
52
    echo call_user_func(
53
        array(Localization::getTranslator(), 'translate'),
54
        $text,
55
        $args
56
    );
57
}
58
59
/**
60
 * Same as the {@link pt()} function, but adds a space after
61
 * the string, so several texts can be chained in an HTML
62
 * document, without having to manually add spaces in between.
63
 *
64
 * @param string $text
65
 * @param mixed ...$args
66
 * @see \AppLocalize\pt()
67
 */
68
function pts(string $text, ...$args) : void
69
{
70
    echo call_user_func(
71
        array(Localization::getTranslator(), 'translate'),
72
        $text,
73
        $args
74
    );
75
    
76
    echo ' ';
77
}
78
79
/**
80
 * Like {@see \AppLocalize\t()}, but the second parameter
81
 * allows specifying context for translators, in cases
82
 * where a text to translate can be ambiguous, or to
83
 * explain the content of placeholders.
84
 *
85
 * @param string $text
86
 * @param string $context
87
 * @param ...$args
88
 * @return string
89
 */
90
function tex(string $text, string $context, ...$args) : string
91
{
92
    unset($context); // Only used by the parser.
93
94
    return call_user_func(
95
        array(Localization::getTranslator(), 'translate'),
96
        $text,
97
        $args
98
    );
99
}
100
101
function ptex(string $text, string $context, ...$args) : void
102
{
103
    unset($context); // Only used by the parser.
104
105
    echo call_user_func(
106
        array(Localization::getTranslator(), 'translate'),
107
        $text,
108
        $args
109
    );
110
}
111
112
function ptexs(string $text, string $context, ...$args) : void
113
{
114
    unset($context); // Only used by the parser.
115
116
    echo call_user_func(
117
        array(Localization::getTranslator(), 'translate'),
118
        $text,
119
        $args
120
    );
121
122
    echo ' ';
123
}
124