summaryrefslogtreecommitdiff
path: root/framework/I18N/core/HTTPNegotiator.php
blob: e9f1d0e0e9130ffeb1791f5abdfdb8a6a472dd81 (plain)
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
<?php

/**
 * HTTPNegotiator class file.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the BSD License.
 *
 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
 *
 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
 * The latest version of PRADO can be obtained from:
 * {@link http://prado.sourceforge.net/}
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package Prado\I18N\core
 */

namespace Prado\I18N\core;

/**
 * Include the CultureInfo class.
 */
require_once(dirname(__FILE__).'/CultureInfo.php');

/**
 * HTTPNegotiator class.
 *
 * Get the language and charset information from the client browser.
 *
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package Prado\I18N\core
 */
class HTTPNegotiator
{
	/**
	 * A list of languages accepted by the browser.
	 * @var array
	 */
	protected $languages;

	/**
	 * A list of charsets accepted by the browser
	 * @var array
	 */
	protected $charsets;

	/**
	 * Get a list of languages acceptable by the client browser
	 * @return array languages ordered in the user browser preferences.
	 */
	function getLanguages()
	{
		if($this->languages !== null) {
			return $this->languages;
		}

		$this->languages = array();

		if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
            return $this->languages;

		//$basedir = CultureInfo::dataDir();
		//$ext = CultureInfo::fileExt();
		$info = new CultureInfo();

		foreach(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang)
		{
            // Cut off any q-value that might come after a semi-colon
            if ($pos = strpos($lang, ';'))
                $lang = trim(substr($lang, 0, $pos));

			if (strstr($lang, '-'))
			{
				$codes = explode('-',$lang);
				if($codes[0] == 'i')
				{
                    // Language not listed in ISO 639 that are not variants
                    // of any listed language, which can be registerd with the
                    // i-prefix, such as i-cherokee
					if(count($codes)>1)
						$lang = $codes[1];
				}
				else
				{
					for($i = 0, $k = count($codes); $i<$k; ++$i)
					{
						if($i == 0)
							$lang = strtolower($codes[0]);
						else
							$lang .= '_'.strtoupper($codes[$i]);
					}
				}
            }



			if($info->validCulture($lang))
				$this->languages[] = $lang;
        }

		return $this->languages;
	}

	/**
	 * Get a list of charsets acceptable by the client browser.
	 * @return array list of charsets in preferable order.
	 */
	function getCharsets()
	{
        if($this->charsets !== null) {
			return $this->charsets;
		}

		$this->charsets = array();

		if (!isset($_SERVER['HTTP_ACCEPT_CHARSET']))
            return $this->charsets;

		foreach (explode(',', $_SERVER['HTTP_ACCEPT_CHARSET']) as $charset)
		{
            if (!empty($charset))
                $this->charsets[] = preg_replace('/;.*/', '', $charset);
        }

		return $this->charsets;
	}
}