1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-18 12:52:42 +01:00

Send mail to targets in the user's translation

Summary: Ref T6367.

Test Plan:
  - Added and executed unit tests.
  - Sent mail to A (en_US) and B (en_A*).
  - Got one mail in English and one mail in ENGLISH.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T6367

Differential Revision: https://secure.phabricator.com/D13142
This commit is contained in:
epriestley 2015-06-03 10:49:27 -07:00
parent 6db97bde12
commit 069e60d2ff
7 changed files with 117 additions and 7 deletions

View file

@ -1976,6 +1976,8 @@ phutil_register_library_map(array(
'PhabricatorListFilterUIExample' => 'applications/uiexample/examples/PhabricatorListFilterUIExample.php',
'PhabricatorLocalDiskFileStorageEngine' => 'applications/files/engine/PhabricatorLocalDiskFileStorageEngine.php',
'PhabricatorLocalTimeTestCase' => 'view/__tests__/PhabricatorLocalTimeTestCase.php',
'PhabricatorLocaleScopeGuard' => 'infrastructure/internationalization/scope/PhabricatorLocaleScopeGuard.php',
'PhabricatorLocaleScopeGuardTestCase' => 'infrastructure/internationalization/scope/__tests__/PhabricatorLocaleScopeGuardTestCase.php',
'PhabricatorLogTriggerAction' => 'infrastructure/daemon/workers/action/PhabricatorLogTriggerAction.php',
'PhabricatorLogoutController' => 'applications/auth/controller/PhabricatorLogoutController.php',
'PhabricatorLunarPhasePolicyRule' => 'applications/policy/rule/PhabricatorLunarPhasePolicyRule.php',
@ -5389,6 +5391,8 @@ phutil_register_library_map(array(
'PhabricatorListFilterUIExample' => 'PhabricatorUIExample',
'PhabricatorLocalDiskFileStorageEngine' => 'PhabricatorFileStorageEngine',
'PhabricatorLocalTimeTestCase' => 'PhabricatorTestCase',
'PhabricatorLocaleScopeGuard' => 'Phobject',
'PhabricatorLocaleScopeGuardTestCase' => 'PhabricatorTestCase',
'PhabricatorLogTriggerAction' => 'PhabricatorTriggerAction',
'PhabricatorLogoutController' => 'PhabricatorAuthController',
'PhabricatorLunarPhasePolicyRule' => 'PhabricatorPolicyRule',

View file

@ -114,10 +114,7 @@ abstract class PhabricatorController extends AphrontController {
$request->setUser($user);
}
$locale_code = $user->getTranslation();
if ($locale_code) {
PhabricatorEnv::setLocaleCode($locale_code);
}
PhabricatorEnv::setLocaleCode($user->getTranslation());
$preferences = $user->loadPreferences();
if (PhabricatorEnv::getEnvConfig('darkconsole.enabled')) {

View file

@ -42,7 +42,8 @@ final class PhabricatorRepositoryPushMailWorker
$task_data = $this->getTaskData();
$viewer = $target->getViewer();
// TODO: Swap locale to viewer locale.
$locale = PhabricatorEnv::beginScopedLocale($viewer->getTranslation());
$logs = $event->getLogs();

View file

@ -2139,8 +2139,10 @@ abstract class PhabricatorApplicationTransactionEditor
$mailed = array();
foreach ($targets as $target) {
$original_actor = $this->getActor();
$this->setActor($target->getViewer());
// TODO: Swap locale to viewer locale.
$viewer = $target->getViewer();
$this->setActor($viewer);
$locale = PhabricatorEnv::beginScopedLocale($viewer->getTranslation());
$caught = null;
try {
@ -2153,6 +2155,8 @@ abstract class PhabricatorApplicationTransactionEditor
}
$this->setActor($original_actor);
unset($locale);
if ($caught) {
throw $ex;
}

View file

@ -129,7 +129,19 @@ final class PhabricatorEnv {
self::setLocaleCode('en_US');
}
public static function beginScopedLocale($locale_code) {
return new PhabricatorLocaleScopeGuard($locale_code);
}
public static function getLocaleCode() {
return self::$localeCode;
}
public static function setLocaleCode($locale_code) {
if (!$locale_code) {
return;
}
if ($locale_code == self::$localeCode) {
return;
}

View file

@ -0,0 +1,54 @@
<?php
/**
* Change the effective locale for the lifetime of this guard.
*
* Use @{method:PhabricatorEnv::beginScopedLocale} to acquire a guard.
* Guards are released when they exit scope.
*/
final class PhabricatorLocaleScopeGuard
extends Phobject {
private static $stack = array();
private $key;
private $destroyed;
public function __construct($code) {
// If this is the first time we're building a guard, push the default
// locale onto the bottom of the stack. We'll never remove it.
if (empty(self::$stack)) {
self::$stack[] = PhabricatorEnv::getLocaleCode();
}
// If there's no locale, use the server default locale.
if (!$code) {
$code = self::$stack[0];
}
// Push this new locale onto the stack and set it as the active locale.
// We keep track of which key this guard owns, in case guards are destroyed
// out-of-order.
self::$stack[] = $code;
$this->key = last_key(self::$stack);
PhabricatorEnv::setLocaleCode($code);
}
public function __destruct() {
if ($this->destroyed) {
return;
}
$this->destroyed = true;
// Remove this locale from the stack and set the new active locale. Usually,
// we're the last item on the stack, so this shortens the stack by one item
// and sets the locale underneath. However, it's possible that guards are
// being destroyed out of order, so we might just be removing an item
// somewhere in the middle of the stack. In this case, we won't actually
// change the locale, just set it to its current value again.
unset(self::$stack[$this->key]);
PhabricatorEnv::setLocaleCode(end(self::$stack));
}
}

View file

@ -0,0 +1,38 @@
<?php
final class PhabricatorLocaleScopeGuardTestCase
extends PhabricatorTestCase {
public function testLocaleScopeGuard() {
$original = PhabricatorEnv::getLocaleCode();
// Set a guard; it should change the locale, then revert it when destroyed.
$guard = PhabricatorEnv::beginScopedLocale('en_GB');
$this->assertEqual('en_GB', PhabricatorEnv::getLocaleCode());
unset($guard);
$this->assertEqual($original, PhabricatorEnv::getLocaleCode());
// Nest guards, then destroy them out of order.
$guard1 = PhabricatorEnv::beginScopedLocale('en_GB');
$this->assertEqual('en_GB', PhabricatorEnv::getLocaleCode());
$guard2 = PhabricatorEnv::beginScopedLocale('en_A*');
$this->assertEqual('en_A*', PhabricatorEnv::getLocaleCode());
unset($guard1);
$this->assertEqual('en_A*', PhabricatorEnv::getLocaleCode());
unset($guard2);
$this->assertEqual($original, PhabricatorEnv::getLocaleCode());
// If you push `null`, that should mean "the default locale", not
// "the current locale".
$guard3 = PhabricatorEnv::beginScopedLocale('en_GB');
$this->assertEqual('en_GB', PhabricatorEnv::getLocaleCode());
$guard4 = PhabricatorEnv::beginScopedLocale(null);
$this->assertEqual($original, PhabricatorEnv::getLocaleCode());
unset($guard4);
$this->assertEqual('en_GB', PhabricatorEnv::getLocaleCode());
unset($guard3);
$this->assertEqual($original, PhabricatorEnv::getLocaleCode());
}
}