From 3602405ec0dbc576fce09ff9e865ba2404622080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 11 Jul 2014 16:03:59 +0200 Subject: WHAT. A. BIG. REFACTOR. + new license (we moved to MIT one) --- inc/poche/Language.class.php | 114 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 inc/poche/Language.class.php (limited to 'inc/poche/Language.class.php') 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 @@ + + * @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); + + $this->wallabag->emptyCache(); + + Tools::redirect('?view=config'); + } +} \ No newline at end of file -- cgit v1.2.3 From 2f26729c841a68669a1baf799091cb2c6c9f585a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Sat, 12 Jul 2014 19:01:11 +0200 Subject: Refactor --- inc/poche/Language.class.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'inc/poche/Language.class.php') diff --git a/inc/poche/Language.class.php b/inc/poche/Language.class.php index 790cc197..8d3912f5 100644 --- a/inc/poche/Language.class.php +++ b/inc/poche/Language.class.php @@ -107,8 +107,7 @@ class Language $_SESSION['poche_user']->setConfig($currentConfig); - $this->wallabag->emptyCache(); - + Tools::emptyCache(); Tools::redirect('?view=config'); } } \ No newline at end of file -- cgit v1.2.3