code: private function getLocale(Request $request): string
      {
          if (null !== $request->attributes->get('_locale')) {
              return $this->formatLocaleId($request->attributes->get('_locale'));
          }
  
          return $request->getPreferredLanguage($this->availableLocales);
      }
docs: Returns the locale from the request, the session or the HTTP header.