BryanDavis has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/101175


Change subject: Use session to remember language selection
......................................................................

Use session to remember language selection

Persist the user's language selection in their session.

Change-Id: I6c7047bfd5e6241bac636f84971600c742aaae57
---
M data/templates/base.html
M src/Wikimania/Scholarship/App.php
M src/Wikimania/Scholarship/Lang.php
3 files changed, 31 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/wikimania-scholarships 
refs/changes/75/101175/1

diff --git a/data/templates/base.html b/data/templates/base.html
index 53db3af..bd65184 100644
--- a/data/templates/base.html
+++ b/data/templates/base.html
@@ -1,3 +1,4 @@
+{% set lang = wgLang.getLang %}
 <!DOCTYPE html>
 <!--[if lt IE 7 ]><html class="ie ie6 no-js" xml:lang="{{ lang }}" lang="{{ 
lang }}"><![endif]-->
 <!--[if IE 7 ]><html class="ie ie7 no-js" xml:lang="{{ lang }}" lang="{{ lang 
}}"><![endif]-->
diff --git a/src/Wikimania/Scholarship/App.php 
b/src/Wikimania/Scholarship/App.php
index cb10c79..dad920b 100644
--- a/src/Wikimania/Scholarship/App.php
+++ b/src/Wikimania/Scholarship/App.php
@@ -57,6 +57,7 @@
                        'smtp.host' => self::config( 'SMTP_HOST', 'localhost' ),
                        'templates.path' => "{$this->deployDir}/data/templates",
                        'i18n.path' => "{$this->deployDir}/data/i18n",
+                       'i18n.default' => 'en',
                ));
 
                $slim = $this->slim;
@@ -143,7 +144,7 @@
                });
 
                $container->singleton( 'wgLang', function ( $c ) {
-                       return new Lang( $c->settings['i18n.path'] );
+                       return new Lang( $c->settings['i18n.path'], 
$c->settings['i18n.default'] );
                });
 
                $container->singleton( 'applyForm', function ( $c ) {
@@ -205,7 +206,6 @@
                $view->replace( array(
                        'app' => $this->slim,
                        'wgLang' => $this->slim->wgLang,
-                       'lang' => $this->slim->wgLang->setLang( $_REQUEST ),
                ) );
        }
 
diff --git a/src/Wikimania/Scholarship/Lang.php 
b/src/Wikimania/Scholarship/Lang.php
index da62c44..f6d59b3 100644
--- a/src/Wikimania/Scholarship/Lang.php
+++ b/src/Wikimania/Scholarship/Lang.php
@@ -47,12 +47,18 @@
        protected $langDir;
 
        /**
-        * @param string $dir Directory containing language files
+        * @param string $defaultLang
         */
-       public function __construct( $dir ) {
+       protected $defaultLang;
+
+       /**
+        * @param string $dir Directory containing language files
+        * @param string $default Default language
+        */
+       public function __construct( $dir, $default = 'en' ) {
                // TODO: assert that the dir exists
                $this->langDir = $dir;
-               $this->lang = 'en';
+               $this->defaultLang = $default;
 
                $messages = array();
                foreach ( glob( "{$this->langDir}/*.php" ) as $file ) {
@@ -72,20 +78,26 @@
        }
 
        /**
-        * Set the active language.
-        * @param array $req Request data
-        * @param string $default Default language
+        * Get the active language.
         * @return string Selected language
         */
-       public function setLang( $req, $default = 'en' ) {
-               if ( isset( $req['uselang'] ) &&
-                       in_array( $req['uselang'], $this->getLangs() )
-               ) {
-                       $lang = $req['uselang'];
+       public function getLang() {
+               if ( isset( $_REQUEST['uselang'] ) ) {
+                       $lang = $_REQUEST['uselang'];
+
+               } elseif ( isset( $_SESSION['uselang'] ) ) {
+                       $lang = $_SESSION['uselang'];
 
                } else {
-                       $lang = $default;
+                       $lang = $this->defaultLang;
                }
+
+               if ( !in_array( $lang, $this->getLangs() ) ) {
+                       $lang = $this->defaultLang;
+               }
+
+               // remember this language selection for future requests
+               $_SESSION['uselang'] = $lang;
 
                $this->lang = $lang;
                return $lang;
@@ -99,6 +111,10 @@
         * @return string Message
         */
        public function message( $key, $params = array() ) {
+               if ( $this->lang === null ) {
+                       $this->getLang();
+               }
+
                // Try the language, then english, then fail
                if ( isset( $this->messages[$this->lang][$key] ) ) {
                        $msg = $this->messages[$this->lang][$key];

-- 
To view, visit https://gerrit.wikimedia.org/r/101175
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6c7047bfd5e6241bac636f84971600c742aaae57
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/wikimania-scholarships
Gerrit-Branch: master
Gerrit-Owner: BryanDavis <bda...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to