Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
4
// | PHP version 4.0                                                      |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 The PHP Group       |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.0 of the PHP license,       |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Naoki Shima <murahachibu@php.net>                           |
17
// |                                                                      |
18
// +----------------------------------------------------------------------+//
19
// $Id: Negotiator.php 110339 2003-01-04 11:55:29Z mj $
20
 
21
/**
22
*
23
* //instantiate Locale_Negotiator
24
* $negotiator       = & new I18N_Negotiator();
25
*
26
* //define which language[s] your site supports :: optional
27
* $supportLangs     = array('fr','jp');
28
*
29
* //find first matched language
30
* $lang             =  $negotiator->getLanguageMatch($supportedLangs);
31
*
32
* //define which countries your site supports :: optional
33
* $supportCountries = array('gb','us');
34
*
35
* //find first matched Country
36
* $countryCode      = $negotiator->getCountryMatch($lang,$supportCountries);
37
*
38
* echo 'Language Code: '.$lang.'
39
* Language Name: '.$negotiator->getLanguageName($lang).'
40
* Country Code: '.$countryCode.'
41
* Country Name: '.$negotiator->getCountryName($countryCode);
42
*/
43
 
44
class I18N_Negotiator{
45
 
46
    // {{{ properties
47
 
48
    /**
49
     * Save default country code.
50
     *
51
     * @type  : string
52
     * @access: private
53
     */
54
    var $_defaultCountry;
55
 
56
    /**
57
     * Save default language code.
58
     *
59
     * @type  : string
60
     * @access: private
61
     */
62
    var $_defaultLanguage;
63
 
64
    /**
65
     * Save default charset code.
66
     *
67
     * @type  : string
68
     * @access: private
69
     */
70
    var $_defaultCharset;
71
 
72
    // }}}
73
    // {{{ constructor
74
 
75
    /**
76
     * Find language code, country code, charset code, and dialect or variant
77
     * of Locale setting in user's browser from $HTTP_ACCEPT_LANGUAGE,
78
     * $LANGUAGE_ACCEPT_CHARSET
79
     *
80
     * @param : string   Default Language
81
     * @param : string   Default Charset
82
     * @param : string   Default Country
83
     */
84
    function I18N_Negotiator($defaultLanguage = "en", $defaultCharset = "ISO-8859-1", $defaultCountry = "")
85
    {
86
        if($_SERVER) {
87
            $HTTP_ACCEPT_LANGUAGE = ( in_array("HTTP_ACCEPT_LANGUAGE",
88
				array_keys($_SERVER)) ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : "";
89
            $HTTP_ACCEPT_CHARSET = ( in_array("HTTP_ACCEPT_CHARSET",
90
				array_keys($_SERVER)) ) ? $_SERVER['HTTP_ACCEPT_CHARSET'] : "";
91
        } else {
92
            global $HTTP_ACCEPT_LANGUAGE,$HTTP_ACCEPT_CHARSET;
93
        }
94
        $this->_defaultCountry = $defaultCountry;
95
        $this->_defaultLanguage = $defaultLanguage;
96
        $this->_defaultCharset = $defaultCharset;
97
        $langs = explode(',',$HTTP_ACCEPT_LANGUAGE);
98
        foreach($langs AS $lang_tag) {
99
            // Cut off any q-value that might come after a semi-colon
100
            if($pos = strpos($lang_tag, ';')) {
101
                $lang_tag = trim(substr($lang_tag,0,$pos));
102
            }
103
            $lang = $lang_tag;
104
            if($pos = strpos($lang_tag, '-')) {
105
                $primary_tag = substr($lang_tag,0,$pos);
106
                $sub_tag = substr($lang_tag,($pos+1));
107
                if($primary_tag == 'i') {
108
                    /**
109
                     * Language not listed in ISO 639 that are not variants
110
                     * of any listed language, which can be registerd with the
111
                     * i-prefix, such as i-cherokee
112
                     */
113
                    $lang = $sub_tag;
114
                } else {
115
                    $lang = $primary_tag;
116
                    $this->_prepareI18NCountry();
117
                    if($this->_lc->isValidCode($sub_tag)) {
118
                        $this->_country[$lang][] = $sub_tag;
119
                    } else {
120
                        /**
121
                         * Dialect or variant information such as no-nynorsk or
122
                         * en-cockney.
123
                         * Script variations, such as az-arabic and az-cyrillic
124
                         */
125
                        $this->_lang_variation[$lang][] = $sub_tag;
126
                    }
127
                }
128
            }
129
            $this->_acceptLanguage[] = $lang;
130
        }
131
        $this->_acceptCharset = explode(',',$HTTP_ACCEPT_CHARSET);
132
    }
133
 
134
    // }}}
135
    // {{{ _constructor();
136
 
137
    /**
138
     * Dummy constructor
139
     * call actual constructor
140
     */
141
    function _constructor()
142
    {
143
        $this->I18N_Negotiator();
144
    }
145
 
146
    // }}}
147
    // {{{ destructor
148
 
149
    /**
150
     * It does nothing right now
151
     */
152
    function _I18N_Negotiater()
153
    {
154
    }
155
 
156
    // }}}
157
    // {{{ getCountryMatch()
158
 
159
    /**
160
     * Find Country Match
161
     *
162
     * @param : string
163
     * @param : array
164
     *
165
     * @return: array
166
     * @access: public
167
     */
168
    function getCountryMatch($lang='',$countries = '')
169
    {
170
        if(!$lang) {
171
            return FALSE;
172
        }
173
        return $this->_getMatch($countries,$this->_country[$lang],
174
                   $this->_defaultCountry);
175
    }
176
 
177
    // }}}
178
    // {{{ getVariantInfo()
179
 
180
    /**
181
     * Return variant info for passed parameter.
182
     *
183
     * @param : string
184
     *
185
     * @return: string
186
     * @access: public
187
     */
188
    function getVariantInfo($lang)
189
    {
190
        return $this->_lang_variation[$lang];
191
    }
192
 
193
    // }}}
194
    // {{{ getCharsetMatch()
195
 
196
    /**
197
     * Find Charset match
198
     *
199
     * @param : array
200
     *
201
     * @return: string
202
     * @access: public
203
     */
204
    function getCharsetMatch($chasets = '')
205
    {
206
        return $this->_getMatch($charsets,$this->_acceptCharset,
207
                   $this->_defaultCharset);
208
    }
209
 
210
    // }}}
211
    // {{{ getLanguageMatch()
212
 
213
    /**
214
     * Find Language match
215
     *
216
     * @param : array
217
     *
218
     * @return: string
219
     * @access: public
220
     */
221
    function getLanguageMatch($langs = '')
222
    {
223
        return $this->_getMatch($langs,$this->_acceptLanguage,
224
                   $this->_defaultLanguage);
225
    }
226
 
227
    // }}}
228
    // {{{ _getMatch()
229
 
230
    /**
231
     * Return first matched value from first and second parameter.
232
     * If there is no match found, then return third parameter.
233
     *
234
     * @param : array
235
     * @param : array
236
     * @param : string
237
     *
238
     * @return: string
239
     * @access: private
240
     */
241
    function _getMatch($needle,$heystack,$default = '')
242
    {
243
        if(!$heystack) {
244
            return $default;
245
        }
246
        if(!$needle) {
247
            return array_shift($heystack);
248
        }
249
        if($result = array_shift(array_intersect($heystack,$needle))) {
250
            return $result;
251
        }
252
        return $default;
253
    }
254
 
255
    /**
256
     * Find Country name for country code passed
257
     *
258
     * @param : string   country code
259
     *
260
     * @return: void
261
     * @access: private
262
     */
263
    function getCountryName($code)
264
    {
265
        $this->_prepareI18NCountry();
266
        return $this->_lc->getName($code);
267
    }
268
 
269
    /**
270
     * Find Country name for country code passed
271
     *
272
     * @param : string   country code
273
     *
274
     * @return: void
275
     * @access: private
276
     */
277
    function getLanguageName($code)
278
    {
279
        $this->_prepareI18NLanguage();
280
        return $this->_ll->getName($code);
281
    }
282
 
283
    /**
284
     * Check if I18N_Language class has been instantiated and set to $this->_ll
285
     * If it's not, it will load the script and instantiate I18N_Language class
286
     *
287
     * @return: void
288
     * @access: private
289
     */
290
    function _prepareI18NLanguage()
291
    {
292
        if(!isset($this->_ll)) {
293
            include_once('I18N/Language.php');
294
            $this->_ll =& new I18N_Language();
295
        }
296
    }
297
 
298
    /**
299
     * Check if I18N_Country class has been instantiated and set to $this->_lc
300
     * If it's not, it will load the script and instantiate I18N_Country class
301
     *
302
     * @return: void
303
     * @access: private
304
     */
305
    function _prepareI18NCountry()
306
    {
307
        if(!isset($this->_lc)) {
308
            include_once('I18N/Country.php');
309
            $this->_lc =& new I18N_Country();
310
        }
311
    }
312
}
313
?>