From b5163c483c88914ac17e94e275871996f12fc210 Mon Sep 17 00:00:00 2001
From: Frederic Guillot <fred@kanboard.net>
Date: Wed, 22 Apr 2015 18:46:04 -0400
Subject: Send notifications with the language of the recipient

---
 app/Model/Config.php       |  4 +++-
 app/Model/Notification.php | 28 ++++++++++++++++++++++------
 2 files changed, 25 insertions(+), 7 deletions(-)

(limited to 'app')

diff --git a/app/Model/Config.php b/app/Model/Config.php
index bbcff50c..813cc84f 100644
--- a/app/Model/Config.php
+++ b/app/Model/Config.php
@@ -117,15 +117,17 @@ class Config extends Base
             'fr_FR' => 'fr',
             'it_IT' => 'it',
             'hu_HU' => 'hu',
+            'nl_NL' => 'nl',
             'pl_PL' => 'pl',
             'pt_BR' => 'pt-br',
             'ru_RU' => 'ru',
+            'sr_Latn_RS' => 'sr',
             'fi_FI' => 'fi',
             'sv_SE' => 'sv',
+            'tr_TR' => 'tr',
             'zh_CN' => 'zh-cn',
             'ja_JP' => 'ja',
             'th_TH' => 'th',
-            'tr_TR' => 'tr',
         );
 
         $lang = $this->getCurrentLanguage();
diff --git a/app/Model/Notification.php b/app/Model/Notification.php
index 8414b121..5835de54 100644
--- a/app/Model/Notification.php
+++ b/app/Model/Notification.php
@@ -3,6 +3,7 @@
 namespace Model;
 
 use Core\Session;
+use Core\Translator;
 use Swift_Message;
 use Swift_Mailer;
 use Swift_TransportException;
@@ -36,7 +37,7 @@ class Notification extends Base
 
             return $this->db
                         ->table(User::TABLE)
-                        ->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email')
+                        ->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language')
                         ->eq('notifications_enabled', '1')
                         ->neq('email', '')
                         ->notin(User::TABLE.'.id', $exclude_users)
@@ -45,7 +46,7 @@ class Notification extends Base
 
         return $this->db
             ->table(ProjectPermission::TABLE)
-            ->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email')
+            ->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language')
             ->join(User::TABLE, 'id', 'user_id')
             ->eq('project_id', $project_id)
             ->eq('notifications_enabled', '1')
@@ -110,19 +111,34 @@ class Notification extends Base
 
             $mailer = Swift_Mailer::newInstance($this->container['mailer']);
 
-            $message = Swift_Message::newInstance()
+            foreach ($users as $user) {
+
+                $this->container['logger']->debug('Send email notification to '.$user['username'].' lang='.$user['language']);
+
+                // Use the user language otherwise use the application language (do not use the session language)
+                if (! empty($user['language'])) {
+                    Translator::load($user['language']);
+                }
+                else {
+                    Translator::load($this->config->get('application_language', 'en_US'));
+                }
+
+                // Send the message
+                $message = Swift_Message::newInstance()
                             ->setSubject($this->getMailSubject($template, $data))
                             ->setFrom(array(MAIL_FROM => $author ?: 'Kanboard'))
-                            ->setBody($this->getMailContent($template, $data), 'text/html');
+                            ->setBody($this->getMailContent($template, $data), 'text/html')
+                            ->setTo(array($user['email'] => $user['name'] ?: $user['username']));
 
-            foreach ($users as $user) {
-                $message->setTo(array($user['email'] => $user['name'] ?: $user['username']));
                 $mailer->send($message);
             }
         }
         catch (Swift_TransportException $e) {
             $this->container['logger']->error($e->getMessage());
         }
+
+        // Restore locales
+        $this->config->setupTranslations();
     }
 
     /**
-- 
cgit v1.2.3