Skip to content

dynamic i18n locales in next.config.js #54762

Answered by simpel
simpel asked this question in App Router
Discussion options

You must be logged in to vote

I ended up solving this by creating a NPM script that fetches all languages and generates a JS file with an array of langs. I then read that file in the next config like this

const locales = require('./data/locales/locales.js')

const nextConfig = {
  i18n: {
    locales: locales,
    defaultLocale: 'en-gb',
  },
  async redirects() { 
    return [
      {
        source: '/quiz/:locale',
        destination: '/:locale/quiz',
        permanent: true,
      
      }
    ] 
  }
...
}

Heres the script I wrote to fetch and create the file in /bin/fetchLocales.js

#! /usr/bin/env node

const fs = require('fs')
const fetch = require('node-fetch')
const dotenv = require('dotenv');

const envs = d…

Replies: 1 comment 2 replies

Comment options

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

@sakhmedbayev
Comment options

Answer selected by simpel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants