File: lang.php

package info (click to toggle)
gallery 1.5-1sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 21,172 kB
  • ctags: 4,516
  • sloc: php: 26,456; sh: 427; perl: 188; makefile: 58; xml: 48
file content (679 lines) | stat: -rw-r--r-- 18,385 bytes parent folder | download
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
<?php
/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: lang.php,v 1.43.2.3 2005/03/10 02:30:08 cryptographite Exp $
 */
?>
<?php

/*
** This function is a wrapper around ngettext for two reasons
** 1.) We can use %s and %d in translation
** 2.) We can use a special "none" without modifying the plural definition.
** Note: The redundant $count is always needed, when you use %d
**
** Example of normal use:
** pluralize_n2(ngettext("1 car", "5 cars", $numCars), $numCars, _("No cars"));
*/
function pluralize_n2($singPlu, $count, $none='') {
	if ($count == 0 && $none != '') {
		return $none;
	} else {
		return sprintf($singPlu, $count);
	}
}


/* Detect the first Language of users Browser
** Some Browser only send 2 digits like he or de.
** This is caught later with the aliases
*/
function getBrowserLanguage() {
	if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
		$lang = explode (",", $_SERVER["HTTP_ACCEPT_LANGUAGE"]);

		/* Maybe there are some extra infos we dont need, so we strip them. */
		$spos=strpos($lang[0],";");
		if ($spos >0) {
			$lang[0]=substr($lang[0],0,$spos);
		}
		
		/* browser may send aa-bb, then we convert to aa_BB */
		$lang_pieces=explode ("-",$lang[0]);
		if (strlen($lang[0]) >2) {
			$browserLang=strtolower($lang_pieces[0]). "_".strtoupper($lang_pieces[1]) ;
		} else {
			$browserLang=$lang[0];
		}
	}
	else {
		$browserLang=false;
	}
	
	return $browserLang;
}


/*
** Set Gallery Default:
** - language
** - charset
** - direction
** - alignment
*/
function setLangDefaults($nls) {
	global $gallery;

	$gallery->language 	= 'en_US';
	$gallery->charset  	= $nls['default']['charset'];
	$gallery->direction	= $nls['default']['direction'];
	$gallery->align		= $nls['default']['alignment'];
}

/*
** This function tries to get the languge given by the Environment.
** if no language is found, or Gallery was not able to get it, NULL is returned.
*/
function getEnvLang() {

	global $GALLERY_EMBEDDED_INSIDE_TYPE;

	global $board_config;				/* Needed for phpBB2 	*/
	global $_CONF;					/* Needed for GeekLog	*/
	global $mosConfig_locale;			/* Needed for Mambo	*/
	global $currentlang;				/* Needed for CPGNuke	*/

	$envLang = NULL;

	switch ($GALLERY_EMBEDDED_INSIDE_TYPE) {
		case 'postnuke':
			if (isset($_SESSION['PNSVlang'])) {
				$envLang = $_SESSION['PNSVlang'];
			}
		break;

		case 'phpnuke':
		case 'nsnnuke':
			if (isset($_COOKIE['lang'])) {
				$envLang = $_COOKIE['lang'];
			}

		break;

		case 'phpBB2':
			if (isset($board_config['default_lang'])) {
				$envLang = $board_config['default_lang'];
			}				
		break;

		case 'GeekLog':
			/* Note: $_CONF is not a Superglobal ;) */
			if (isset($_CONF['language'])) {
				$envLang = $_CONF['language'];
			} else if (isset($_CONF['locale'])) {
				$envLang = $_CONF['locale'];
			}				
		break;

		case 'mambo':
			if (isset($mosConfig_locale)){
				$envLang = $mosConfig_locale;
			}				
		break;

		case 'cpgnuke':
			if (isset($currentlang)){
				$envLang = $currentlang;
			}				
		break;

		default:
			return NULL;
		break;
	}

	return $envLang;
}

