ZanPHP /zan/helpers/i18n.php

Language PHP Lines 368
MD5 Hash 8a221fe72ac62a5f526568d9b28796d3
Repository https://github.com/triartdesign/ZanPHP.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
/**
 * ZanPHP
 *
 * An open source agile and rapid development framework for PHP 5
 *
 * @package		ZanPHP
 * @author		MilkZoft Developer Team
 * @copyright	Copyright (c) 2011, MilkZoft, Inc.
 * @license		http://www.zanphp.com/documentation/en/license/
 * @link		http://www.zanphp.com
 * @version		1.0
 */
 
/**
 * Access from index.php:
 */
if(!defined("_access")) {
	die("Error: You don't have permission to access here...");
}

/**
 * i18n Helper
 *
 * 
 *
 * @package		ZanPHP
 * @subpackage	core
 * @category	helpers
 * @author		MilkZoft Developer Team
 * @link		http://www.zanphp.com/documentation/en/helpers/i18n_helper
 */

/**
 * __()
 *
 * Returns the translation of a specific text
 * 
 * @param string $text
 * @return string value
 */
function __($text, $encode = TRUE) {
	if(get("translation") === "gettext") {
		global $Gettext_Reader;
		
		if(is_null($Gettext_Reader)) {
			return $text;
		}
		
		return $Gettext_Reader->translate($text);
	} else {
		global $Load, $phrase;
		
		$language = whichLanguage();
		
		if(file_exists("www/lib/languages/". strtolower($language) .".php")) {
			include_once "www/lib/languages/". strtolower($language) .".php";
		} 

		$position = strtolower(str_replace(" ", "_", $text)); 
		$position = strtolower(str_replace("?,", "", $position));
		$position = strtolower(str_replace("!", "", $position));
		$position = strtolower(str_replace("ÂĄ", "", $position));
		$position = strtolower(str_replace("Âż", "", $position));
		$position = strtolower(str_replace(",", "", $position));
		$position = strtolower(str_replace(":", "", $position));
		$position = strtolower(str_replace("'", "", $position));

		if(isset($phrase[$position])) {
			return ($encode) ? encode($phrase[$position]) : $phrase[$position];
		} else {
			if($language !== "English") {
				$content = "";
				$logfile = "www/lib/languages/". strtolower($language) . ".txt"; 
				$today	 = date("d/m/Y");

				if(file_exists($logfile)) {
					$content = file_get_contents($logfile);
				}

				$file = fopen($logfile, "a+");
				$pos  = strrpos($content, "$today");

				if($pos !== FALSE) {
					if(!@preg_match("/\\b" . addslashes($position) . "\\b/i", substr($content, $pos + 14))) {
						fwrite($file, "$position\r\n");
					}
				} else {
					fwrite($file, "--- $today ---\r\n");
					fwrite($file, "$position\r\n");
				}
			}

			return $text;
		}
	}
}

/**
 * getLanguage
 *
 * Returns the translation of a specific Language Word
 * 
 * @param boolean $invert = TRUE
 * @return string value
 */
function getLanguage($lang, $flags = FALSE) {
	$languages = getLanguagesFromDir();
	
	foreach($languages as $language) {
		if($flags) {
			if($language["language"] === $lang) {
				return '<img class="flag no-border" src="'. get("webURL") .'/www/lib/images/icons/flags/'. strtolower($lang) .'.png" alt="'. __($lang) .'" />';	
			}
		} else {
			if($language["language"] === $lang) {
				return __($lang);
			}
		}
	}

	return FALSE;
}

/**
 * isLanguage
 *
 * Defines if an Abbrevation is a standard XML language
 * 
 * @param boolean $invert = TRUE
 * @return string value
 */
function isLang($lang = FALSE) {
	$lang = (!$lang) ? strtolower(segment(0)) : $lang;	
	
	$langs = array("ar", "bs", "be", "bu", "ca", "ch", "cr", "cz", "da", "du", "en", "et", "fi", "fr", "ga", "ge", "gr", "he", "hu", "in", "it", "jp", "ku", "li", "ma", "pe", "po", "pt", "ro", "ru", "se", "sk", "sn", "es", "sw", "th", "tk", "uk", "ur", "vi");
	
	return in_array($lang, $langs) ? TRUE : FALSE;
}


/**
 * whichLanguage
 *
 * Returns the default Language
 * 
 * @param boolean $invert = TRUE
 * @return string value
 */
function whichLanguage($invert = TRUE, $lower = FALSE) {
	global $Load;
	
	$Load->helper("router");

	if(isLang(segment(0))) {
		if(segment(0) and !$invert) {
			return segment(0);
		} elseif(segment(0) and getLang(segment(0), TRUE)) { 
			return ($lower) ? strtolower(getLang(segment(0), TRUE)) : getLang(segment(0), TRUE);
		} elseif(!$invert) {
			return getLang(get("webLanguage"));
		} else {
			return ($lower) ? strtolower(get("webLanguage")) : get("webLanguage");
		}	
	} else {
		if(!$invert) {
			return getLang(get("webLanguage"));
		} else {
			return ($lower) ? strtolower(get("webLanguage")) : get("webLanguage");
		}	
	}
}

function getLanguages($flags = FALSE) {
	$data = array();

	$languages = getLanguagesFromDir();

	foreach($languages as $language) {
		$default = ($language["language"] === get("webLanguage")) ? TRUE : FALSE;

		$data[] = array("default" => $default, "name" => $language["language"], "value" => getLanguage($language["language"], $flags));
	}
	
	return $data;
}

