tobytwigger / laravel-translate

Supercharged instant translations for any Laravel project.
https://tobytwigger.github.io/laravel-translate
MIT License
6 stars 9 forks source link

Method Illuminate\Translation\Translator::getDefault does not exist. #31

Closed sheinfeld closed 2 years ago

sheinfeld commented 3 years ago

Hi @tobytwigger

After installing your package, I've had some troubles with my laravel app.

Do you have any idea what is the conflict?

[2021-08-16 08:30:37] local.ERROR: Method Illuminate\Translation\Translator::getDefault does not exist. {"exception":"[object] (BadMethodCallException(code: 0): Method Illuminate\\Translation\\Translator::getDefault does not exist. at [PATH]/vendor/laravel/framework/src/Illuminate/Macroable/Traits/Macroable.php:103)
[stacktrace]
#0 [PATH]/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(261): Illuminate\\Translation\\Translator->__call()
#1 [PATH]/vendor/symfony/translation/Translator.php(162): Illuminate\\Support\\Facades\\Facade::__callStatic()
#2 [PATH]/vendor/nesbot/carbon/src/Carbon/Translator.php(327): Symfony\\Component\\Translation\\Translator->getLocale()
#3 [PATH]/vendor/symfony/translation/Translator.php(94): Carbon\\Translator->setLocale()
#4 [PATH]/vendor/nesbot/carbon/src/Carbon/Translator.php(81): Symfony\\Component\\Translation\\Translator->__construct()
#5 [PATH]/vendor/nesbot/carbon/src/Carbon/Translator.php(70): Carbon\\Translator->__construct()
#6 [PATH]/vendor/nesbot/carbon/src/Carbon/Traits/Localization.php(691): Carbon\\Translator::get()
#7 [PATH]/vendor/nesbot/carbon/src/Carbon/Traits/Localization.php(728): Carbon\\Carbon::translator()
#8 [PATH]/vendor/nesbot/carbon/src/Carbon/Traits/Localization.php(480): Carbon\\Carbon::getLocaleAwareTranslator()
#9 [PATH]/vendor/nesbot/carbon/src/Carbon/Laravel/ServiceProvider.php(40): Carbon\\Carbon::setLocale()
#10 [PATH]/vendor/nesbot/carbon/src/Carbon/Laravel/ServiceProvider.php(20): Carbon\\Laravel\\ServiceProvider->updateLocale()
#11 [PATH]/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Carbon\\Laravel\\ServiceProvider->boot()
#12 [PATH]/vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#13 [PATH]/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure()
#14 [PATH]/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod()
#15 [PATH]/vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\\Container\\BoundMethod::call()
#16 [PATH]/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(914): Illuminate\\Container\\Container->call()
#17 [PATH]/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(895): Illuminate\\Foundation\\Application->bootProvider()
#18 [internal function]: Illuminate\\Foundation\\Application->Illuminate\\Foundation\\{closure}()
#19 [PATH]/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(896): array_walk()
#20 [PATH]/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php(17): Illuminate\\Foundation\\Application->boot()
#21 [PATH]/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(237): Illuminate\\Foundation\\Bootstrap\\BootProviders->bootstrap()
#22 [PATH]/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(310): Illuminate\\Foundation\\Application->bootstrapWith()
#23 [PATH]/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(127): Illuminate\\Foundation\\Console\\Kernel->bootstrap()
#24 [PATH]/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle()
#25 {main}
"} 
tobytwigger commented 3 years ago

Hey @sheinfeld , thanks for the bug report.

For information, this is a trace of what causes the issue:

  1. The Carbon service provider calls Carbon::setLocale() - https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Laravel/ServiceProvider.php#L40
  2. It gets the translator so the locale can be set - https://github.com/briannesbitt/Carbon/blob/95681e675906446864ba8130722b63792fe2d8f4/src/Carbon/Traits/Localization.php#L480
  3. The translator is retrieved, which create a new \Carbon\Translator class - https://github.com/briannesbitt/Carbon/blob/95681e675906446864ba8130722b63792fe2d8f4/src/Carbon/Traits/Localization.php#L691
  4. This extends the \Symfony\Component\Translation\Translator class - https://github.com/symfony/translation/blob/5.3/Translator.php
  5. It tries to set the locale, but that function is overridden in \Carbon\Translator - https://github.com/briannesbitt/Carbon/blob/95681e675906446864ba8130722b63792fe2d8f4/src/Carbon/Translator.php#L314
  6. This calls getLocale to get the previous locale before changing it. - https://github.com/briannesbitt/Carbon/blob/95681e675906446864ba8130722b63792fe2d8f4/src/Carbon/Translator.php#L327

Then https://github.com/symfony/translation/blob/5.3/Translator.php#L162 is the bit that actually causes a problem - since the base locale isn't found, it calls \Locale::getDefault(), which presumably works in symfony as \Locale will be a symfony class, but for Laravel this is the laravel translator, which doesn't have a getDefault() function.

My only suggestions are to make sure you define an initial locale, setting it in config: https://github.com/laravel/laravel/blob/8.x/config/app.php#L83, and running composer update. If this doesn't work, it's an issue with carbon so should be raised there.

sheinfeld commented 3 years ago

@tobytwigger that's the thinig... I have set a locale and a fallback_locale...

tobytwigger commented 3 years ago

From the look of it then it looks like a problem with Carbon. If you uninstall this package, does the problem go away? If it doesn't, comment out seconds of the translation service provider to see if you can identify what's causing the problem.

Failing that, raise this with the Carbon library

tobytwigger commented 2 years ago

This was caused by a \Locale facade being defined in config/app.php