@@ -746,7 +746,69 @@ export const LanguageRouter = (
746746 } ;
747747
748748
749- const expressHandler = async ( req : any , res : any , next ?: any ) => {
749+
750+ /**
751+ * Handles language detection and translation loading for a standard HTTP request.
752+ * @param req Request object that should be handled.
753+ * @returns Object containing redirect and redirectResponseCode if a redirect should be done.
754+ */
755+ const handleHttpRequest = ( req : Request ) : LanguageNextResponse => {
756+ if ( ! loadedLangs ) preloadSync ( ) ;
757+ const { uri, lang, pathUri } = detectLanguage ( req . url , req . headers ) ;
758+ const isRoot = req . url . length <= 1 ;
759+
760+ const response : LanguageNextResponse = {
761+ language : lang ,
762+ languages : [ ...languagesSorted ] ,
763+ path : pathUri ,
764+ } ;
765+
766+ // redirect root if not language prefixed
767+ if ( redirectRoot && isRoot ) {
768+ response . redirect = '/' + lang + '/' ;
769+ response . redirectResponseCode = redirectRootResponseCode ;
770+ }
771+
772+ // update cookie
773+ if ( cookieName && cookieUpdate ) {
774+ response . cookie = {
775+ name : cookieName ,
776+ value : lang ,
777+ options : {
778+ expire : cookieExpire ,
779+ domain : cookieDomain ? cookieDomain : undefined ,
780+ path : cookiePath ? cookiePath : undefined ,
781+ }
782+ } ;
783+ }
784+
785+ // add language attribute to request object
786+ if ( languageAttr ) {
787+ ( req as any ) [ languageAttr ] = lang ;
788+ ( req as any ) [ languageAttr + 's' ] = [ ...languagesSorted ] ;
789+ }
790+
791+ // add translations attribute to request
792+ if ( translationsAttr ) {
793+ response . translations = _getTranslations ( lang , defaultLang , [ ] , translationsDir ) ;
794+ ( req as any ) [ translationsAttr ] = response . translations ;
795+ }
796+
797+ // add path attribute to request object
798+ if ( pathAttr ) ( req as any ) [ pathAttr ] = pathUri ;
799+
800+ return response ;
801+ } ;
802+
803+
804+
805+ /**
806+ * Can be passed to an express app to handle language detection and translation loading.
807+ * @param req Request object that should be handled.
808+ * @param res Response object to which the response should be written.
809+ * @param next Function that should be called if the request should be passed to the next middleware.
810+ */
811+ const handleExpress = async ( req : any , res : any , next ?: any ) => {
750812 if ( ! loadedLangs ) await preload ( ) ;
751813 const { uri, lang, pathUri } = await detectLanguage ( req . url , req . headers ) ;
752814 const isRoot = req . url . length <= 1 ;
@@ -793,21 +855,21 @@ export const LanguageRouter = (
793855 * Optionally preload LanguageRouter so first request can be handled faster.
794856 * @returns Promise<void> that resolves when preloading is done.
795857 */
796- expressHandler . preload = preload ;
858+ handleExpress . preload = preload ;
797859
798860 /**
799861 * Optionally preload LanguageRouter so first request can be handled faster.
800862 */
801- expressHandler . preloadSync = preloadSync ;
863+ handleExpress . preloadSync = preloadSync ;
802864
803865 /**
804866 * Can be called inside a Next.js middleware to handle language detection and translation loading.
805867 * @param req NextRequest object that should be handled.
806868 * @returns Object containing redirect and redirectResponseCode if a redirect should be done.
807869 */
808- expressHandler . nextJsMiddlewareHandler = nextJsMiddlewareHandler ;
870+ handleExpress . nextJsMiddlewareHandler = nextJsMiddlewareHandler ;
809871
810- return expressHandler ;
872+ return handleExpress ;
811873} ;
812874
813875export default {
0 commit comments