|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2018 Justin Kuenzel (jukusoft.com) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Project: JuKuCMS |
|
22
|
|
|
* License: Apache 2.0 license |
|
23
|
|
|
* User: Justin |
|
24
|
|
|
* Date: 07.04.2018 |
|
25
|
|
|
* Time: 12:18 |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
class Translator { |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static $backend = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* translate a string |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $key message to translate |
|
39
|
|
|
* @param string $domain domain where to search key (optional) |
|
40
|
|
|
* @param array $params array with params to replace in translated message |
|
41
|
|
|
* |
|
42
|
|
|
* @throws IllegalStateException if settings key "translator_class_name" is not set |
|
43
|
|
|
* @see Translator_Backend::translate() |
|
44
|
|
|
* @since 0.1.0 |
|
45
|
|
|
* |
|
46
|
|
|
* @return string translated message |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function translate (string $key, string $domain = "", array $params = array()) : string { |
|
49
|
|
|
return self::getBackend()->translate($key, $domain, $params); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* translate a string, plural version of translate() |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $key message to translate |
|
56
|
|
|
* @param string $domain domain where to search key (optional) |
|
57
|
|
|
* @param array $params array with params to replace in translated message |
|
58
|
|
|
* |
|
59
|
|
|
* @throws IllegalStateException if settings key "translator_class_name" is not set |
|
60
|
|
|
* @see Translator_Backend::n_translate() |
|
61
|
|
|
* @since 0.1.0 |
|
62
|
|
|
* |
|
63
|
|
|
* @return string translated message |
|
64
|
|
|
*/ |
|
65
|
|
|
public function n_translate (string $key, string $plural_key, int $n, string $domain = "", array $params = array()) : string { |
|
66
|
|
|
return self::getBackend()->n_translate($key, $plural_key, $n, $domain, $params); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* get current instance of translator backend |
|
71
|
|
|
* |
|
72
|
|
|
* @see GetTextBackend |
|
73
|
|
|
* |
|
74
|
|
|
* @throws IllegalStateException if settings key "translator_class_name" is not set |
|
75
|
|
|
* |
|
76
|
|
|
* @return Translator_Backend instance of translator backend |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function &getBackend () : Translator_Backend { |
|
79
|
|
|
if (self::$backend == null) { |
|
80
|
|
|
//get translator backend |
|
81
|
|
|
$class_name = Settings::get("translator_class_name", "GetTextBackend"); |
|
82
|
|
|
|
|
83
|
|
|
self::$backend = new $class_name(); |
|
84
|
|
|
|
|
85
|
|
|
//initialize backend with current language |
|
86
|
|
|
self::$backend->init(Registry::singleton()->getSetting("lang_token")); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return self::$backend; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
?> |
|
|
|
|
|
|
95
|
|
|
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.