From 48ebcf06795072a91a32ffcfbac11339e4ae075f Mon Sep 17 00:00:00 2001 From: vrana Date: Thu, 14 Jun 2012 18:08:06 -0700 Subject: [PATCH] Allow user override translation and implement PhutilPerson Test Plan: Altered database. Wrote a custom translation and selected it in preferences. Verified that the text is custom translated. Set language back to default. Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Maniphest Tasks: T1139 Differential Revision: https://secure.phabricator.com/D2757 --- resources/sql/patches/usertranslation.sql | 2 ++ src/__phutil_library_map__.php | 6 +++- .../base/controller/PhabricatorController.php | 11 ++++++ ...atorUserProfileSettingsPanelController.php | 34 ++++++++++++++++--- .../people/storage/PhabricatorUser.php | 12 ++++++- .../setup/sql/PhabricatorBuiltinPatchList.php | 4 +++ 6 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 resources/sql/patches/usertranslation.sql diff --git a/resources/sql/patches/usertranslation.sql b/resources/sql/patches/usertranslation.sql new file mode 100644 index 0000000000..912b3f43c6 --- /dev/null +++ b/resources/sql/patches/usertranslation.sql @@ -0,0 +1,2 @@ +ALTER TABLE `{$NAMESPACE}_user`.`user` + ADD `translation` varchar(64) COLLATE utf8_bin AFTER `sex`; diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index d33fd1f048..6e0c9ba95d 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1908,7 +1908,11 @@ phutil_register_library_map(array( 'PhabricatorUIPagerExample' => 'PhabricatorUIExample', 'PhabricatorUITooltipExample' => 'PhabricatorUIExample', 'PhabricatorUnitsTestCase' => 'PhabricatorTestCase', - 'PhabricatorUser' => 'PhabricatorUserDAO', + 'PhabricatorUser' => + array( + 0 => 'PhabricatorUserDAO', + 1 => 'PhutilPerson', + ), 'PhabricatorUserAccountSettingsPanelController' => 'PhabricatorUserSettingsPanelController', 'PhabricatorUserConduitSettingsPanelController' => 'PhabricatorUserSettingsPanelController', 'PhabricatorUserDAO' => 'PhabricatorLiskDAO', diff --git a/src/applications/base/controller/PhabricatorController.php b/src/applications/base/controller/PhabricatorController.php index 11f195bf2b..33d7d84fa3 100644 --- a/src/applications/base/controller/PhabricatorController.php +++ b/src/applications/base/controller/PhabricatorController.php @@ -60,6 +60,17 @@ abstract class PhabricatorController extends AphrontController { } } + $translation = $user->getTranslation(); + if ($translation && + $translation != PhabricatorEnv::getEnvConfig('translation.provider') && + class_exists($translation) && + is_subclass_of($translation, 'PhabricatorTranslation')) { + $translation = newv($translation, array()); + PhutilTranslator::getInstance() + ->setLanguage($translation->getLanguage()) + ->addTranslations($translation->getTranslations()); + } + $request->setUser($user); if ($user->getIsDisabled() && $this->shouldRequireEnabledUser()) { diff --git a/src/applications/people/controller/settings/panels/PhabricatorUserProfileSettingsPanelController.php b/src/applications/people/controller/settings/panels/PhabricatorUserProfileSettingsPanelController.php index 4856a04d5a..c0f2807705 100644 --- a/src/applications/people/controller/settings/panels/PhabricatorUserProfileSettingsPanelController.php +++ b/src/applications/people/controller/settings/panels/PhabricatorUserProfileSettingsPanelController.php @@ -41,12 +41,16 @@ final class PhabricatorUserProfileSettingsPanelController $profile->setBlurb($request->getStr('blurb')); $sex = $request->getStr('sex'); - if (in_array($sex, array('m', 'f'))) { + $sexes = array(PhutilPerson::SEX_MALE, PhutilPerson::SEX_FEMALE); + if (in_array($sex, $sexes)) { $user->setSex($sex); } else { $user->setSex(null); } + // Checked in runtime. + $user->setTranslation($request->getStr('translation')); + if (!empty($_FILES['image'])) { $err = idx($_FILES['image'], 'error'); if ($err != UPLOAD_ERR_NO_FILE) { @@ -111,11 +115,27 @@ final class PhabricatorUserProfileSettingsPanelController $profile_uri = PhabricatorEnv::getURI('/p/'.$user->getUsername().'/'); $sexes = array( - '' => 'Unknown', - 'm' => 'Male', - 'f' => 'Female', + PhutilPerson::SEX_UNKNOWN => 'Unknown', + PhutilPerson::SEX_MALE => 'Male', + PhutilPerson::SEX_FEMALE => 'Female', ); + $translations = array(); + $symbols = id(new PhutilSymbolLoader()) + ->setType('class') + ->setAncestorClass('PhabricatorTranslation') + ->setConcreteOnly(true) + ->selectAndLoadSymbols(); + foreach ($symbols as $symbol) { + $class = $symbol['name']; + $translations[$class] = newv($class, array())->getName(); + } + asort($translations); + $default = PhabricatorEnv::newObjectFromConfig('translation.provider'); + $translations = array( + '' => 'Sever Default ('.$default->getName().')', + ) + $translations; + $form = new AphrontFormView(); $form ->setUser($request->getUser()) @@ -133,6 +153,12 @@ final class PhabricatorUserProfileSettingsPanelController ->setLabel('Sex') ->setName('sex') ->setValue($user->getSex())) + ->appendChild( + id(new AphrontFormSelectControl()) + ->setOptions($translations) + ->setLabel('Translation') + ->setName('translation') + ->setValue($user->getTranslation())) ->appendChild( id(new AphrontFormMarkupControl()) ->setLabel('Profile URI') diff --git a/src/applications/people/storage/PhabricatorUser.php b/src/applications/people/storage/PhabricatorUser.php index 3f54a6efe3..56482894a6 100644 --- a/src/applications/people/storage/PhabricatorUser.php +++ b/src/applications/people/storage/PhabricatorUser.php @@ -16,7 +16,7 @@ * limitations under the License. */ -final class PhabricatorUser extends PhabricatorUserDAO { +final class PhabricatorUser extends PhabricatorUserDAO implements PhutilPerson { const SESSION_TABLE = 'phabricator_session'; const NAMETOKEN_TABLE = 'user_nametoken'; @@ -25,6 +25,7 @@ final class PhabricatorUser extends PhabricatorUserDAO { protected $userName; protected $realName; protected $sex; + protected $translation; protected $passwordSalt; protected $passwordHash; protected $profileImagePHID; @@ -90,6 +91,11 @@ final class PhabricatorUser extends PhabricatorUserDAO { return $this; } + // To satisfy PhutilPerson. + public function getSex() { + return $this->sex; + } + public function isLoggedIn() { return !($this->getPHID() === null); } @@ -624,6 +630,10 @@ EOBODY; return $this->getUsername().' ('.$this->getRealName().')'; } + public function __toString() { + return $this->getUsername(); + } + public static function loadOneWithEmailAddress($address) { $email = id(new PhabricatorUserEmail())->loadOneWhere( 'address = %s', diff --git a/src/infrastructure/setup/sql/PhabricatorBuiltinPatchList.php b/src/infrastructure/setup/sql/PhabricatorBuiltinPatchList.php index 5c3c664595..8da08b96c3 100644 --- a/src/infrastructure/setup/sql/PhabricatorBuiltinPatchList.php +++ b/src/infrastructure/setup/sql/PhabricatorBuiltinPatchList.php @@ -891,6 +891,10 @@ final class PhabricatorBuiltinPatchList extends PhabricatorSQLPatchList { 'type' => 'sql', 'name' => $this->getPatchPath('threadtopic.sql'), ), + 'usertranslation.sql' => array( + 'type' => 'sql', + 'name' => $this->getPatchPath('usertranslation.sql'), + ), ); }