From d715c144f8f76f72cd6e65d67438fe9bc53e6ccd Mon Sep 17 00:00:00 2001 From: following Date: Mon, 31 Aug 2015 14:58:31 +0200 Subject: [PATCH] use receipents' language for translating email templates --- bin/clear-webcache.php | 10 ++------- htdocs/config2/locale.inc.php | 27 +++++++++++++++++++++++- htdocs/lib/common.inc.php | 13 +----------- htdocs/lib2/common.inc.php | 13 +----------- htdocs/lib2/logic/user.class.php | 1 + htdocs/lib2/mail.class.php | 27 ++++++++++++++++++++---- htdocs/lib2/smarty/ocplugins/block.t.php | 4 ++++ 7 files changed, 58 insertions(+), 37 deletions(-) diff --git a/bin/clear-webcache.php b/bin/clear-webcache.php index 973f2e954..7c1f4f471 100644 --- a/bin/clear-webcache.php +++ b/bin/clear-webcache.php @@ -90,10 +90,7 @@ function createMenuCache() { // cheating a little bit $opt['template']['locale'] = $sLanguage; - setlocale(LC_MONETARY, $opt['locale'][$opt['template']['locale']]['locales']); - setlocale(LC_TIME, $opt['locale'][$opt['template']['locale']]['locales']); - if (defined('LC_MESSAGES')) - setlocale(LC_MESSAGES, $opt['locale'][$opt['template']['locale']]['locales']); + set_php_locale(); if ($translate->t('INTERNAL_LANG', 'all', 'OcSmarty.class.php', '') != $sLanguage) { @@ -176,10 +173,7 @@ function precompileTemplateWithLanguage($sTemplate, $sLanguage) // cheating a little bit $opt['template']['locale'] = $sLanguage; - setlocale(LC_MONETARY, $opt['locale'][$opt['template']['locale']]['locales']); - setlocale(LC_TIME, $opt['locale'][$opt['template']['locale']]['locales']); - if (defined('LC_MESSAGES')) - setlocale(LC_MESSAGES, $opt['locale'][$opt['template']['locale']]['locales']); + set_php_locale(); if ($translate->t('INTERNAL_LANG', 'all', 'OcSmarty.class.php', '') != $sLanguage) { diff --git a/htdocs/config2/locale.inc.php b/htdocs/config2/locale.inc.php index 85859ca20..e307a7b8d 100644 --- a/htdocs/config2/locale.inc.php +++ b/htdocs/config2/locale.inc.php @@ -10,7 +10,7 @@ * ATTENTION: This file is also used in old template system. * (this means any call to framework functions may be incompatible) * - * Only set the following keys in $opt[] + * Only set the following keys in $opt[]: * * $opt['template']['locales'] * $opt['geokrety']['locales'] @@ -36,6 +36,9 @@ $opt['template']['locales']['FR']['flag'] = 'images/flag/FR.png'; $opt['template']['locales']['FR']['name'] = 'Français'; +/* + Disabling unused locales speeds up template precompilation (clear-webcache.php). + $opt['template']['locales']['SV']['show'] = true; // sv_SE $opt['template']['locales']['SV']['flag'] = 'images/flag/SE.png'; $opt['template']['locales']['SV']['name'] = 'Svenska'; @@ -60,6 +63,7 @@ $opt['template']['locales']['JA']['show'] = true; // ja_JP $opt['template']['locales']['JA']['flag'] = 'images/flag/JP.png'; $opt['template']['locales']['JA']['name'] = '日本語'; +*/ // geokrety language key association $opt['geokrety']['locales']['DE'] = 'de_DE.UTF-8'; @@ -68,6 +72,7 @@ $opt['geokrety']['locales']['ES'] = 'es_ES.UTF-8'; $opt['geokrety']['locales']['FR'] = 'fr_FR.UTF-8'; +/* $opt['geokrety']['locales']['SV'] = 'sv_SE.UTF-8'; $opt['geokrety']['locales']['NO'] = 'no_NO.UTF-8'; $opt['geokrety']['locales']['NL'] = 'en_EN'; @@ -76,6 +81,7 @@ $opt['geokrety']['locales']['DA'] = 'da_DK.UTF-8'; $opt['geokrety']['locales']['PT'] = 'pt_PT.UTF-8'; $opt['geokrety']['locales']['JA'] = 'ja_JP.UTF-8'; +*/ $opt['locale']['DE']['locales'] = array('de_DE.utf8', 'de_DE@euro', 'de_DE', 'de', 'ge'); $opt['locale']['EN']['locales'] = array('en_US.utf8', 'en_US', 'en'); @@ -83,6 +89,7 @@ $opt['locale']['ES']['locales'] = array('es_ES.utf8', 'es_ES', 'es'); $opt['locale']['FR']['locales'] = array('fr_FR.utf8', 'fr_FR@euro', 'fr_FR', 'french', 'fr'); +/* $opt['locale']['SV']['locales'] = array('sv_SE.utf8', 'sv_SE', 'se'); $opt['locale']['NO']['locales'] = array('no_NO.utf8', 'no_NO', 'no'); $opt['locale']['PL']['locales'] = array('pl_PL.utf8', 'pl_PL', 'pl'); @@ -91,6 +98,7 @@ $opt['locale']['DA']['locales'] = array('da_DK.utf8', 'da_DK', 'dk'); $opt['locale']['PT']['locales'] = array('pt_PT.utf8', 'pt_PT', 'pt'); $opt['locale']['JA']['locales'] = array('ja_JP.utf8', 'ja_JP', 'jp'); +*/ $opt['locale']['EN']['format']['dm'] = '%m/%d'; $opt['locale']['EN']['format']['dateshort'] = '%m/%d/%y'; @@ -181,6 +189,7 @@ $opt['locale']['FR']['page']['license'] = 'Creative Commons License Terms
Lecontenu de Opencaching.de sont sous licence Creative Commons BY-BC-ND 3.0 DE.
'; $opt['locale']['FR']['helpwiki'] = "http://wiki.opencaching.de/index.php/"; +/* $opt['locale']['SV']['format']['dateshort'] = '%y-%m-%d'; $opt['locale']['SV']['format']['dm'] = '%d/%m'; $opt['locale']['SV']['format']['date'] = '%Y-%m-%d'; @@ -296,5 +305,21 @@ $opt['locale']['JA']['country'] = 'JP'; $opt['locale']['JA']['page']['subtitle1'] = 'Opencachingとジオキャッシング'; $opt['locale']['JA']['page']['subtitle2'] = ''; +*/ + +function set_php_locale() +{ + global $opt; + + setlocale(LC_MONETARY, $opt['locale'][$opt['template']['locale']]['locales']); + setlocale(LC_TIME, $opt['locale'][$opt['template']['locale']]['locales']); + if (defined('LC_MESSAGES')) + setlocale(LC_MESSAGES, $opt['locale'][$opt['template']['locale']]['locales']); + + // no localisation! + setlocale(LC_COLLATE, $opt['locale']['EN']['locales']); + setlocale(LC_CTYPE, $opt['locale']['EN']['locales']); + setlocale(LC_NUMERIC, $opt['locale']['EN']['locales']); // important for mysql-queries! +} ?> diff --git a/htdocs/lib/common.inc.php b/htdocs/lib/common.inc.php index eea13a2fa..7ee884c28 100644 --- a/htdocs/lib/common.inc.php +++ b/htdocs/lib/common.inc.php @@ -1121,18 +1121,7 @@ function load_gettext() $opt['template']['locale'] = $locale; bindtextdomain('messages', $rootpath . '/cache2/translate'); - - // setup the PHP locale - setlocale(LC_MONETARY, $opt['locale'][$locale]['locales']); - setlocale(LC_TIME, $opt['locale'][$locale]['locales']); - if (defined('LC_MESSAGES')) - setlocale(LC_MESSAGES, $opt['locale'][$locale]['locales']); - - // no localisation! - setlocale(LC_COLLATE, $opt['locale']['EN']['locales']); - setlocale(LC_CTYPE, $opt['locale']['EN']['locales']); - setlocale(LC_NUMERIC, $opt['locale']['EN']['locales']); // important for mysql-queries! - + set_php_locale(); textdomain('messages'); } diff --git a/htdocs/lib2/common.inc.php b/htdocs/lib2/common.inc.php index 442cd46cf..c687e22c6 100644 --- a/htdocs/lib2/common.inc.php +++ b/htdocs/lib2/common.inc.php @@ -230,18 +230,7 @@ function set_language() $cookie->set('locale', $opt['template']['locale'], $opt['template']['default']['locale']); bindtextdomain('messages', $opt['rootpath'] . 'cache2/translate'); - - // setup the PHP locale - setlocale(LC_MONETARY, $opt['locale'][$opt['template']['locale']]['locales']); - setlocale(LC_TIME, $opt['locale'][$opt['template']['locale']]['locales']); - if (defined('LC_MESSAGES')) - setlocale(LC_MESSAGES, $opt['locale'][$opt['template']['locale']]['locales']); - - // no localisation! - setlocale(LC_COLLATE, $opt['locale']['EN']['locales']); - setlocale(LC_CTYPE, $opt['locale']['EN']['locales']); - setlocale(LC_NUMERIC, $opt['locale']['EN']['locales']); // important for mysql-queries! - + set_php_locale(); textdomain('messages'); } diff --git a/htdocs/lib2/logic/user.class.php b/htdocs/lib2/logic/user.class.php index 48e80c3f9..538b1942e 100644 --- a/htdocs/lib2/logic/user.class.php +++ b/htdocs/lib2/logic/user.class.php @@ -683,6 +683,7 @@ function sendEMail($nFromUserId, $sSubject, $sText, $bSendEMailAddress) $mail = new mail(); $mail->name = 'usercontactmail'; $mail->to = $this->getEMail(); + $mail->recipient_locale = $this->getLanguageCode(); $mail->from = $opt['mail']['usermail']; diff --git a/htdocs/lib2/mail.class.php b/htdocs/lib2/mail.class.php index 8c839e3c2..ccd70e51d 100644 --- a/htdocs/lib2/mail.class.php +++ b/htdocs/lib2/mail.class.php @@ -12,6 +12,7 @@ class mail extends Smarty var $name = 'sys_nothing'; var $main_template = 'sys_main'; var $compile_id = null; + var $recipient_locale = null; var $from = ''; var $to = ''; @@ -59,26 +60,44 @@ function assign_rs($name, $rs) function send() { - global $tpl, $opt, $login; + global $tpl, $opt; if (!$this->template_exists($this->name . '.tpl')) $tpl->error(ERROR_MAIL_TEMPLATE_NOT_FOUND); $this->assign('template', $this->name); + if (!$this->recipient_locale) + $this->recipient_locale = $opt['template']['locale']; $optn['mail']['contact'] = $opt['mail']['contact']; $optn['page']['absolute_url'] = $opt['page']['absolute_url']; - $optn['format'] = $opt['locale'][$opt['template']['locale']]['format']; + $optn['format'] = $opt['locale'][$this->recipient_locale]['format']; $this->assign('opt', $optn); $this->assign('to', $this->to); $this->assign('from', $this->from); $this->assign('subject', $this->subject); - $llogin['username'] = isset($login) ? $login->username : ''; - $this->assign('login', $llogin); + // This is nasty, but as there is only a global translation system + // (based on gettext) and there are no precompiled, language-dependend email + // templates available, we must temporarily change the locale according to + // the recipient's locale. If some error occurs while running fetch(), + // the error message may be displayed in the recipient's language. + + $sender_locale = $opt['template']['locale']; + if ($this->recipient_locale != $sender_locale) + { + $opt['template']['locale'] = $this->recipient_locale; + set_php_locale(); + } $body = $this->fetch($this->main_template . '.tpl', '', $this->get_compile_id()); + if ($this->recipient_locale != $sender_locale) + { + $opt['template']['locale'] = $sender_locale; + set_php_locale(); + } + // check if the target domain exists if the domain does not // exist, the mail is sent to the own domain (?!) $domain = mail::getToMailDomain($this->to); diff --git a/htdocs/lib2/smarty/ocplugins/block.t.php b/htdocs/lib2/smarty/ocplugins/block.t.php index 46ad3ec3f..c20fc17c1 100644 --- a/htdocs/lib2/smarty/ocplugins/block.t.php +++ b/htdocs/lib2/smarty/ocplugins/block.t.php @@ -6,6 +6,10 @@ * * For more information about this smarty-extension see * prefilter.t.php + * + * This extension probably is OBSOLETE: {t} blocks are handled by + * prefilter.t.php when precompiling the templates via clear-webcache.php. + * smarty_block_t() probably never gets called. ***************************************************************************/ /**