Localizable.php 673B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace Illuminate\Support\Traits;
  3. trait Localizable
  4. {
  5. /**
  6. * Run the callback with the given locale.
  7. *
  8. * @param string $locale
  9. * @param \Illuminate\Contracts\Translation\Translator $translator
  10. * @param \Closure $callback
  11. * @return bool
  12. */
  13. public function withLocale($locale, $translator, $callback)
  14. {
  15. if (! $locale || ! $translator) {
  16. return $callback();
  17. }
  18. $original = $translator->getLocale();
  19. try {
  20. $translator->setLocale($locale);
  21. return $callback();
  22. } finally {
  23. $translator->setLocale($original);
  24. }
  25. }
  26. }