translate() public method

Given a message key, returns an appropriately translated full-text string
public translate ( string $message_key, array $args = [], string $language = "" ) : string
$message_key string The short message code
$args array An array of arguments to pass through vsprintf().
$language string Optionally, the standard language code (defaults to site/user default, then English)
return string Either the translated string, the English string, or the original language string.
Beispiel #1
0
 /**
  * Get body for the notification
  *
  * Plugin can define a subtype specific body simply by providing a
  * translation for the string "notification:body:<action>:<type>:<subtype".
  *
  * For example in mod/blog/languages/en.php:
  *
  *    'notification:body:publish:object:blog' => '
  *         Hi %s!
  *
  *         %s has created a new post called "%s" in the group %s.
  *
  *         It says:
  *
  *         "%s"
  *
  *         You can comment the post here:
  *         %s
  *     ',
  *
  * The arguments passed into the translation are:
  *     1. Recipient's name
  *     2. Name of the user who triggered the notification
  *     3. Title of the content
  *     4. Name of the content's container
  *     5. The actual content (entity's 'description' field)
  *     6. URL to the content
  *
  * Argument swapping can be used to change the order of the parameters.
  * See http://php.net/manual/en/function.sprintf.php#example-5427
  *
  * @param NotificationEvent $event     Notification event
  * @param ElggUser          $recipient Notification recipient
  * @return string Notification body in the recipient's language
  */
 private function getNotificationBody(NotificationEvent $event, ElggUser $recipient)
 {
     $actor = $event->getActor();
     $object = $event->getObject();
     /* @var \ElggObject $object */
     $language = $recipient->language;
     // Check custom notification body for the action/type/subtype combination
     $body_key = "notification:{$event->getDescription()}:body";
     if ($this->translator->languageKeyExists($body_key, $language)) {
         if ($object instanceof \ElggEntity) {
             $display_name = $object->getDisplayName();
             $container_name = '';
             $container = $object->getContainerEntity();
             if ($container) {
                 $container_name = $container->getDisplayName();
             }
         } else {
             $display_name = '';
             $container_name = '';
         }
         return $this->translator->translate($body_key, array($recipient->name, $actor->name, $display_name, $container_name, $object->description, $object->getURL()), $language);
     }
     // Fall back to default body
     return $this->translator->translate('notification:body', array($object->getURL()), $language);
 }
Beispiel #2
0
 /**
  * Upgrades Elgg Database and code
  *
  * @return bool
  */
 protected function processUpgrades()
 {
     $dbversion = (int) $this->datalist->get('version');
     if ($this->upgradeCode($dbversion)) {
         system_message($this->translator->translate('upgrade:core'));
         // Now we trigger an event to give the option for plugins to do something
         $upgrade_details = new \stdClass();
         $upgrade_details->from = $dbversion;
         $upgrade_details->to = elgg_get_version();
         $this->events->trigger('upgrade', 'upgrade', $upgrade_details);
         return true;
     }
     return false;
 }
Beispiel #3
0
 /**
  * Get a user by GUID even if the entity is hidden or disabled
  *
  * @param int $guid User GUID. Default is logged in user
  *
  * @return ElggUser|false
  * @throws UserFetchFailureException
  * @access private
  */
 public function getUserForPermissionsCheck($guid = 0)
 {
     if (!$guid) {
         return $this->session->getLoggedInUser();
     }
     // need to ignore access and show hidden entities for potential hidden/disabled users
     $ia = $this->session->setIgnoreAccess(true);
     $show_hidden = access_show_hidden_entities(true);
     $user = $this->get($guid, 'user');
     $this->session->setIgnoreAccess($ia);
     access_show_hidden_entities($show_hidden);
     if (!$user) {
         // requested to check access for a specific user_guid, but there is no user entity, so the caller
         // should cancel the check and return false
         $message = $this->translator->translate('UserFetchFailureException', array($guid));
         $this->logger->warn($message);
         throw new UserFetchFailureException();
     }
     return $user;
 }
Beispiel #4
0
 public function testDoesNotProcessArgsOnKey()
 {
     $this->assertEquals('nonexistent:%s', $this->translator->translate('nonexistent:%s', [1]));
 }