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