summaryrefslogtreecommitdiff
path: root/include/MyLocale.class.php
blob: 49d68568ebcc50fe300760f8713048afa1557753 (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
<?php

class MyLocale {

    static private $allowedLangs = [];
    static private $_currentLocale = '';

    static function init() {
        date_default_timezone_set(Env::get('locale','timezone'));
        self::setLocale();
    }

    static function getAllowedLangs() {
        if (!count(self::$allowedLangs)) {
            self::$allowedLangs = Env::get('locale', 'languages');
        }

        return self::$allowedLangs;
    }

    static function getPreferredLang($allowed_languages = [], $deflang = '') {
        if (count($allowed_languages)==0) {
            $allowed_languages = self::getAllowedLangs();
        }

        if ($deflang=='') {
            $deflang = Env::get('locale','default-language');
        }

        if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            $accepted = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
            foreach ($accepted as $usrlang) {
                foreach ($allowed_languages as $pagelang) {
                    if ($pagelang === substr($usrlang, 0, strlen($pagelang))) {
                        return $pagelang;
                    }
                }
            }
        }
        elseif ($deflang) {
            return $deflang;
        }
        return $allowed_languages[0];
    }

    /**
     * sets current locale environment. It partially duplicates functionality of Env::lang()
     * in regards of storing current settings.
     *
     * @param langid - new language code. If null or empty string, default language will be
     * guessed by using Env::lang() method.
     *
     * @return void
     */
    static function setLocale($langid='') {
        if ($langid=='') {
            $langid = Env::lang();
        }
	if (self::$_currentLocale==$langid) {
            return;
	}
        self::$_currentLocale = $langid;
        bindtextdomain('main', BASEPATH.'/locale');
        bind_textdomain_codeset('main','utf-8');
        textdomain('main');
	try {
	  $localeId = Env::get('locale', 'locale-id', $langid);
	}
	catch (EnvNoSuchEntryException $e) {
	  $localeId = Env::get('locale', 'locale-id', Env::get('locale', 'default-language'));
	}

        setlocale(LC_MESSAGES, $localeId);
        setlocale(LC_CTYPE, $localeId);
        setlocale(LC_NUMERIC, $localeId);
        setlocale(LC_MONETARY, $localeId);
        setlocale(LC_TIME, $localeId);
        putenv('LANGUAGE=' . $localeId);
        Env::setLang($langid);
    }

    static function formatCurrency($value, $currency) {
        $locale = localeconv();
        $f = number_format(round($value, 2), 2, null, ' ');
        return sprintf(($locale['p_cs_precedes'] ? '%2$s %1$s' : '%s %s'),
                       $f, Currency::getCurrencySymbol($currency));
    }
}
?>