Skip to content

Localized urls for multilanguage website (same as mcamara / laravel-localization) #3468

Answered by Julien-R44
ElyesaErgezen asked this question in Help
Discussion options

You must be logged in to vote

I haven't tested it but normally something like this should work:

Route.group(() => {
    Route.get('/', 'IndexController.index')
}).prefix(':lang').middleware('validateLangPrefix')

Then you could make a middleware that validates that the prefix :lang is valid

export default class ValidateLangPrefix {
  public async handle({ request }: HttpContextContract, next: () => Promise<void>) {
    const lang = request.param('lang')

    if (!['fr', 'en', 'it'].includes(lang.toLowerCase())) {
      // Throw error or return 404 for you users
      throw new Error(`Invalid locale: ${lang}`)
    }

    await next()
  }
}

Read this for more information on how to make middleware:
https://docs.adonisjs.c…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ElyesaErgezen
Comment options

@Allviinn
Comment options

Answer selected by ElyesaErgezen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #3460 on January 03, 2022 16:12.