aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/poche/Language.class.php
blob: 8d3912f5f8d01fa68d8944ab822f4a9585aa6a83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
 * wallabag, self hostable application allowing you to not miss any content anymore
 *
 * @category   wallabag
 * @author     Nicolas Lœuillet <nicolas@loeuillet.org>
 * @copyright  2013
 * @license    http://opensource.org/licenses/MIT see COPYING file
 */

class Language
{
    protected $wallabag;

    private $currentLanguage;

    private $languageNames = array(
        'cs_CZ.utf8' => 'čeština',
        'de_DE.utf8' => 'German',
        'en_EN.utf8' => 'English',
        'es_ES.utf8' => 'Español',
        'fa_IR.utf8' => 'فارسی',
        'fr_FR.utf8' => 'Français',
        'it_IT.utf8' => 'Italiano',
        'pl_PL.utf8' => 'Polski',
        'pt_BR.utf8' => 'Português (Brasil)',
        'ru_RU.utf8' => 'Pусский',
        'sl_SI.utf8' => 'Slovenščina',
        'uk_UA.utf8' => 'Українська',
    );

    public function __construct(Poche $wallabag)
    {
        $this->wallabag = $wallabag;
        $pocheUser = Session::getParam('poche_user');
        $language  = (is_null($pocheUser) ? LANG : $pocheUser->getConfigValue('language'));

        @putenv('LC_ALL=' . $language);
        setlocale(LC_ALL, $language);
        bindtextdomain($language, LOCALE);
        textdomain($language);

        $this->currentLanguage = $language;
    }

    public function getLanguage() {
        return $this->currentLanguage;
    }

    public function getInstalledLanguages() {
        $handle = opendir(LOCALE);
        $languages = array();

        while (($language = readdir($handle)) !== false) {
            # Languages are stored in a directory, so all directory names are languages
            # @todo move language installation data to database
            if (! is_dir(LOCALE . '/' . $language) || in_array($language, array('..', '.', 'tools'))) {
                continue;
            }

            $current = false;

            if ($language === $this->getLanguage()) {
                $current = true;
            }

            $languages[] = array('name' => (isset($this->languageNames[$language]) ? $this->languageNames[$language] : $language), 'value' => $language, 'current' => $current);
        }

        return $languages;
    }


    /**
     * Update language for current user
     *
     * @param $newLanguage
     */
    public function updateLanguage($newLanguage)
    {
        # we are not going to change it to the current language
        if ($newLanguage == $this->getLanguage()) {
            $this->wallabag->messages->add('w', _('still using the "' . $this->getLanguage() . '" language!'));
            Tools::redirect('?view=config');
        }

        $languages = $this->getInstalledLanguages();
        $actualLanguage = false;

        foreach ($languages as $language) {
            if ($language['value'] == $newLanguage) {
                $actualLanguage = true;
                break;
            }
        }

        if (!$actualLanguage) {
            $this->wallabag->messages->add('e', _('that language does not seem to be installed'));
            Tools::redirect('?view=config');
        }

        $this->wallabag->store->updateUserConfig($this->wallabag->user->getId(), 'language', $newLanguage);
        $this->wallabag->messages->add('s', _('you have changed your language preferences'));

        $currentConfig = $_SESSION['poche_user']->config;
        $currentConfig['language'] = $newLanguage;

        $_SESSION['poche_user']->setConfig($currentConfig);

        Tools::emptyCache();
        Tools::redirect('?view=config');
    }
}