function getLanguagesInput($lang = NULL, $name = "language", $input = "radio") {
	$languages = getLanguages(TRUE);
	$HTML = NULL;

	if($input === "select") {
		$HTML = '<select name="'. $name .'" size="1">';
	}

	foreach($languages as $language) {
		if(!isset($checked)) {
			if(!is_null($lang)) {
				if($lang === $language["name"]) {
					$check = ($input === "radio") ? ' checked="checked"' : ' selected="selected"';
					$checked = TRUE;								
				} else {
					$check = NULL;
				}
			} else {
				if($language["default"] === TRUE) {				
					$check = ($input === "radio") ? ' checked="checked"' : ' selected="selected"';
					$checked = TRUE;
				}
			}
		}

		$show = isset($check) ? $check : NULL;

		if($input === "radio") {
			$HTML .= ' <input id="language" name="'. $name .'" type="radio" value="'. $language["name"] .'" '. $show .' /> '. $language["value"] .' ';			
		} elseif($input === "select") {
			$HTML .= ' <option value="'. $language["name"] .'"'. $show .'>'. __($language["name"]) .'</option>';
		}

		unset($check);
	}

	if($input === "select") {
		$HTML .= '</select>';
	}
	
	return $HTML;
}

function getLocal($lang = FALSE) {	
	if(!$lang) {
		$lang = whichLanguage();
	}

	$languages = array(
		"Arabic"	 => "ar_AR",
		"Basque"	 => "eu_ES",
		"Belarusian" => "be_BY",
		"Bulgarian"  => "bg_BG",
		"Catalan"	 => "ca_ES",
		"Chinese"	 => "zh_CN",
		"Croatian"   => "hr_HR",
		"Czech"		 => "cs_CZ",
		"Danish"	 => "da_DK",
		"Dutch"		 => "nl_NL",
		"English" 	 => "en_US",
		"Estonian"   => "et_EE",
		"Finnish"	 => "fi_FI",
		"French"  	 => "fr_FR",
		"Galician"   => "gl_ES",
		"German"	 => "de_DE",
		"Greek"		 => "el_GR",
		"Hebrew"	 => "he_IL",
		"Hungarian"  => "hu_HU",
		"Indonesian" => "id_ID",
		"Italian"	 => "it_IT",
		"Japanese"	 => "ja_JP",
		"Kurdish"	 => "ku_TR",
		"Lithuanian" => "lt_LT",
		"Macedonian" => "mk_MK",
		"Persian"	 => "Persian",
		"Polish"	 => "pl_PL",
		"Portuguese" => "pt_BR",
		"Romanian"   => "ro_RO",
		"Russian"	 => "ru_RU",
		"Serbian"	 => "sr_RS",
		"Slovak"	 => "sk_SK",
		"Slovenian"	 => "sl_SI",
		"Spanish" 	 => "es_LA",
		"Swedish"	 => "sv_SE",
		"Thai"		 => "th_TH",
		"Turkish"	 => "tr_TR",
		"Ukrainian"  => "uk_UA",
		"Vietnamese" => "vi_VN"
	);	

	foreach($languages as $language => $locale) {
		if($language === $lang) {
			return $locale;
		}
	}
}

function getLang($lg, $invert = FALSE) {
	$languages = array(
		"Arabic"	 => "ar",
		"Basque"	 => "bs",
		"Belarusian" => "be",
		"Bulgarian"  => "bu",
		"Catalan"	 => "ca",
		"Chinese"	 => "ch",
		"Croatian"   => "cr",
		"Czech"		 => "cz",
		"Danish"	 => "da",
		"Dutch"		 => "du",
		"English" 	 => "en",
		"Estonian"   => "et",
		"Finnish"	 => "fi",
		"French"  	 => "fr",
		"Galician"   => "ga",
		"German"	 => "ge",
		"Greek"		 => "gr",
		"Hebrew"	 => "he",
		"Hungarian"  => "hu",
		"Indonesian" => "in",
		"Italian"	 => "it",
		"Japanese"	 => "jp",
		"Kurdish"	 => "ku",
		"Lithuanian" => "li",
		"Macedonian" => "ma",
		"Persian"	 => "pe",
		"Polish"	 => "po",
		"Portuguese" => "pt",
		"Romanian"   => "ro",
		"Russian"	 => "ru",
		"Serbian"	 => "se",
		"Slovak"	 => "sk",
		"Slovenian"	 => "sn",
		"Spanish" 	 => "es",
		"Swedish"	 => "sw",
		"Thai"		 => "th",
		"Turkish"	 => "tk",
		"Ukrainian"  => "uk",
		"Vietnamese" => "vi"
	);	
	
	foreach($languages as $language => $lang) {
		if($invert) {
			if($lg === $lang) {
				return $language;
			}	
		} else {
			if($language === $lg) {
				return $lang;
			}
		}
	}

	return ($invert) ? "English" : "en";
}

function getLanguagesFromDir() {
	$path = "www/lib/languages";
	$dir  = dir($path);

	$i = 1;
	
	while($element = $dir->read()) { 
		if($element !== ".." and $element !== "." and $element !== ".DS_Store" and $element !== "index.html") {
			$language  = str_replace("language.", "", $element);
			$parts = explode(".", $language);

			if(count($parts) > 1) {
				if($parts[1] === "php") {
					$languages[$i]["language"] = ucfirst($parts[0]);			
					$languages[$i]["lang"]	   = getLang(ucfirst($parts[0]));
						
					$i++;		
				}
			}
		}
	}	
		
	$dir->close();		
	
	return $languages;
}
Back to Top