Test Failed
Push — master ( 283876...c3946c )
by Justin
03:41
created

GetTextBackend::n_translate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 8.8571
cc 5
eloc 9
nc 4
nop 5
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:20
26
 */
27
28
class GetTextBackend implements Translator_Backend {
29
30
	//current language token
31
	protected $lang_token = "";
32
33
	//current domain
34
	protected $domain = "";
35
36
	/**
37
	 * initialize translator backend
38
	 *
39
	 * @param $lang_token $lang_token contains language & country, e.q. de_DE
40
	 */
41
	public function init(string $lang_token) {
42
		//check, if gettext is available
43
		if (!PHPUtils::isGettextAvailable()) {
44
			throw new IllegalStateException("PHP extension 'gettext' is not available.");
45
		}
46
47
		putenv("LANG=" . $lang_token);
48
		setlocale(LC_ALL, $lang_token);
49
	}
50
51
	public function translate(string $key, string $domain = "", $params = null): string {
52
		$text = "";
53
54
		if (empty($domain)) {
55
			$text = gettext($key);
56
		} else {
57
			$text = dgettext($domain, $key);
58
		}
59
60
		if ($params != null && !empty($params)) {
61
			foreach ($params as $key=>$value) {
62
				//replace variables
63
				$text = str_replace("{" . $key . "}", $value, $text);
64
			}
65
		}
66
67
		return $text;
68
	}
69
70
	public function n_translate (string $key, string $plural_key, int $n, string $domain = "", $params = null) : string {
71
		$text = "";
72
73
		if (empty($domain)) {
74
			$text = ngettext($key, $plural_key, $n);
75
		} else {
76
			$text = dngettext($domain, $key, $plural_key, $n);
77
		}
78
79
		if ($params != null && !empty($params)) {
80
			foreach ($params as $key=>$value) {
81
				//replace variables
82
				$text = str_replace("{" . $key . "}", $value, $text);
83
			}
84
		}
85
86
		return $text;
87
	}
88
89
	public function bindLangPack(string $domain, string $path) {
90
		bindtextdomain($domain, $path);
91
		bind_textdomain_codeset($domain, 'UTF-8');
92
	}
93
94
	public function setDefaultDomain(string $domain) {
95
		textdomain($domain);
96
	}
97
}
98
99
?>
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...
100