Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

untranslated callback, monolog integration #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Kdyby/Translation/DI/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TranslationExtension extends Nette\DI\CompilerExtension
self::RESOLVER_REQUEST => TRUE,
self::RESOLVER_HEADER => TRUE,
),
'hookToMonolog' => TRUE, // checks for \Monolog\Logger and automatically hooks logging of untranslated messages
);

/**
Expand Down Expand Up @@ -100,6 +101,10 @@ public function loadConfiguration()
$catalogueCompiler->addSetup('enableDebugMode');
}

if ( $config['hookToMonolog'] && class_exists('\Monolog\Logger' ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather depend on the PSR interface than on monolog.

$translator->addSetup('setLogger', array( '@Monolog\Logger' ));
}

$this->loadLocaleResolver($config);

$builder->addDefinition($this->prefix('helpers'))
Expand Down
33 changes: 33 additions & 0 deletions src/Kdyby/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ class Translator extends BaseTranslator implements ITranslator
*/
private $localeWhitelist;

/** @var callable[] */
public $onUntranslated;

/** @var \Monolog\Logger */
private $logger;

/**
* @param IUserLocaleResolver $localeResolver
Expand Down Expand Up @@ -134,6 +138,9 @@ public function translate($message, $count = NULL, $parameters = array(), $domai
return $message;

} elseif ($message instanceof Nette\Utils\Html) {
if (!empty($this->onUntranslated) )
$this->onUntranslated( $message );

if ($this->panel) {
$this->panel->markUntranslated($message);
}
Expand Down Expand Up @@ -173,6 +180,9 @@ public function trans($message, array $parameters = array(), $domain = NULL, $lo

$result = parent::trans($id, $parameters, $domain, $locale);
if ($result === "\x01") {
if (!empty($this->onUntranslated) )
$this->onUntranslated( $message );

if ($this->panel !== NULL) {
$this->panel->markUntranslated($message);
}
Expand Down Expand Up @@ -211,6 +221,9 @@ public function transChoice($message, $number, array $parameters = array(), $dom
}

if ($result === "\x01") {
if (!empty($this->onUntranslated) )
$this->onUntranslated( $message );

if ($this->panel !== NULL) {
$this->panel->markUntranslated($message);
}
Expand Down Expand Up @@ -416,6 +429,26 @@ public static function buildWhitelistRegexp($whitelist)
return $whitelist ? '~^(' . implode('|', $whitelist) . ')~i' : NULL;
}

/**
* @param \Monolog\Logger $logger
* @return Translator
*/
public function setLogger( $logger ) {
$this->logger = $logger;
if ( $logger !== NULL ) {
$this->onUntranslated[] = array( $this, 'logUntranslated' );
}
return $this;
}

/**
* @param string $message
*/
public function logUntranslated($message) {
if ( $this->logger !== NULL ) {
$this->logger->warning( "Untranslated message '{$message}'" );
}
}


/*************************** Nette\Object ***************************/
Expand Down