File: Lang.php

package info (click to toggle)
bamboo 1.2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 580 kB
  • ctags: 1,338
  • sloc: php: 4,061; makefile: 44; sh: 36
file content (101 lines) | stat: -rw-r--r-- 3,359 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
<?php

#########################################################
# LANGUAGE FUNCTIONS

// global vars, should only be accessed through Lang class
$_lang_possible       = null;   // array of possible language codes
$_lang_possible_map   = null;   // array of possible codes => full name
$_lang_priorities     = null;   // array of language codes, ordered by priority
$_lang_default        = '';     // fallback default lang code
$_lang_map = array(             // map codes to full names
	'de' => 'Deutsch',
	'en' => 'English',
	'el' => '&epsilon;&lambda;&lambda;&eta;&nu;&iota;&kappa;&#940;',
	'es' => 'Espa&ntilde;ol',
	'fr' => 'Fran&ccedil;ais',
	'it' => 'Italiano',
	'pt' => 'Portugu&ecirc;s',
);

class Lang {

// returns the top choice language
function top() {global $_lang_priorities; return $_lang_priorities[0];}

// sets the current language setting
function set($language) {if (isset($language) && $language != '') $_SESSION['lang'] = $language;}

// init: called very early in frontdoor.php
function init($default, $possible) {
	global $_lang_default, $_lang_possible, $_lang_possible_map, $_lang_map;
	$_lang_default = $default;
	$_lang_possible = $possible;
	$_lang_possible_map = array();
	foreach($_lang_possible as $code) {
		if (isset($_lang_map[$code])) $_lang_possible_map[$code] = $_lang_map[$code];
	}
}

/**
 * returns language priorities, and updates them if necesssary.
 **/

function priority() {
	global $_lang_priorities, $_lang_possible, $_lang_default;
	$languages = &$_lang_priorities;
	if ( ! isset($languages) ) {
		// start with what the browser wants
		if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
			$languages = split("," , preg_replace('/(;q=\d+.\d+)|(\-..)| /i', '', $_SERVER['HTTP_ACCEPT_LANGUAGE']) );
		else
			$languages = array();

		// if session defines a language, put it in first place
		if ( isset( $_SESSION['lang'] ) )
			array_unshift( $languages, $_SESSION['lang'] );
	
		// remove languages which are not allowed in the site configuration
		if (is_array($_lang_possible) && count($_lang_possible)) {
			$languages = array_intersect($_lang_possible, $languages);		
		}

		// Just in case we have no other languages, set a default.
		$languages[] = $_lang_default;
	
		// remove duplicates and make sure indexes are sequential.
		$languages = array_values(array_unique($languages));	
	}

	// if session lang is set, make sure it is our 'highest' priority language.
	// we do this every time, not just the first time, in case it has changed.
	if ( isset($_SESSION['lang']) && $_SESSION['lang'] != $languages[0]) {
		array_unshift( $languages, $_SESSION['lang'] );
		$languages = array_values(array_unique($languages));
	}

	//  if there is no session yet, set the session language to the top choice.
	if (! isset($_SESSION['lang']) ) {
		$_SESSION['lang'] = $languages[0];
	}

	return $languages;
}

// returns a form for selecting the language.
function getform($action, $extra='') {
	global $_lang_possible_map;
	$html = "<form action=\"$action\" method=\"get\">\n";
	$html .= $extra;
	$html .= "<select name=\"lang\" onchange=\"this.form.submit();\">\n";
	foreach ($_lang_possible_map as $lcode => $lname) {
		$selected = $lcode == $_SESSION['lang'] ? "SELECTED" : ""; 
		$html .= "<option value=\"$lcode\" $selected>$lname</option>\n";
	}
	$html .= "</select>\n</form>\n";
	return $html;
}

}
return;
?>