/*
** In some Environments we dont want to allow the user
** to change the language.
** In this case we override Mode 3 with Mode 1 and
** Gallery runs in the language the Environment use.
*/
function forceStaticLang() {
	global $GALLERY_EMBEDDED_INSIDE_TYPE;
	global $gallery;

	$useStatic=array('mambo', 'phpBB2', 'GeekLog');

	if (in_array($GALLERY_EMBEDDED_INSIDE_TYPE, $useStatic)) {
		$gallery->app->ML_mode=1;
	}
}	

function initLanguage($sendHeader=true) {
	static $languages_initialized = false;

	global $gallery, $GALLERY_EMBEDDED_INSIDE, $GALLERY_EMBEDDED_INSIDE_TYPE;

	// $locale is *NUKEs locale var
	global $locale ;

	$nls = getNLS();

	/* Set Defaults, they may be overwritten. */
	setLangDefaults($nls);

	// before we do any tests or settings test if we are in mode 0
	// If so, we skip language settings at all

	// Mode 0 means no Multilanguage at all.
	if (isset($gallery->app->ML_mode) && $gallery->app->ML_mode == 0 && !$languages_initialized) {
		// Maybe PHP has no (n)gettext, then we have to substitute _() and ngettext
		if (!gettext_installed()) {
			function _($string) {
				return $string ;
			}
		}
		if (!ngettext_installed()) {
			function ngettext($singular, $quasi_plural,$num=0) {
                       		if ($num == 1) {
                               		return $singular;
	                        } else {
       		                        return $quasi_plural;
               		        }
			}
		}

		/* Skip rest*/
		$languages_initialized = true;
		return;
	}


	/* 
	** Does the user wants a new lanuage ?
	** This is used in Standalone and *Nuke
	*/
	if (isset($_GET['newlang'])) {
		$newlang=$_GET['newlang'];
	}

	/**
	 ** Note: ML_mode is only used when not embedded
	 **/

	if (isset($GALLERY_EMBEDDED_INSIDE_TYPE)) {
		/* Gallery is embedded

		/* Gallery can set nukes language.
		** For phpBB2, GeekLog and Mambo this is not possible, Gallery will always use their language.
		*/
		forceStaticLang();

		if (!empty($newlang)) {
			// Set Language to the User selected language.
			$gallery->language=$newlang;
		} else {
			/* No new language.
			** Lets see in which Environment were are and look for a language.
			** Lets try to determ the used language
			*/ 
			$gallery->language = getEnvLang();
		}
	} else {
		// We're not embedded.
		// If we got a ML_mode from config.php we use it
		// If not we use Mode 2 (Browserlanguage)

		if (isset($gallery->app->ML_mode)) {
			$ML_mode=$gallery->app->ML_mode;
		} else {
			$ML_mode=2;
		}

		switch ($ML_mode) {
			case 1:
				//Static Language
				$gallery->language = $gallery->app->default_language;
				break;
			case 3:
				// Does the user want a new language ?
				if (!empty($newlang)) {
					// Set Language to the User selected language.
					$gallery->language=$newlang;
				} elseif (isset($gallery->session->language)) {
					//maybe we already have a language
					$gallery->language=$gallery->session->language;
				} elseif (isset($gallery->app->default_language)) {
					// Maybe we have a defaultlanguage set in config.php
		                        $gallery->language = $gallery->app->default_language;
				}
				break;
			default:
				// Use Browser Language or Userlanguage 
				// when mode 2 or any other (wrong) mode

				$gallery->browser_language=getBrowserLanguage();

				if (!empty($gallery->user) && $gallery->user->getDefaultLanguage() != "") {
					$gallery->language = $gallery->user->getDefaultLanguage();
				} elseif (isset($gallery->browser_language)) {
					$gallery->language=$gallery->browser_language;
				}
				break;
		}
	}

	// if an alias for the (new or Env) language is given, use it
	if (isset($nls['alias'][$gallery->language])) {
		$gallery->language = $nls['alias'][$gallery->language] ;
	}

	/**
	 **  Fall back to Default Language if :
	 **	- we cant detect Language
	 **	- Nuke/phpBB2 sent an unsupported
	 **	- User sent an undefined
	 **/

	if (! isset($nls['language'][$gallery->language])) {
		if (isset($gallery->app->default_language)) {
			$gallery->language = $gallery->app->default_language;
		} elseif(isset($gallery->browser_language)) {
			$gallery->language = $gallery->browser_language;
		} else {
			// when we REALLY REALLY cant detect a language
			$gallery->language="en_US";
		}
	}

	// And now set this language into session
	$gallery->session->language = $gallery->language;

	// locale
	if (isset($gallery->app->locale_alias[$gallery->language])) {
		$gallery->locale=$gallery->app->locale_alias["$gallery->language"];
	} else {
		$gallery->locale=$gallery->language;
	}

	// Override NUKEs locale :)))	
	$locale=$gallery->locale;

	// Check defaults :
	$checklist=array('direction', 'charset', 'alignment') ;

	/*
	** This checks wether the previously defined values are available.
	** All available values are in $nls
	** If they are not defined we used the defaults from nls.php
	*/
	foreach($checklist as $check) {
		// if no ... is given, use default
		if ( !isset($nls[$check][$gallery->language])) {
			$gallery->$check = $nls['default'][$check] ;
		} else {
			$gallery->$check = $nls[$check][$gallery->language] ;
		}
	}

	// When all is done do the settings
	
	// There was previously a != SUNOS check around the LANG= line.  We've determined that it was
	// probably a bogus bug report, since all documentation says this is fine.
	putenv("LANG=". $gallery->language);
	putenv("LANGUAGE=". $gallery->language);
	// This line was added in 1.5-cvs-b190 to fix problems on FreeBSD 4.10
	putenv("LC_ALL=". $gallery->language);

	// Set Locale
	setlocale(LC_ALL,$gallery->locale);

	/* 
	** Set Charset header
	** We do this only if we are not embedded and the "user" wants it.
	** Because headers might be sent already.
	*/
	if (! isset($GALLERY_EMBEDDED_INSIDE) || $sendHeader == false) {
		header('Content-Type: text/html; charset=' . $gallery->charset);
	}

	/*
	** Test if we're using gettext.
	** if yes, do some gettext settings.
	** if not emulate _() function or ngettext()
	**/

	if (gettext_installed()) {
		$bindtextdomain=bindtextdomain($gallery->language. "-gallery_". where_i_am(), dirname(dirname(__FILE__)) . '/locale');
		textdomain($gallery->language. "-gallery_". where_i_am());

	} elseif (!$languages_initialized) {
		emulate_gettext();
	}

	// We test this separate because ngettext() is only available in PHP >=4.2.0 but _() in all PHP4
	if (!ngettext_installed() && !$languages_initialized) {
		emulate_ngettext();
	}

	$languages_initialized = true;
}


