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