Skip to content

Commit

Permalink
Adding a check to be sure that the locale exists
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfallet authored and Nathan Fallet committed Apr 21, 2022
1 parent 93318ea commit 361b192
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Sources/LingoVapor/LocaleRedirectMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public final class LocaleRedirectMiddleware: Middleware {
public init() {}
public func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
// Check if a language is passed in url
guard let lang = request.parameters.get("locale")
guard let locale = request.parameters.get("locale")
else {
// Redirect corresponding to specified locale
// For example, `/home/` will redirect to `/<request.locale>/home/`
Expand All @@ -22,9 +22,15 @@ public final class LocaleRedirectMiddleware: Middleware {
))
}

// Check that the locale is available, else it's a 404
guard locales.contains(locale)
else {
return request.eventLoop.makeFailedFuture(Abort(.notFound))
}

// Set request locale from url
// For example, `/fr/home/` will display homepage in French
request.session.data["locale"] = locales.contains(lang) ? lang : lingo.defaultLocale
request.session.data["locale"] = locale
return next.respond(to: request)
}
}

0 comments on commit 361b192

Please sign in to comment.