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

Your translator implementation overwrites other 3rd party translator implementations #5406

Open
torian257x opened this issue Mar 4, 2022 · 3 comments
Labels

Comments

@torian257x
Copy link

Bug description

Statamic\Extensions\Translation\Translator

the problem is, if someone else registers a translator themselves, yours can get priority
and even though you call

CpServiceProvider

$this->app->extend('translator', function ($translator, $app) {
            $extended = new Translator($app['files'], $translator->getLoader(), $translator->getLocale());
            $extended->setFallback($translator->getFallback());

            return $extended;
        });

the problem is that the original $translator never gets the chance to do their own get()

not sure what is the best solution... magic methods maybe?

How to reproduce

say, try to use statamic together with laravel-translation-manager

or any translation package that has its own translator implementation

Logs

No response

Versions

$ php please support:details
Statamic 3.2.35 Pro
Laravel 8.83.3
PHP 8.1.2
edalzell/blade-directives 3.5.2
optimoapps/statamic-bard-text-align 1.0.2
pecotamic/sitemap 1.2.7

Installation

Existing Laravel app

Additional details

No response

@torian257x
Copy link
Author

torian257x commented Mar 4, 2022

I found some info:

I don't think you can do it from your package itself, it would be quite weird and a security nightmare, but you can always do it from the application that consume both of these packages via a key in composer.json : https://laravel.com/docs/9.x/packages#opting-out-of-package-discovery.

I don't know if your package is always installed alongside statamic, but you could offer both your standard service provider and an AggregateServiceProvider that contain yours and statamic main one.

Edit : I'm pretty sure service providers can only be run once, so adding this in your package service provider might do the trick.

class MyServiceProvider extends ServiceProvider
{
    public function register()
    {
        // Force it to run first if installed
        if (class_exists('Statamic\Providers\StatamicServiceProvider')) {
            $this->app->register('Statamic\Providers\StatamicServiceProvider');
        }

        // ...
    }

    public function boot()
    {
        // ...
    }
}

If what you want to do to the translater can be done with macro, registering those now should work as both the base translator is macroable and statamic extend the base one. If not it complicate things a bit.

@ryanmitchell
Copy link
Contributor

Surely the answer is to bind your own translator?

$this->app->extend('translator', function ($translator, $app) { return new MyTranslator(); });

which then works out which translator to refer to for each string...

@torian257x
Copy link
Author

no... the problem is the statamic translator is not a decorator

instead, you do hand around only the loader and locale

see here for an example of decorator

https://laravel.com/docs/9.x/container#extending-bindings

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

No branches or pull requests

3 participants