function getTranslationFile() {

	global $gallery;
	static $translationfile;

	if (empty($translationfile)) {
		$filename=dirname(dirname(__FILE__)) . '/locale/' . $gallery->language . '/'. $gallery->language . '-gallery_' .  where_i_am()  . '.po';
		$translationfile=file($filename);
	}

return $translationfile;
}

/* Substitute ngettext function
** NOTE: this is the first primitive Step !!
** It fully ignores the plural definition !!
*/
function emulate_ngettext() {


	global $translation;
	global $gallery;

	if (in_array($gallery->language,array_keys(gallery_languages())) &&
		$gallery->language != 'en_US') {
		$lines=getTranslationFile();
		foreach ($lines as $key => $value) {
		//We trim the String to get rid of cr/lf
			$value=trim($value);
			if (stristr($value, "msgid") && ! stristr($lines[$key-1],"fuzzy") && !stristr($value,"msgid_plural")) {
//				echo "\n<br>---SID". $value;
//					echo "\n<br>---PID". $lines[$key+1];
				if (stristr($lines[$key+1],"msgid_plural")) {
					$singular_key=substr($value, 7,-1);
					$translation[$singular_key]=substr(trim($lines[$key+2]),11,-1);
					$plural_key=substr(trim($lines[$key+1]), 14,-1);
					$translation[$plural_key]=substr(trim($lines[$key+3]),11,-1);
//	echo "\n<br>SK". $singular_key;
//	echo "\n<br>ST". $translation[$singular_key];
//	echo "\n<br>PK". $plural_key;
//	echo "\n<br>PT". $translation[$plural_key];
				}
			}
		}
		// Substitute ngettext() function
		function ngettext($singular, $quasi_plural,$num=0) {
//			echo "\n<br>----";
//			echo "\nSL: $singular, PL: $quasi_plural, N: $num";
			if ($num == 1) {
				if (! empty($GLOBALS['translation'][$singular])) {
					return $GLOBALS['translation'][$singular] ;
				} else {
					return $singular;
				}
			}
			else {
				if (! empty($GLOBALS['translation'][$quasi_plural])) {
					return $GLOBALS['translation'][$quasi_plural] ;
				}
				else {
					return $quasi_plural;
				}
			}
		}
	}
	else {
		// There is no translation file or we are using original (en_US), so just return what we got
		function ngettext($singular, $quasi_plural,$num=0) {
			if ($num == 1) {
				return $singular;
			} else {
				return $quasi_plural;
			}
		}
	}
}

