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()); }
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); }