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. |
public function translate($message_key, array $args = [], $language = "")
{
// TODO find a way to cache getLanguage() and get rid of this
static $CURRENT_LANGUAGE;
if (!is_string($message_key) || strlen($message_key) < 1) {
_elgg_services()->logger->warn('$message_key needs to be a string in ' . __METHOD__ . '(), ' . gettype($message_key) . ' provided');
return '';
}
if (!$CURRENT_LANGUAGE) {
$CURRENT_LANGUAGE = $this->getCurrentLanguage();
}
if (!$language) {
$language = $CURRENT_LANGUAGE;
}
$this->ensureTranslationsLoaded($language);
$notice = '';
$string = $message_key;
// avoid dupes without overhead of array_unique
$langs[$language] = true;
$langs['en'] = true;
foreach (array_keys($langs) as $try_lang) {
if (isset($GLOBALS['_ELGG']->translations[$try_lang][$message_key])) {
$string = $GLOBALS['_ELGG']->translations[$try_lang][$message_key];
// only pass through if we have arguments to allow backward compatibility
// with manual sprintf() calls.
if ($args) {
$string = vsprintf($string, $args);
}
break;
} else {
$notice = sprintf('Missing %s translation for "%s" language key', $try_lang === 'en' ? 'English' : $try_lang, $message_key);
}
}
if ($notice) {
_elgg_services()->logger->notice($notice);
}
return $string;
}
/** * 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); }