Test Failed
Push — master ( 7c671f...3d53a5 )
by Justin
04:02 queued 18s
created

Translator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
dl 0
loc 91
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A n_translate() 0 2 1
A translate() 0 2 1
A getBackend() 0 12 2
B translateTitle() 0 27 6
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 static 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
	public static function translateTitle (string $token) {
70
		//translate title
71
		if (strpos($token, "lang_")) {
72
			$array1 = explode("_", $token);
73
74
			if (count($array1) == 2) {
75
				//translate
76
				$token = Translator::translate($array1[1]);
77
			} else if (count($array1) == 3) {
78
				//translate with domain
79
				$token = Translator::translate($array1[2], $array1[1]);
80
			} else if (count($array1) > 3) {
81
				$token_parts = array();
82
83
				for ($i = 2; $i < count($array1); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
84
					$token_parts[] = $array1[$i];
85
				}
86
87
				//generate final token
88
				$token = implode("_", $token_parts);
89
90
				//translate with domain
91
				$token = Translator::translate($token, $array1[1]);
92
			}
93
		}
94
95
		return $token;
96
	}
97
98
	/**
99
	 * get current instance of translator backend
100
	 *
101
	 * @see GetTextBackend
102
	 *
103
	 * @throws IllegalStateException if settings key "translator_class_name" is not set
104
	 *
105
	 * @return Translator_Backend instance of translator backend
106
	 */
107
	public static function &getBackend () : Translator_Backend {
108
		if (self::$backend == null) {
109
			//get translator backend
110
			$class_name = Settings::get("translator_class_name", "GetTextBackend");
111
112
			self::$backend = new $class_name();
113
114
			//initialize backend with current language
115
			self::$backend->init(Registry::singleton()->getSetting("lang_token"));
116
		}
117
118
		return self::$backend;
119
	}
120
121
}
122
123
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

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.

Loading history...
124