We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit 74a89b1f authored by Brion Vibber's avatar Brion Vibber

Locale switch cleanup: use common_switch_locale() which is safer for updating...

Locale switch cleanup: use common_switch_locale() which is safer for updating gettext state. Also moved a few calls to reduce chance of hitting an exception before switching back.
Should help with problems where xmppdaemon would get stuck in wrong locale.
parent 114df398
...@@ -224,9 +224,6 @@ function mail_subscribe_notify_profile($listenee, $other) ...@@ -224,9 +224,6 @@ function mail_subscribe_notify_profile($listenee, $other)
if ($other->hasRight(Right::EMAILONSUBSCRIBE) && if ($other->hasRight(Right::EMAILONSUBSCRIBE) &&
$listenee->email && $listenee->emailnotifysub) { $listenee->email && $listenee->emailnotifysub) {
// use the recipient's localization
common_init_locale($listenee->language);
$profile = $listenee->getProfile(); $profile = $listenee->getProfile();
$name = $profile->getBestName(); $name = $profile->getBestName();
...@@ -236,6 +233,9 @@ function mail_subscribe_notify_profile($listenee, $other) ...@@ -236,6 +233,9 @@ function mail_subscribe_notify_profile($listenee, $other)
$recipients = $listenee->email; $recipients = $listenee->email;
// use the recipient's localization
common_switch_locale($listenee->language);
$headers = _mail_prepare_headers('subscribe', $listenee->nickname, $other->nickname); $headers = _mail_prepare_headers('subscribe', $listenee->nickname, $other->nickname);
$headers['From'] = mail_notify_from(); $headers['From'] = mail_notify_from();
$headers['To'] = $name . ' <' . $listenee->email . '>'; $headers['To'] = $name . ' <' . $listenee->email . '>';
...@@ -271,7 +271,7 @@ function mail_subscribe_notify_profile($listenee, $other) ...@@ -271,7 +271,7 @@ function mail_subscribe_notify_profile($listenee, $other)
common_local_url('emailsettings')); common_local_url('emailsettings'));
// reset localization // reset localization
common_init_locale(); common_switch_locale();
mail_send($recipients, $headers, $body); mail_send($recipients, $headers, $body);
} }
} }
...@@ -473,7 +473,7 @@ function mail_confirm_sms($code, $nickname, $address) ...@@ -473,7 +473,7 @@ function mail_confirm_sms($code, $nickname, $address)
function mail_notify_nudge($from, $to) function mail_notify_nudge($from, $to)
{ {
common_init_locale($to->language); common_switch_locale($to->language);
// TRANS: Subject for 'nudge' notification email // TRANS: Subject for 'nudge' notification email
$subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname); $subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname);
...@@ -491,7 +491,7 @@ function mail_notify_nudge($from, $to) ...@@ -491,7 +491,7 @@ function mail_notify_nudge($from, $to)
$from->nickname, $from->nickname,
common_local_url('all', array('nickname' => $to->nickname)), common_local_url('all', array('nickname' => $to->nickname)),
common_config('site', 'name')); common_config('site', 'name'));
common_init_locale(); common_switch_locale();
$headers = _mail_prepare_headers('nudge', $to->nickname, $from->nickname); $headers = _mail_prepare_headers('nudge', $to->nickname, $from->nickname);
...@@ -525,7 +525,7 @@ function mail_notify_message($message, $from=null, $to=null) ...@@ -525,7 +525,7 @@ function mail_notify_message($message, $from=null, $to=null)
return true; return true;
} }
common_init_locale($to->language); common_switch_locale($to->language);
// TRANS: Subject for direct-message notification email // TRANS: Subject for direct-message notification email
$subject = sprintf(_('New private message from %s'), $from->nickname); $subject = sprintf(_('New private message from %s'), $from->nickname);
...@@ -549,7 +549,7 @@ function mail_notify_message($message, $from=null, $to=null) ...@@ -549,7 +549,7 @@ function mail_notify_message($message, $from=null, $to=null)
$headers = _mail_prepare_headers('message', $to->nickname, $from->nickname); $headers = _mail_prepare_headers('message', $to->nickname, $from->nickname);
common_init_locale(); common_switch_locale();
return mail_to_user($to, $subject, $body, $headers); return mail_to_user($to, $subject, $body, $headers);
} }
...@@ -577,7 +577,7 @@ function mail_notify_fave($other, $user, $notice) ...@@ -577,7 +577,7 @@ function mail_notify_fave($other, $user, $notice)
$bestname = $profile->getBestName(); $bestname = $profile->getBestName();
common_init_locale($other->language); common_switch_locale($other->language);
// TRANS: Subject for favorite notification email // TRANS: Subject for favorite notification email
$subject = sprintf(_('%s (@%s) added your notice as a favorite'), $bestname, $user->nickname); $subject = sprintf(_('%s (@%s) added your notice as a favorite'), $bestname, $user->nickname);
...@@ -605,7 +605,7 @@ function mail_notify_fave($other, $user, $notice) ...@@ -605,7 +605,7 @@ function mail_notify_fave($other, $user, $notice)
$headers = _mail_prepare_headers('fave', $other->nickname, $user->nickname); $headers = _mail_prepare_headers('fave', $other->nickname, $user->nickname);
common_init_locale(); common_switch_locale();
mail_to_user($other, $subject, $body, $headers); mail_to_user($other, $subject, $body, $headers);
} }
......
...@@ -34,6 +34,14 @@ function common_user_error($msg, $code=400) ...@@ -34,6 +34,14 @@ function common_user_error($msg, $code=400)
$err->showPage(); $err->showPage();
} }
/**
* This should only be used at setup; processes switching languages
* to send text to other users should use common_switch_locale().
*
* @param string $language Locale language code (optional; empty uses
* current user's preference or site default)
* @return mixed success
*/
function common_init_locale($language=null) function common_init_locale($language=null)
{ {
if(!$language) { if(!$language) {
...@@ -50,6 +58,15 @@ function common_init_locale($language=null) ...@@ -50,6 +58,15 @@ function common_init_locale($language=null)
return $ok; return $ok;
} }
/**
* Initialize locale and charset settings and gettext with our message catalog,
* using the current user's language preference or the site default.
*
* This should generally only be run at framework initialization; code switching
* languages at runtime should call common_switch_language().
*
* @access private
*/
function common_init_language() function common_init_language()
{ {
mb_internal_encoding('UTF-8'); mb_internal_encoding('UTF-8');
......
...@@ -272,12 +272,12 @@ function remove_facebook_app($flink) ...@@ -272,12 +272,12 @@ function remove_facebook_app($flink)
function mail_facebook_app_removed($user) function mail_facebook_app_removed($user)
{ {
common_init_locale($user->language);
$profile = $user->getProfile(); $profile = $user->getProfile();
$site_name = common_config('site', 'name'); $site_name = common_config('site', 'name');
common_switch_locale($user->language);
$subject = sprintf( $subject = sprintf(
_m('Your %1$s Facebook application access has been disabled.', _m('Your %1$s Facebook application access has been disabled.',
$site_name)); $site_name));
...@@ -291,7 +291,7 @@ function mail_facebook_app_removed($user) ...@@ -291,7 +291,7 @@ function mail_facebook_app_removed($user)
"re-installing the %2\$s Facebook application.\n\nRegards,\n\n%2\$s"), "re-installing the %2\$s Facebook application.\n\nRegards,\n\n%2\$s"),
$user->nickname, $site_name); $user->nickname, $site_name);
common_init_locale(); common_switch_locale();
return mail_to_user($user, $subject, $body); return mail_to_user($user, $subject, $body);
} }
...@@ -335,10 +335,10 @@ function remove_twitter_link($flink) ...@@ -335,10 +335,10 @@ function remove_twitter_link($flink)
function mail_twitter_bridge_removed($user) function mail_twitter_bridge_removed($user)
{ {
common_init_locale($user->language);
$profile = $user->getProfile(); $profile = $user->getProfile();
common_switch_locale($user->language);
$subject = sprintf(_m('Your Twitter bridge has been disabled.')); $subject = sprintf(_m('Your Twitter bridge has been disabled.'));
$site_name = common_config('site', 'name'); $site_name = common_config('site', 'name');
...@@ -354,7 +354,7 @@ function mail_twitter_bridge_removed($user) ...@@ -354,7 +354,7 @@ function mail_twitter_bridge_removed($user)
common_local_url('twittersettings'), common_local_url('twittersettings'),
common_config('site', 'name')); common_config('site', 'name'));
common_init_locale(); common_switch_locale();
return mail_to_user($user, $subject, $body); return mail_to_user($user, $subject, $body);
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment