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