function emulate_gettext() {
	global $translation;
	global $gallery;

	if (in_array($gallery->language,array_keys(gallery_languages())) &&
		$gallery->language != 'en_US') {
		$filename=dirname(dirname(__FILE__)) . '/locale/' . $gallery->language . '/'. $gallery->language . '-gallery_' .  where_i_am()  . '.po';
		$lines=file($filename);

		foreach ($lines as $key => $value) {
			/* We trim the String to get rid of cr/lf */
			$value=trim($value);
			if (stristr($value, "msgid") 
				&& ! stristr($lines[$key-1],"fuzzy") 
				&& ! stristr($lines[$key],"msgid_plural")
				&& ! stristr($value,"msgid_plural")) {
				$new_key=substr($value, 7,-1);
				$translation[$new_key]=substr(trim($lines[$key+1]),8,-1);
//		echo "\n<br>NK". $new_key;
//		echo "\n<br>NT". $translation[$new_key];
			}
		}
		// Substitute _() gettext function
		function _($search) {
			if (! empty($GLOBALS['translation'][$search])) {
				return $GLOBALS['translation'][$search] ;
			}
			else {
				return $search;
			}
		}
	}
	// There is no translation file or we are using original (en_US), so just return what we got
	else {
		function _($search) {
			return $search;
		}
	}
}

function gettext_installed() {
	if (in_array("gettext", get_loaded_extensions()) && function_exists('gettext') && function_exists('_')) {
		return true;
	}
	else {
		return false;
	}
}

function ngettext_installed() {
	if (in_array("ngettext", get_loaded_extensions()) || function_exists('ngettext')) {
		return true;
	}
	else {
		return false;
	}
}


/* returns all languages in this gallery installation */
function gallery_languages() {
	$nls = getNLS();
	return $nls['language'];
}

/* returns all language relative that gallery could collect. */
function getNLS() {
    static $nls;

    if (empty($nls)) {
    	$nls = array();
	// Load defaults
	include (dirname(dirname(__FILE__)) . '/nls.php');

	$modules = array('config','core');
	$dir = dirname(dirname(__FILE__)) . '/locale';
	if (fs_is_dir($dir) && is_readable($dir) && $handle = fs_opendir($dir)) {
	    while ($dirname = readdir($handle)) {
	    	if (ereg("^([a-z]{2}_[A-Z]{2})", $dirname)) {
		    $locale = $dirname;
		    $fc = 0;
		    foreach ($modules as $module) {
		    	if (gettext_installed()) {
			    if (fs_file_exists(dirname(dirname(__FILE__)) . "/locale/$dirname/$locale-gallery_$module.po")) $fc++;
			} else {
			    if (fs_file_exists(dirname(dirname(__FILE__)) . "/locale/$dirname/LC_MESSAGES/$locale-gallery_$module.mo")) $fc++;
			}
		    }
		    if (fs_file_exists(dirname(dirname(__FILE__)) . "/locale/$dirname/$locale-nls.php") && $fc==sizeof($modules)) {
		    	include (dirname(dirname(__FILE__)) . "/locale/$dirname/$locale-nls.php");
		    }
		}
	    }
	    closedir($handle);
	}
    }

    return $nls;
}

