@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * Change the effective locale for the lifetime of this guard.
5 *
6 * Use @{method:PhabricatorEnv::beginScopedLocale} to acquire a guard.
7 * Guards are released when they exit scope.
8 */
9final class PhabricatorLocaleScopeGuard
10 extends Phobject {
11
12 private static $stack = array();
13 private $key;
14 private $destroyed;
15
16 public function __construct($code) {
17 // If this is the first time we're building a guard, push the default
18 // locale onto the bottom of the stack. We'll never remove it.
19 if (empty(self::$stack)) {
20 self::$stack[] = PhabricatorEnv::getLocaleCode();
21 }
22
23 // If there's no locale, use the server default locale.
24 if (!$code) {
25 $code = self::$stack[0];
26 }
27
28 // Push this new locale onto the stack and set it as the active locale.
29 // We keep track of which key this guard owns, in case guards are destroyed
30 // out-of-order.
31 self::$stack[] = $code;
32 $this->key = last_key(self::$stack);
33
34 PhabricatorEnv::setLocaleCode($code);
35 }
36
37 public function __destruct() {
38 if ($this->destroyed) {
39 return;
40 }
41 $this->destroyed = true;
42
43 // Remove this locale from the stack and set the new active locale. Usually,
44 // we're the last item on the stack, so this shortens the stack by one item
45 // and sets the locale underneath. However, it's possible that guards are
46 // being destroyed out of order, so we might just be removing an item
47 // somewhere in the middle of the stack. In this case, we won't actually
48 // change the locale, just set it to its current value again.
49
50 unset(self::$stack[$this->key]);
51 PhabricatorEnv::setLocaleCode(end(self::$stack));
52 }
53
54}