getRequest(); if ( !$request->wasPosted() ) { $this->dieWithError( [ 'apierror-mustbeposted', $request->getText( 'action' ) ] ); } $languageCode = $request->getText( 'languagecode' ); if ( !Language::isSupportedLanguage( $languageCode ) ) { $this->dieWithError( [ 'apierror-invalidlang', $this->encodeParamName( 'languagecode' ) ] ); } $user = $this->getUser(); if ( $user->isAnon() ) { if ( $this->getConfig()->get( 'ULSAnonCanChangeLanguage' ) ) { // Anonymous users can change language. // Use a cookie that also can changed by JavaScript. $request->response()->setCookie( 'language', $languageCode, 0, [ 'httpOnly' => false ] ); return; } $this->dieWithError( [ 'apierror-ulssetlang-anon-notallowed' ] ); } $updateUser = $user->getInstanceForUpdate(); $updateUser->setOption( 'language', $languageCode ); // Sync the DB on post-send DeferredUpdates::addCallableUpdate( function () use ( $updateUser ) { $updateUser->saveSettings(); } ); } public function getAllowedParams() { return [ 'languagecode' => [ ApiBase::PARAM_REQUIRED => true, ] ]; } public function isInternal() { // Try to scare people away from using this externally return true; } public function needsToken() { return 'csrf'; } }