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