Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.08% covered (success)
96.08%
49 / 51
90.00% covered (success)
90.00%
9 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Language
96.08% covered (success)
96.08%
49 / 51
90.00% covered (success)
90.00%
9 / 10
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
6
 getDefaultLanguageName
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getCurrentLanguage
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 getLocale
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 getCurrentLocale
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCurrentLocale
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getLocaleList
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getDefault
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
 getLanguageFromRequest
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getLanguageFromUri
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace BO\Slim;
4
5use Psr\Http\Message\RequestInterface;
6use Symfony\Bridge\Twig\Extension\TranslationExtension;
7
8class Language
9{
10    public static $supportedLanguages = array();
11
12    public $current = '';
13
14    protected $currentLocale = '';
15
16    protected $default = '';
17
18    protected static $translatorInstance = null;
19
20    public function __construct(RequestInterface $request, array $supportedLanguages)
21    {
22        self::$supportedLanguages = $supportedLanguages;
23        $this->current = $this->getLanguageFromRequest($request);
24        $fallbackLocale = $this->getLocale($this->getDefault());
25        $this->currentLocale = $this->getLocale($this->getCurrentLanguage());
26        $this->setCurrentLocale();
27        $defaultLang = $this->getDefault();
28
29        if (
30            \App::MULTILANGUAGE
31            || (strlen($fallbackLocale) > 0 && strlen($this->currentLocale) > 0 && strlen($defaultLang) > 0)
32        ) {
33            if (null === self::$translatorInstance) {
34                self::$translatorInstance = (new LanguageTranslator(
35                    $fallbackLocale,
36                    $this->currentLocale,
37                    $defaultLang
38                ))->getInstance();
39                \BO\Slim\Bootstrap::addTwigExtension(new TranslationExtension(self::$translatorInstance));
40            } else {
41                self::$translatorInstance->setLocale($this->currentLocale);
42            }
43        }
44    }
45
46    public function getDefaultLanguageName()
47    {
48        $default = \App::$supportedLanguages[$this->getDefault()]['name'] ?? null;
49        return $default;
50    }
51
52    public function getCurrentLanguage($lang = '')
53    {
54        $current = (isset(self::$supportedLanguages[$this->current])) ? $this->current : $this->getDefault();
55        return ($lang != '') ? $lang : $current;
56    }
57
58    public function getLocale($locale = '')
59    {
60        $locale = ('' == $locale) ? $this->getDefault() : $locale;
61        if (
62            isset(self::$supportedLanguages[$this->getCurrentLanguage($locale)]) &&
63            isset(self::$supportedLanguages[$this->getCurrentLanguage($locale)]['locale'])
64        ) {
65            $locale = self::$supportedLanguages[$this->getCurrentLanguage($locale)]['locale'];
66        }
67        return $locale;
68    }
69
70    public function getCurrentLocale()
71    {
72        return $this->currentLocale;
73    }
74
75    public function setCurrentLocale()
76    {
77        if (class_exists("Locale")) {
78            \Locale::setDefault($this->currentLocale);
79        }
80        \setlocale(LC_ALL, $this->getLocaleList($this->currentLocale));
81    }
82
83    protected function getLocaleList($locale)
84    {
85        $localeList[] = $this->getCurrentLanguage();
86        $localeList[] = $locale;
87        $suffixList = ['utf8', 'utf-8'];
88        foreach ($suffixList as $suffix) {
89            array_unshift($localeList, $locale . '.' . $suffix);
90        }
91        return $localeList;
92    }
93
94    public function getDefault()
95    {
96        if (! $this->default) {
97            foreach (self::$supportedLanguages as $lang_id => $lang_data) {
98                if (isset($lang_data['default']) && $lang_data['default']) {
99                    $this->default = $lang_id;
100                    break;
101                }
102            }
103            if (! $this->default) {
104                reset(self::$supportedLanguages);
105                $this->default = key(self::$supportedLanguages);
106            }
107        }
108        return $this->default;
109    }
110
111    // Detect current language based on request URI or Parameter
112    protected function getLanguageFromRequest(RequestInterface $request)
113    {
114        $language = $this->getLanguageFromUri($request);
115        $route = $request->getAttribute('route');
116
117        if ($route instanceof \Slim\Routing\Route) {
118            $lang = $route->getArgument('lang');
119            $language = (!empty($lang)) ? $lang : $language;
120        }
121
122        return $language;
123    }
124
125    protected function getLanguageFromUri($request)
126    {
127        $requestParamLang = $request->getParam('lang');
128        return ($requestParamLang) ? $requestParamLang : $this->getDefault();
129    }
130}