Skip to content

[Translation] Add translation provider events #52376

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

Closed
wants to merge 5 commits into from

Conversation

wickedOne
Copy link
Contributor

@wickedOne wickedOne commented Oct 31, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #49520
License MIT

two new events to allow mutation of the translator bag just before it's send to the provider or written to the filesystem.
briefly discussed over here: #49231 (comment)

@carsonbot carsonbot added this to the 6.4 milestone Oct 31, 2023
@welcoMattic welcoMattic modified the milestones: 6.4, 7.1 Oct 31, 2023
@OskarStark OskarStark changed the title [Translation] translation provider events [Translation] Add translation provider events Oct 31, 2023
@wickedOne
Copy link
Contributor Author

just to be sure: does this need documenting?

@welcoMattic
Copy link
Member

@wickedOne can you provide more context about your use case, and why translation pull/push events are needed?

Note that 6.4/7.0 are closed for new features and you will need to target 7.1 branch as soon as it will be created.

@wickedOne
Copy link
Contributor Author

as stated in this comment #49231 (comment) there are several usecases which would benefit of having these events available before the write (be it either to the provider or the filesystem) action.

of course there are other ways around it, but this is the least hacky one imo

@welcoMattic
Copy link
Member

Can you report use cases here in the PR description to ease the review for everyone? Thanks

@wickedOne
Copy link
Contributor Author

the events were added to enable easy access for post processing of translations.

one real life example for instance is a translation with references to other translations.

<trans-unit id="Nip4l6f" resname="general.confirm">
  <source>general.confirm</source>
  <target>are you sure?</target>
</trans-unit>
<trans-unit id="OqbB6Xw" resname="general.confirm.really">
  <source>general.confirm.really</source>
  <target>really, {general.confim}</target>
</trans-unit>

this is not supported by for instance Phrase, so with the pull event you can resolve & replace those references yourself before the translations are written to the filesystem.

another example would be resolving cascading fallback locales for pull-, or adding a ticket number as note metadata for push events.

@wickedOne
Copy link
Contributor Author

thanks for the review @OskarStark
changes committed accordingly

Copy link
Contributor

@OskarStark OskarStark left a comment

Choose a reason for hiding this comment

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

After a CHANGELOG entry is added

@wickedOne
Copy link
Contributor Author

After a CHANGELOG entry is added

thanks, will do that the 7.1 branch is created

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Nov 20, 2023

Branch 7.1 now created ;)

- added translation pull & push event
@stof
Copy link
Member

stof commented Nov 20, 2023

references between translations is not supported by the Symfony translator either, so I don't see why the lack of support in Phrase would be an issue. So I would still like to understand the actual use case.

defaultLocale: $defaultLocale,
transPaths: [$this->translationAppDir.'/translations'],
enabledLocales: $locales,
eventDispatcher: $dispatcher
Copy link
Member

Choose a reason for hiding this comment

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

As Symfony does not provide BC for argument names (except for attribute constructors), I would not use them in tests: this would hide BC breaks changing the parameter list by adding a new parameter in the middle.

use Symfony\Component\Translation\Provider\FilteringProvider;
use Symfony\Component\Translation\Provider\TranslationProviderCollection;
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
use Symfony\Component\Translation\TranslatorBag;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
Copy link
Member

Choose a reason for hiding this comment

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

As this only dispatches an event, it should depend on the PSR interface instead to be more flexible.

@wickedOne
Copy link
Contributor Author

references between translations is not supported by the Symfony translator either, so I don't see why the lack of support in Phrase would be an issue. So I would still like to understand the actual use case.

correct, that isn't supported by either, just like cascading locales, so dispatching these events is to provide a little bit more flexibility to the end user in regards to post-processing of translations

@wickedOne
Copy link
Contributor Author

@stof changes applied in 06320bb

@welcoMattic
Copy link
Member

As we discussed with @OskarStark during SymfonyCon Brussels hackday, the provided use case can easily be handled by using decorator pattern (decorate any TranslationProvider and pre/post process what you need in your decorator).

I'm not in favor of merging this PR for now.

@nicolas-grekas
Copy link
Member

Closing then. Thanks @wickedOne for proposing and @welcoMattic + @OskarStark for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Translation] Translation provider event system
6 participants