Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Add event manager as soft dependency to translator #4187

Merged
merged 3 commits into from
Apr 29, 2013
Merged

Add event manager as soft dependency to translator #4187

merged 3 commits into from
Apr 29, 2013

Conversation

DASPRiD
Copy link
Member

@DASPRiD DASPRiD commented Apr 5, 2013

This PR allows users to attach to two new events in the translator:

  • loadMessages.no-messages-loaded which will always fire when messages for a specific locale/text-domain combination could not be loaded
  • getTranslatedMessage.missing-translation which will always fire when a message in a specific locale/text-domain could not be found

By default, events are disabled, and the translator will not try to create an event manager instance. This makes it a rather nice soft dependency, without having it as requirement.

@macnibblet
Copy link
Contributor

Awesome, very much needed for creating a web UI for handling translations! 👍

@bakura10
Copy link
Contributor

bakura10 commented Apr 6, 2013

Naming of events is a bit strange: getTranslatedMessage.missing-translation

Why first part is camelCased and second part dash-separated (I'm just asking the question, I always have trouble choosing names for events) (btw you could add class constants too, that would simplify things)

$events->setIdentifiers(array(
__CLASS__,
get_called_class(),
'module_manager',
Copy link
Contributor

Choose a reason for hiding this comment

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

Ups :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, whoopS… We need traits :D

@prolic
Copy link
Contributor

prolic commented Apr 9, 2013

Agree with @bakura10.
Please add a class constant with the event name(s).
I saw you accidentally copied "module_manager" as event identifier, please remove that.

Besides these notes, I am good for it. Thanks.

@weierophinney
Copy link
Member

I agree with @bakura10 -- keep a consistent style in the event naming. Either don't prefix the events with the method name, or use camelCasing in the second segment of the event name:

  • loadMessages.no-messages-loaded becomes either no-messages-loaded or loadMessages.noMessagesLoaded
  • getTranslatedMessage.missing-translation becomes either missing-translation or getTranslatedMessage.missingTranslation

I think with those changes, it's ready.

@bakura10
Copy link
Contributor

What abut simply "noMessagesLoaded", "missingTranslation" ?

(note: camelCased was used here, so I suggest we stick with that: https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/ResponseSender/SendResponseEvent.php#L20)

@weierophinney
Copy link
Member

@bakura10 I'm fine with those, too -- I just want to see consistency! :)

@iquabius
Copy link
Contributor

I think we shouldn't use the method's name as prefix for event names in the shipped ZF2 events, so IMO @bakura10 suggestion is the best choice.

{
$events->setIdentifiers(array(
__CLASS__,
get_called_class(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Does get_called_class() and get_class($this) have the same behavior when called in a non-static method?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, I remember something about this: http://us2.php.net/manual/en/function.get-called-class.php#94331

@DASPRiD change this to get_class($this), please.

Copy link
Contributor

Choose a reason for hiding this comment

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

No! Why? get_called_class() is even faster and has the same result.

Copy link
Member

Choose a reason for hiding this comment

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

@prolic Read the link -- it has unreliable behavior when used in a non-static method; it was built to work inside a static method, not an instance method. We changed the EventManagerAware trait to use get_class($this) for the same reason.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the hint. Then we have some more bugs in ZF2. I will provide fixes for them tomorrow.
E.g. see: https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Controller/AbstractController.php

Copy link
Contributor

Choose a reason for hiding this comment

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

One fix quick done: #4235

@weierophinney
Copy link
Member

I think we shouldn't use the method's name as prefix for event names in the shipped ZF2 events

@wryck7 We have messaged since well before stable that event names should typically be ___FUNCTION__ in order to aid discovery. The only time we typically deviate from this is when multiple events may be triggered from within a method, or if the event triggered is only a small part of the workflow of a method.

*
* @var EventManagerInterface
*/
protected $events;
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't $eventManager make more sense here?

@DASPRiD
Copy link
Member Author

DASPRiD commented Apr 28, 2013

Changes applied, ready to merge.

@prolic
Copy link
Contributor

prolic commented Apr 28, 2013

good to merge

)
);
}

$this->messages[$textDomain][$locale] = null;
Copy link
Member

Choose a reason for hiding this comment

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

Question: might we want to change the logic here to allow setting the messages based on the last return from triggering the event? In other words, if $results->last() is an array, we could set the messages...

Copy link
Contributor

Choose a reason for hiding this comment

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

+1, I missed that here.

weierophinney added a commit that referenced this pull request Apr 29, 2013
…r-integration

Add event manager as soft dependency to translator

Conflicts:
	library/Zend/I18n/composer.json
weierophinney added a commit that referenced this pull request Apr 29, 2013
- If a string value is returned from the `EVENT_MISSING_TRANSLATION`
  event, we should use it as the translation.
- If a `TextDomain` is returned from the `EVENT_NO_MESSAGES_LOADED`
  event, we should use it for the text domain.
weierophinney added a commit that referenced this pull request Apr 29, 2013
@weierophinney weierophinney merged commit e376710 into zendframework:develop Apr 29, 2013
@ghost ghost assigned weierophinney Apr 29, 2013
weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
…ure/translator-event-manager-integration

Add event manager as soft dependency to translator

Conflicts:
	library/Zend/I18n/composer.json
weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
- If a string value is returned from the `EVENT_MISSING_TRANSLATION`
  event, we should use it as the translation.
- If a `TextDomain` is returned from the `EVENT_NO_MESSAGES_LOADED`
  event, we should use it for the text domain.
weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants