Skip to content

Commit

Permalink
use receipents' language for translating email templates
Browse files Browse the repository at this point in the history
  • Loading branch information
following5 committed Aug 31, 2015
1 parent 4e4a242 commit d715c14
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 37 deletions.
10 changes: 2 additions & 8 deletions bin/clear-webcache.php
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
27 changes: 26 additions & 1 deletion htdocs/config2/locale.inc.php
Expand Up @@ -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']
Expand All @@ -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';
Expand All @@ -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';
Expand All @@ -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';
Expand All @@ -76,13 +81,15 @@
$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');
$opt['locale']['IT']['locales'] = array('it_IT.utf8', 'it_IT', 'it');
$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');
Expand All @@ -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';
Expand Down Expand Up @@ -181,6 +189,7 @@
$opt['locale']['FR']['page']['license'] = '<a rel="license" href="%1"><img alt="Creative Commons License Terms" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-nd/3.0/de/88x31.png" /></a><div style="text-align:center; margin:8px 0 0 6px;">Le<a href="articles.php?page=impressum#datalicense">contenu</a> de Opencaching.de sont sous licence Creative Commons <a rel="license" href="%1" target="_blank">BY-BC-ND 3.0 DE</a>.</div>';
$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';
Expand Down Expand Up @@ -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!
}

?>
13 changes: 1 addition & 12 deletions htdocs/lib/common.inc.php
Expand Up @@ -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');
}

Expand Down
13 changes: 1 addition & 12 deletions htdocs/lib2/common.inc.php
Expand Up @@ -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');
}

Expand Down
1 change: 1 addition & 0 deletions htdocs/lib2/logic/user.class.php
Expand Up @@ -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'];

Expand Down
27 changes: 23 additions & 4 deletions htdocs/lib2/mail.class.php
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions htdocs/lib2/smarty/ocplugins/block.t.php
Expand Up @@ -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.
***************************************************************************/

/**
Expand Down

0 comments on commit d715c14

Please sign in to comment.