@@ -1,4 +1,4 @@ |
||
1 | 1 | <?php |
2 | 2 | return [ |
3 | - "test" => "bu bir test", |
|
3 | + "test" => "bu bir test", |
|
4 | 4 | ]; |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -require_once __DIR__ . '/vendor/autoload.php'; // Autoload files using Composer autoload |
|
3 | +require_once __DIR__.'/vendor/autoload.php'; // Autoload files using Composer autoload |
|
4 | 4 | |
5 | 5 | use multilangphp\multilangphp; |
6 | 6 |
@@ -12,176 +12,176 @@ |
||
12 | 12 | */ |
13 | 13 | class multilangPHP |
14 | 14 | { |
15 | - private $language = "tr"; |
|
16 | - private $dir = "system/lang/"; |
|
17 | - private $from = "php"; |
|
18 | - private $definedExtension = [ |
|
19 | - "php", |
|
20 | - "json" |
|
21 | - ]; |
|
15 | + private $language = "tr"; |
|
16 | + private $dir = "system/lang/"; |
|
17 | + private $from = "php"; |
|
18 | + private $definedExtension = [ |
|
19 | + "php", |
|
20 | + "json" |
|
21 | + ]; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Get the value of language |
|
25 | - * |
|
26 | - * @return string |
|
27 | - */ |
|
28 | - public function getLanguage(): string |
|
29 | - { |
|
30 | - return $this->language; |
|
31 | - } |
|
23 | + /** |
|
24 | + * Get the value of language |
|
25 | + * |
|
26 | + * @return string |
|
27 | + */ |
|
28 | + public function getLanguage(): string |
|
29 | + { |
|
30 | + return $this->language; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Set the value of language |
|
35 | - * |
|
36 | - * @param string $language |
|
37 | - * @return self |
|
38 | - */ |
|
39 | - public function setLanguage(string $language): self |
|
40 | - { |
|
41 | - $this->language = $language; |
|
42 | - return $this; |
|
43 | - } |
|
33 | + /** |
|
34 | + * Set the value of language |
|
35 | + * |
|
36 | + * @param string $language |
|
37 | + * @return self |
|
38 | + */ |
|
39 | + public function setLanguage(string $language): self |
|
40 | + { |
|
41 | + $this->language = $language; |
|
42 | + return $this; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Get the value of dir |
|
47 | - * |
|
48 | - * @return string |
|
49 | - */ |
|
50 | - public function getDir(): string |
|
51 | - { |
|
52 | - return $this->dir; |
|
53 | - } |
|
45 | + /** |
|
46 | + * Get the value of dir |
|
47 | + * |
|
48 | + * @return string |
|
49 | + */ |
|
50 | + public function getDir(): string |
|
51 | + { |
|
52 | + return $this->dir; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Set the value of dir |
|
57 | - * |
|
58 | - * @param string $dir |
|
59 | - * @return self |
|
60 | - */ |
|
61 | - public function setDir(string $dir): self |
|
62 | - { |
|
63 | - $this->dir = $dir; |
|
64 | - return $this; |
|
65 | - } |
|
55 | + /** |
|
56 | + * Set the value of dir |
|
57 | + * |
|
58 | + * @param string $dir |
|
59 | + * @return self |
|
60 | + */ |
|
61 | + public function setDir(string $dir): self |
|
62 | + { |
|
63 | + $this->dir = $dir; |
|
64 | + return $this; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Get the value of from |
|
69 | - * |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - public function getFrom(): string |
|
73 | - { |
|
74 | - return $this->from; |
|
75 | - } |
|
67 | + /** |
|
68 | + * Get the value of from |
|
69 | + * |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + public function getFrom(): string |
|
73 | + { |
|
74 | + return $this->from; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Set the value of from |
|
79 | - * |
|
80 | - * @param string $from |
|
81 | - * @return self |
|
82 | - */ |
|
83 | - public function setFrom(string $from): self |
|
84 | - { |
|
85 | - if(!in_array($from, $this->definedExtension)) |
|
86 | - { |
|
87 | - $usable = ""; |
|
88 | - foreach($this->definedExtension as $tip) $usable .= sprintf(' "%s",', $tip); |
|
89 | - $usable = substr($usable, 0, -1); |
|
90 | - exit("MultilangPHP: You can only use the". $usable . ' parameters.'); |
|
91 | - } |
|
92 | - $this->from = $from; |
|
93 | - return $this; |
|
94 | - } |
|
77 | + /** |
|
78 | + * Set the value of from |
|
79 | + * |
|
80 | + * @param string $from |
|
81 | + * @return self |
|
82 | + */ |
|
83 | + public function setFrom(string $from): self |
|
84 | + { |
|
85 | + if(!in_array($from, $this->definedExtension)) |
|
86 | + { |
|
87 | + $usable = ""; |
|
88 | + foreach($this->definedExtension as $tip) $usable .= sprintf(' "%s",', $tip); |
|
89 | + $usable = substr($usable, 0, -1); |
|
90 | + exit("MultilangPHP: You can only use the". $usable . ' parameters.'); |
|
91 | + } |
|
92 | + $this->from = $from; |
|
93 | + return $this; |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Get exact location of the language file |
|
98 | - * |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - public function getLangFile(): string |
|
102 | - { |
|
103 | - return sprintf("%s%s.%s", $this->getDir(), $this->getLanguage(), $this->getFrom()); |
|
104 | - } |
|
96 | + /** |
|
97 | + * Get exact location of the language file |
|
98 | + * |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + public function getLangFile(): string |
|
102 | + { |
|
103 | + return sprintf("%s%s.%s", $this->getDir(), $this->getLanguage(), $this->getFrom()); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Control File Extension Function |
|
108 | - * |
|
109 | - * if file extension correct; return true |
|
110 | - * |
|
111 | - * @param string $file |
|
112 | - * @param string $extension |
|
113 | - * @return boolean |
|
114 | - */ |
|
115 | - public function controlFileExtention(string $file, string $extension = null): bool |
|
116 | - { |
|
117 | - if (!$extension) $extension = $this->getFrom(); |
|
118 | - $fKontrol = explode(".", $file); |
|
119 | - if($fKontrol[count($fKontrol)-1] == $extension) return true; |
|
120 | - return false; |
|
121 | - } |
|
106 | + /** |
|
107 | + * Control File Extension Function |
|
108 | + * |
|
109 | + * if file extension correct; return true |
|
110 | + * |
|
111 | + * @param string $file |
|
112 | + * @param string $extension |
|
113 | + * @return boolean |
|
114 | + */ |
|
115 | + public function controlFileExtention(string $file, string $extension = null): bool |
|
116 | + { |
|
117 | + if (!$extension) $extension = $this->getFrom(); |
|
118 | + $fKontrol = explode(".", $file); |
|
119 | + if($fKontrol[count($fKontrol)-1] == $extension) return true; |
|
120 | + return false; |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * Return language(s) in selected directory |
|
125 | - * |
|
126 | - * @param string $returnType |
|
127 | - * @return array|string |
|
128 | - */ |
|
129 | - public function listAllLangFiles(string $returnType = "array") |
|
130 | - { |
|
131 | - if ($returnType == "array" || $returnType == "html") |
|
132 | - { |
|
133 | - $langPHP_array = array(); |
|
134 | - $langPHP_html = '<div class="multilang">'; |
|
135 | - $openDir = opendir($this->getDir()); |
|
136 | - while (($file = readdir($openDir)) != FALSE ) { |
|
137 | - if ($file =='.' || $file == '..' || is_file($file) || !$this->controlFileExtention($file)) continue; |
|
138 | - if ($returnType == "array") $langPHP_array[basename($file, sprintf(".%s",$this->getFrom()))] = $file; |
|
139 | - if ($returnType == "html") $langPHP_html .= sprintf(' <a href="?lang=%s" title="language %s">%s</a> ', basename($file, sprintf('.%s', $this->getFrom())), basename($file, sprintf('.%s', $this->getFrom())), basename($file, sprintf('.%s', $this->getFrom()))); |
|
140 | - } |
|
141 | - $langPHP_html .= '</div>'; |
|
142 | - closedir($openDir); |
|
143 | - return $returnType == "array" ? $langPHP_array : $langPHP_html; |
|
144 | - } |
|
145 | - exit("MultilangPHP: You can only use the 'array' and 'html' parameters on ".__METHOD__."()"); |
|
146 | - } |
|
123 | + /** |
|
124 | + * Return language(s) in selected directory |
|
125 | + * |
|
126 | + * @param string $returnType |
|
127 | + * @return array|string |
|
128 | + */ |
|
129 | + public function listAllLangFiles(string $returnType = "array") |
|
130 | + { |
|
131 | + if ($returnType == "array" || $returnType == "html") |
|
132 | + { |
|
133 | + $langPHP_array = array(); |
|
134 | + $langPHP_html = '<div class="multilang">'; |
|
135 | + $openDir = opendir($this->getDir()); |
|
136 | + while (($file = readdir($openDir)) != FALSE ) { |
|
137 | + if ($file =='.' || $file == '..' || is_file($file) || !$this->controlFileExtention($file)) continue; |
|
138 | + if ($returnType == "array") $langPHP_array[basename($file, sprintf(".%s",$this->getFrom()))] = $file; |
|
139 | + if ($returnType == "html") $langPHP_html .= sprintf(' <a href="?lang=%s" title="language %s">%s</a> ', basename($file, sprintf('.%s', $this->getFrom())), basename($file, sprintf('.%s', $this->getFrom())), basename($file, sprintf('.%s', $this->getFrom()))); |
|
140 | + } |
|
141 | + $langPHP_html .= '</div>'; |
|
142 | + closedir($openDir); |
|
143 | + return $returnType == "array" ? $langPHP_array : $langPHP_html; |
|
144 | + } |
|
145 | + exit("MultilangPHP: You can only use the 'array' and 'html' parameters on ".__METHOD__."()"); |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * If set From php, we are getting languages in php file |
|
150 | - * |
|
151 | - * @param string $called |
|
152 | - * @return string |
|
153 | - */ |
|
154 | - public function callInPHP(string $called): string |
|
155 | - { |
|
156 | - $call = include($this->getLangFile()); |
|
157 | - if (!is_array($call)) exit("MultilangPHP: Some have problem in language file, file is not returning array: ".__METHOD__."()"); |
|
158 | - return $call[$called]; |
|
159 | - } |
|
148 | + /** |
|
149 | + * If set From php, we are getting languages in php file |
|
150 | + * |
|
151 | + * @param string $called |
|
152 | + * @return string |
|
153 | + */ |
|
154 | + public function callInPHP(string $called): string |
|
155 | + { |
|
156 | + $call = include($this->getLangFile()); |
|
157 | + if (!is_array($call)) exit("MultilangPHP: Some have problem in language file, file is not returning array: ".__METHOD__."()"); |
|
158 | + return $call[$called]; |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * If set $from json, we are getting languages in json file |
|
163 | - * |
|
164 | - * @param string $called |
|
165 | - * @return string |
|
166 | - */ |
|
167 | - public function callInJSON(string $called): string |
|
168 | - { |
|
169 | - $call = file_get_contents($this->getLangFile()); |
|
170 | - if (!$call) exit("MultilangPHP: JSON file not readable!"); |
|
171 | - $decodeCall = json_decode($call, true); |
|
172 | - if (is_array($decodeCall) && !$decodeCall[$called]) $decodeCall = $decodeCall[0]; |
|
173 | - return $decodeCall[$called] ? $decodeCall[$called] : exit(sprintf('multilangPHP: "%s" not found in %s file', $called, $this->getLangFile())); |
|
174 | - } |
|
161 | + /** |
|
162 | + * If set $from json, we are getting languages in json file |
|
163 | + * |
|
164 | + * @param string $called |
|
165 | + * @return string |
|
166 | + */ |
|
167 | + public function callInJSON(string $called): string |
|
168 | + { |
|
169 | + $call = file_get_contents($this->getLangFile()); |
|
170 | + if (!$call) exit("MultilangPHP: JSON file not readable!"); |
|
171 | + $decodeCall = json_decode($call, true); |
|
172 | + if (is_array($decodeCall) && !$decodeCall[$called]) $decodeCall = $decodeCall[0]; |
|
173 | + return $decodeCall[$called] ? $decodeCall[$called] : exit(sprintf('multilangPHP: "%s" not found in %s file', $called, $this->getLangFile())); |
|
174 | + } |
|
175 | 175 | |
176 | - /** |
|
177 | - * Call method |
|
178 | - * |
|
179 | - * @param string $value |
|
180 | - * @return string |
|
181 | - */ |
|
182 | - public function call(string $value): string |
|
183 | - { |
|
184 | - if (!$value) exit("MultilangPHP: You have to use with a parameter on ".__METHOD__."()"); |
|
185 | - return call_user_func(array($this, sprintf('callIn%s', strtoupper($this->getFrom()))), $value); |
|
186 | - } |
|
176 | + /** |
|
177 | + * Call method |
|
178 | + * |
|
179 | + * @param string $value |
|
180 | + * @return string |
|
181 | + */ |
|
182 | + public function call(string $value): string |
|
183 | + { |
|
184 | + if (!$value) exit("MultilangPHP: You have to use with a parameter on ".__METHOD__."()"); |
|
185 | + return call_user_func(array($this, sprintf('callIn%s', strtoupper($this->getFrom()))), $value); |
|
186 | + } |
|
187 | 187 | } |
188 | 188 | \ No newline at end of file |
@@ -82,12 +82,12 @@ discard block |
||
82 | 82 | */ |
83 | 83 | public function setFrom(string $from): self |
84 | 84 | { |
85 | - if(!in_array($from, $this->definedExtension)) |
|
85 | + if (!in_array($from, $this->definedExtension)) |
|
86 | 86 | { |
87 | 87 | $usable = ""; |
88 | - foreach($this->definedExtension as $tip) $usable .= sprintf(' "%s",', $tip); |
|
88 | + foreach ($this->definedExtension as $tip) $usable .= sprintf(' "%s",', $tip); |
|
89 | 89 | $usable = substr($usable, 0, -1); |
90 | - exit("MultilangPHP: You can only use the". $usable . ' parameters.'); |
|
90 | + exit("MultilangPHP: You can only use the".$usable.' parameters.'); |
|
91 | 91 | } |
92 | 92 | $this->from = $from; |
93 | 93 | return $this; |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | { |
117 | 117 | if (!$extension) $extension = $this->getFrom(); |
118 | 118 | $fKontrol = explode(".", $file); |
119 | - if($fKontrol[count($fKontrol)-1] == $extension) return true; |
|
119 | + if ($fKontrol[count($fKontrol) - 1] == $extension) return true; |
|
120 | 120 | return false; |
121 | 121 | } |
122 | 122 | |
@@ -133,9 +133,9 @@ discard block |
||
133 | 133 | $langPHP_array = array(); |
134 | 134 | $langPHP_html = '<div class="multilang">'; |
135 | 135 | $openDir = opendir($this->getDir()); |
136 | - while (($file = readdir($openDir)) != FALSE ) { |
|
137 | - if ($file =='.' || $file == '..' || is_file($file) || !$this->controlFileExtention($file)) continue; |
|
138 | - if ($returnType == "array") $langPHP_array[basename($file, sprintf(".%s",$this->getFrom()))] = $file; |
|
136 | + while (($file = readdir($openDir)) != FALSE) { |
|
137 | + if ($file == '.' || $file == '..' || is_file($file) || !$this->controlFileExtention($file)) continue; |
|
138 | + if ($returnType == "array") $langPHP_array[basename($file, sprintf(".%s", $this->getFrom()))] = $file; |
|
139 | 139 | if ($returnType == "html") $langPHP_html .= sprintf(' <a href="?lang=%s" title="language %s">%s</a> ', basename($file, sprintf('.%s', $this->getFrom())), basename($file, sprintf('.%s', $this->getFrom())), basename($file, sprintf('.%s', $this->getFrom()))); |
140 | 140 | } |
141 | 141 | $langPHP_html .= '</div>'; |
@@ -85,7 +85,9 @@ discard block |
||
85 | 85 | if(!in_array($from, $this->definedExtension)) |
86 | 86 | { |
87 | 87 | $usable = ""; |
88 | - foreach($this->definedExtension as $tip) $usable .= sprintf(' "%s",', $tip); |
|
88 | + foreach($this->definedExtension as $tip) { |
|
89 | + $usable .= sprintf(' "%s",', $tip); |
|
90 | + } |
|
89 | 91 | $usable = substr($usable, 0, -1); |
90 | 92 | exit("MultilangPHP: You can only use the". $usable . ' parameters.'); |
91 | 93 | } |
@@ -114,9 +116,13 @@ discard block |
||
114 | 116 | */ |
115 | 117 | public function controlFileExtention(string $file, string $extension = null): bool |
116 | 118 | { |
117 | - if (!$extension) $extension = $this->getFrom(); |
|
119 | + if (!$extension) { |
|
120 | + $extension = $this->getFrom(); |
|
121 | + } |
|
118 | 122 | $fKontrol = explode(".", $file); |
119 | - if($fKontrol[count($fKontrol)-1] == $extension) return true; |
|
123 | + if($fKontrol[count($fKontrol)-1] == $extension) { |
|
124 | + return true; |
|
125 | + } |
|
120 | 126 | return false; |
121 | 127 | } |
122 | 128 | |
@@ -134,9 +140,15 @@ discard block |
||
134 | 140 | $langPHP_html = '<div class="multilang">'; |
135 | 141 | $openDir = opendir($this->getDir()); |
136 | 142 | while (($file = readdir($openDir)) != FALSE ) { |
137 | - if ($file =='.' || $file == '..' || is_file($file) || !$this->controlFileExtention($file)) continue; |
|
138 | - if ($returnType == "array") $langPHP_array[basename($file, sprintf(".%s",$this->getFrom()))] = $file; |
|
139 | - if ($returnType == "html") $langPHP_html .= sprintf(' <a href="?lang=%s" title="language %s">%s</a> ', basename($file, sprintf('.%s', $this->getFrom())), basename($file, sprintf('.%s', $this->getFrom())), basename($file, sprintf('.%s', $this->getFrom()))); |
|
143 | + if ($file =='.' || $file == '..' || is_file($file) || !$this->controlFileExtention($file)) { |
|
144 | + continue; |
|
145 | + } |
|
146 | + if ($returnType == "array") { |
|
147 | + $langPHP_array[basename($file, sprintf(".%s",$this->getFrom()))] = $file; |
|
148 | + } |
|
149 | + if ($returnType == "html") { |
|
150 | + $langPHP_html .= sprintf(' <a href="?lang=%s" title="language %s">%s</a> ', basename($file, sprintf('.%s', $this->getFrom())), basename($file, sprintf('.%s', $this->getFrom())), basename($file, sprintf('.%s', $this->getFrom()))); |
|
151 | + } |
|
140 | 152 | } |
141 | 153 | $langPHP_html .= '</div>'; |
142 | 154 | closedir($openDir); |
@@ -154,7 +166,9 @@ discard block |
||
154 | 166 | public function callInPHP(string $called): string |
155 | 167 | { |
156 | 168 | $call = include($this->getLangFile()); |
157 | - if (!is_array($call)) exit("MultilangPHP: Some have problem in language file, file is not returning array: ".__METHOD__."()"); |
|
169 | + if (!is_array($call)) { |
|
170 | + exit("MultilangPHP: Some have problem in language file, file is not returning array: ".__METHOD__."()"); |
|
171 | + } |
|
158 | 172 | return $call[$called]; |
159 | 173 | } |
160 | 174 | |
@@ -167,9 +181,13 @@ discard block |
||
167 | 181 | public function callInJSON(string $called): string |
168 | 182 | { |
169 | 183 | $call = file_get_contents($this->getLangFile()); |
170 | - if (!$call) exit("MultilangPHP: JSON file not readable!"); |
|
184 | + if (!$call) { |
|
185 | + exit("MultilangPHP: JSON file not readable!"); |
|
186 | + } |
|
171 | 187 | $decodeCall = json_decode($call, true); |
172 | - if (is_array($decodeCall) && !$decodeCall[$called]) $decodeCall = $decodeCall[0]; |
|
188 | + if (is_array($decodeCall) && !$decodeCall[$called]) { |
|
189 | + $decodeCall = $decodeCall[0]; |
|
190 | + } |
|
173 | 191 | return $decodeCall[$called] ? $decodeCall[$called] : exit(sprintf('multilangPHP: "%s" not found in %s file', $called, $this->getLangFile())); |
174 | 192 | } |
175 | 193 | |
@@ -181,7 +199,9 @@ discard block |
||
181 | 199 | */ |
182 | 200 | public function call(string $value): string |
183 | 201 | { |
184 | - if (!$value) exit("MultilangPHP: You have to use with a parameter on ".__METHOD__."()"); |
|
202 | + if (!$value) { |
|
203 | + exit("MultilangPHP: You have to use with a parameter on ".__METHOD__."()"); |
|
204 | + } |
|
185 | 205 | return call_user_func(array($this, sprintf('callIn%s', strtoupper($this->getFrom()))), $value); |
186 | 206 | } |
187 | 207 | } |
188 | 208 | \ No newline at end of file |