]>
Commit | Line | Data |
---|---|---|
3602405e NL |
1 | <?php |
2 | /** | |
3 | * wallabag, self hostable application allowing you to not miss any content anymore | |
4 | * | |
5 | * @category wallabag | |
6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> | |
7 | * @copyright 2013 | |
8 | * @license http://opensource.org/licenses/MIT see COPYING file | |
9 | */ | |
10 | ||
11 | class Language | |
12 | { | |
13 | protected $wallabag; | |
14 | ||
15 | private $currentLanguage; | |
16 | ||
17 | private $languageNames = array( | |
18 | 'cs_CZ.utf8' => 'čeština', | |
19 | 'de_DE.utf8' => 'German', | |
c35753dd | 20 | 'en_GB.utf8' => 'English (GB)', |
bbbda080 | 21 | 'en_US.utf8' => 'English (US)', |
3602405e NL |
22 | 'es_ES.utf8' => 'Español', |
23 | 'fa_IR.utf8' => 'فارسی', | |
24 | 'fr_FR.utf8' => 'Français', | |
25 | 'it_IT.utf8' => 'Italiano', | |
26 | 'pl_PL.utf8' => 'Polski', | |
27 | 'pt_BR.utf8' => 'Português (Brasil)', | |
28 | 'ru_RU.utf8' => 'Pусский', | |
29 | 'sl_SI.utf8' => 'Slovenščina', | |
30 | 'uk_UA.utf8' => 'Українська', | |
31 | ); | |
32 | ||
33 | public function __construct(Poche $wallabag) | |
34 | { | |
35 | $this->wallabag = $wallabag; | |
36 | $pocheUser = Session::getParam('poche_user'); | |
37 | $language = (is_null($pocheUser) ? LANG : $pocheUser->getConfigValue('language')); | |
38 | ||
39 | @putenv('LC_ALL=' . $language); | |
40 | setlocale(LC_ALL, $language); | |
41 | bindtextdomain($language, LOCALE); | |
42 | textdomain($language); | |
43 | ||
44 | $this->currentLanguage = $language; | |
45 | } | |
46 | ||
47 | public function getLanguage() { | |
48 | return $this->currentLanguage; | |
49 | } | |
50 | ||
51 | public function getInstalledLanguages() { | |
52 | $handle = opendir(LOCALE); | |
53 | $languages = array(); | |
54 | ||
55 | while (($language = readdir($handle)) !== false) { | |
56 | # Languages are stored in a directory, so all directory names are languages | |
57 | # @todo move language installation data to database | |
58 | if (! is_dir(LOCALE . '/' . $language) || in_array($language, array('..', '.', 'tools'))) { | |
59 | continue; | |
60 | } | |
61 | ||
62 | $current = false; | |
63 | ||
64 | if ($language === $this->getLanguage()) { | |
65 | $current = true; | |
66 | } | |
67 | ||
68 | $languages[] = array('name' => (isset($this->languageNames[$language]) ? $this->languageNames[$language] : $language), 'value' => $language, 'current' => $current); | |
69 | } | |
70 | ||
71 | return $languages; | |
72 | } | |
73 | ||
74 | ||
75 | /** | |
76 | * Update language for current user | |
77 | * | |
78 | * @param $newLanguage | |
79 | */ | |
80 | public function updateLanguage($newLanguage) | |
81 | { | |
82 | # we are not going to change it to the current language | |
83 | if ($newLanguage == $this->getLanguage()) { | |
84 | $this->wallabag->messages->add('w', _('still using the "' . $this->getLanguage() . '" language!')); | |
85 | Tools::redirect('?view=config'); | |
86 | } | |
87 | ||
88 | $languages = $this->getInstalledLanguages(); | |
89 | $actualLanguage = false; | |
90 | ||
91 | foreach ($languages as $language) { | |
92 | if ($language['value'] == $newLanguage) { | |
93 | $actualLanguage = true; | |
94 | break; | |
95 | } | |
96 | } | |
97 | ||
98 | if (!$actualLanguage) { | |
99 | $this->wallabag->messages->add('e', _('that language does not seem to be installed')); | |
100 | Tools::redirect('?view=config'); | |
101 | } | |
102 | ||
103 | $this->wallabag->store->updateUserConfig($this->wallabag->user->getId(), 'language', $newLanguage); | |
104 | $this->wallabag->messages->add('s', _('you have changed your language preferences')); | |
105 | ||
106 | $currentConfig = $_SESSION['poche_user']->config; | |
107 | $currentConfig['language'] = $newLanguage; | |
108 | ||
109 | $_SESSION['poche_user']->setConfig($currentConfig); | |
110 | ||
2f26729c | 111 | Tools::emptyCache(); |
3602405e NL |
112 | Tools::redirect('?view=config'); |
113 | } | |
bbbda080 | 114 | } |