aboutsummaryrefslogtreecommitdiffstats
path: root/localeDetection.class.php
blob: 40e9862e4ac9d8e14c0182d53dc80a5526405c68 (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
<?php

/* ChooseLocale
 *
 * Licence: MPL 2/GPL 2.0/LGPL 2.1
 * Author: Pascal Chevrel, Mozilla
 * Date : 2010-07-17
 *
 * Description:
 * Class to choose the locale which locale we will show to the visitor
 * based on http acceptlang headers and our list of supported locales.
 *
 *
*/




class ChooseLocale
{
    public    $HTTPAcceptLang;
    public    $supportedLocales;
    protected $detectedLocale;
    protected $defaultLocale;
    public    $mapLonglocales;


    public function __construct($list=array('en-US'))
    {
        $this -> HTTPAcceptLang   = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        $this -> supportedLocales = array_unique($list);
        $this -> setDefaultLocale('en-US');
        $this -> setCompatibleLocale();
        $this -> mapLonglocales = true;

    }

    public function getAcceptLangArray()
    {
        if (empty($this->HTTPAcceptLang)) return null;

        return explode(',', $this->HTTPAcceptLang);
    }

    public function getCompatibleLocale()
    {
        $l       = $this -> defaultLocale;
        $acclang = $this -> getAcceptLangArray();

        if(!is_array($acclang)) {
            return $this -> defaultLocale;
        }

        foreach ($acclang as $var) {
            $locale      = $this -> _cleanHTTPlocaleCode($var);
            $shortLocale = array_shift(explode('-', $locale));

            if (in_array($locale, $this -> supportedLocales)) {
                $l = $locale;
                break;
            }

            if (in_array($shortLocale, $this -> supportedLocales)) {
                $l = $shortLocale;
                break;
            }

            // check if we map visitors short locales to site long locales
            // like en -> en-GB
            if ($this -> mapLonglocales == true) {
                foreach ($this -> supportedLocales as $var) {
                    $shortSupportedLocale = array_shift(explode('-', $var));
                    if ($shortLocale == $shortSupportedLocale) {
                        $l = $var;
                        break;
                    }
                }
            }

            }

        return $l;
    }

    public function getDefaultLocale() {
        return $this -> defaultLocale;
    }

    public function setCompatibleLocale() {
        $this -> detectedLocale  = $this -> getCompatibleLocale();
    }

    public function setDefaultLocale($locale) {

        // the default locale should always be among the site locales
        // if not, the first locale in the supportedLocales array is default
        if (!in_array($locale, $this -> supportedLocales)) {
            $this -> defaultLocale = $this -> supportedLocales[0];

        } else {
            $this -> defaultLocale = $locale;
        }
        return;
    }

    private function _cleanHTTPlocaleCode($str)
    {
        $locale = explode(';', $str);
        $locale = trim($locale[0]);

        return $locale;
    }

}