function i18n($buf) {
       	return $buf;
}


function isSupportedCharset($charset) {
	$supportedCharsets=array(
		'UTF-8',
		'ISO-8859-1',
		'ISO-8859-15',
		'cp1252',
		'BIG5',
		'GB2312',
		'BIG5-HKSCS',
		'Shift_JIS',
		'EUC-JP'
	);

	$supportedCharsetsNewerPHP=array(
		'cp866',
		'cp1251',
		'KOI8-R'
	);

        /*
        ** Check if we are using PHP >= 4.1.0
        ** If yes, we can use 3rd Parameter so e.g. titles in chinese BIG5 or UTF8 are displayed correct.
        ** Otherwise they are messed.
        ** Not all Gallery Charsets are supported by PHP, so only thoselisted are recognized.
        */
	if (function_exists('version_compare')) {
		if ( (version_compare(phpversion(), "4.1.0", ">=") && in_array($charset, $supportedCharsets)) ||
		     (version_compare(phpversion(), "4.3.2", ">=") && in_array($charset, $supportedCharsetsNewerPHP)) ) {
			return true;
		} else {
			// Unsupported Charset
			return false;
		}
	} else {
		// PHP too old
		return false;
	}
}
	
/* Gallery Version of htmlentities
** Enhancement: Depending on PHP Version and Charset use 
** optional 3rd Parameter of php's htmlentities
*/
function gallery_htmlentities($string) {
	global $gallery;

	if (isSupportedCharset($gallery->charset)) {
		return htmlentities($string,ENT_COMPAT ,$gallery->charset);
	} else {
		return htmlentities($string);
        }
}

/*
** Convert all HTML entities to their applicable characters
*/
function unhtmlentities ($string) {
	global $gallery;

	if (empty($string)) {
		return "";
	}

	if (function_exists('html_entity_decode')) {
		$nls=getNLS();
		if (isset ($nls['charset'][$gallery->language])) {
			$charset=$nls['charset'][$gallery->language];
		} else {
			$charset=$nls['default']['charset'];
		}

		if (isSupportedCharset($charset) && strtolower($charset) != 'utf-8') {
			$return = html_entity_decode($string,ENT_COMPAT ,$charset);
		}
		else {
			// For unsupported charsets you may do this:
			$trans_tbl = get_html_translation_table (HTML_ENTITIES);
			$trans_tbl = array_flip ($trans_tbl);
			$return = strtr ($string, $trans_tbl);
		}
	} else {
		// For users with PHP prior to 4.3.0 you may do this:
		$trans_tbl = get_html_translation_table (HTML_ENTITIES);
		$trans_tbl = array_flip ($trans_tbl);
		$return = strtr ($string, $trans_tbl);
	}

return $return;
}

/* These are custom fields that are turned on and off at an album
 * level, and are populated for each photo automatically, without the
 * user typing values.  The $value of each pair should be translated
 * as appropriate in the ML version.
 */
function automaticFieldsList() {
        return array(
		'Upload Date' 	=> _("Upload Date"),
                'Capture Date' 	=> _("Capture Date"),
                'Dimensions' 	=> _("Image Size"),
                'EXIF' 		=> _("Additional EXIF Data"));
}

/* These are custom fields which can be entered manual by the User
** Since they are used often, we translated them.
*/
function translateableFields() {
	return array(
		'Title'		=> _("Title"),
		'Description'	=> _("Description"),
		'description'	=> _("description"),
		'AltText'	=> _("Alt Text / onMouseOver")
	);
}

?>