Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/**
4
 * HTTPNegotiator class file.
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the BSD License.
8
 *
9
 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
10
 *
11
 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
12
 * The latest version of PRADO can be obtained from:
13
 * {@link http://prado.sourceforge.net/}
14
 *
15
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
16
 * @version $Revision: 1.2 $  $Date: 2005/01/05 03:15:14 $
17
 * @package System.I18N.core
18
 */
19
 
20
/**
21
 * Include the CultureInfo class.
22
 */
23
require_once(dirname(__FILE__).'/CultureInfo.php');
24
 
25
/**
26
 * HTTPNegotiator class.
27
 *
28
 * Get the language and charset information from the client browser.
29
 *
30
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
31
 * @version v1.0, last update on Fri Dec 24 16:01:35 EST 2004
32
 * @package System.I18N.core
33
 */
34
class HTTPNegotiator
35
{
36
	/**
37
	 * A list of languages accepted by the browser.
38
	 * @var array
39
	 */
40
	protected $languages;
41
 
42
	/**
43
	 * A list of charsets accepted by the browser
44
	 * @var array
45
	 */
46
	protected $charsets;
47
 
48
	/**
49
	 * Get a list of languages acceptable by the client browser
50
	 * @return array languages ordered in the user browser preferences.
51
	 */
52
	function getLanguages()
53
	{
54
		if($this->languages !== null) {
55
			return $this->languages;
56
		}
57
 
58
		$this->languages = array();
59
 
60
		if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
61
            return $this->languages;
62
 
63
		//$basedir = CultureInfo::dataDir();
64
		//$ext = CultureInfo::fileExt();
65
		$info = new CultureInfo();
66
 
67
		foreach(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang)
68
		{
69
            // Cut off any q-value that might come after a semi-colon
70
            if ($pos = strpos($lang, ';'))
71
                $lang = trim(substr($lang, 0, $pos));
72
 
73
			if (strstr($lang, '-'))
74
			{
75
				$codes = explode('-',$lang);
76
				if($codes[0] == 'i')
77
				{
78
                    // Language not listed in ISO 639 that are not variants
79
                    // of any listed language, which can be registerd with the
80
                    // i-prefix, such as i-cherokee
81
					if(count($codes)>1)
82
						$lang = $codes[1];
83
				}
84
				else
85
				{
86
					for($i = 0, $k = count($codes); $i<$k; ++$i)
87
					{
88
						if($i == 0)
89
							$lang = strtolower($codes[0]);
90
						else
91
							$lang .= '_'.strtoupper($codes[$i]);
92
					}
93
				}
94
            }
95
 
96
 
97
 
98
			if($info->validCulture($lang))
99
				$this->languages[] = $lang;
100
        }
101
 
102
		return $this->languages;
103
	}
104
 
105
	/**
106
	 * Get a list of charsets acceptable by the client browser.
107
	 * @return array list of charsets in preferable order.
108
	 */
109
	function getCharsets()
110
	{
111
        if($this->charsets !== null) {
112
			return $this->charsets;
113
		}
114
 
115
		$this->charsets = array();
116
 
117
		if (!isset($_SERVER['HTTP_ACCEPT_CHARSET']))
118
            return $this->charsets;
119
 
120
		foreach (explode(',', $_SERVER['HTTP_ACCEPT_CHARSET']) as $charset)
121
		{
122
            if (!empty($charset))
123
                $this->charsets[] = preg_replace('/;.*/', '', $charset);
124
        }
125
 
126
		return $this->charsets;
127
	}
128
}
129