React and NextJS: How can i detect the Location of my Client?
Asked Answered
B

1

7

I have created a Website using NextJS and React. I want to provide the Website in several languages. To achieve this, I wanted to create in the page folder, subfolders, for example for the English language /en, for the German language, /de etc.

When a visitor then opens my Website, the correct subfolder is selected based on the public IP:

Example: Request comes from America : the /pages/en folder is selected, The request comes from Germany : the /pages/de folder is selected etc.

How can I do that? Since I have little experience in NodeJS environments, please show me concrete examples, many thanks...

I already have a server.js file. Maybe I can use this yes for it too...

const express = require('express')
const next = require('next')
const bodyParser = require('body-parser')
const request = require('request')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare()
    .then(() => {
        const server = express()

        //parse application
        server.use(bodyParser.urlencoded({ extended: false }))

        //parse application/json
        server.use(bodyParser.json())

        server.post('/', (req, res) => {
            addEmailToMailChimp(req.body.email, (error, response, body) => {
                // This is the callback function which is passed to `addEmailToMailChimp`
                console.log(response);
                try {
                    var respObj = {}; //Initial response object
                    if (response.statusCode === 200) {
                      respObj = { success: `Subscribed using ${req.body.email}!`, message: response.body };
                    } else {
                      respObj = { error: `Error trying to subscribe ${req.body.email}. Please try again.`, message: response.body };
                    }
                    res.send(respObj);
                  } catch (err) {
                    var respErrorObj = { error: 'There was an error with your request', message: err.message };
                    res.send(respErrorObj);
                  }
            });
        })

        server.get('*', (req, res) => {
            return handle(req, res)
        })

        server.listen(3000, (err) => {
            if (err) throw err
            console.log('> Ready on http://localhost:3000')
        })
    })
    .catch((ex) => {
        console.error(ex.stack)
        process.exit(1)
    })
Brawner answered 30/9, 2018 at 15:32 Comment(2)
check this github.com/roryrjb/iplocationWhorl
This is a good article to get you started: medium.com/@adeyinkaadegbenro/…Calcaneus
T
0

Use Next.js Internationalized Routing feature.

For Sub-path Routing

// next.config.js
module.exports = {
  i18n: {
    locales: ['en-US', 'fr', 'nl-NL'],
    defaultLocale: 'en-US',
  },
}

For Domain Routing:

// next.config.js
module.exports = {
  i18n: {
    locales: ['en-US', 'fr', 'nl-NL', 'nl-BE'],
    defaultLocale: 'en-US',

    domains: [
      {
        // Note: subdomains must be included in the domain value to be matched
        // e.g. www.example.com should be used if that is the expected hostname
        domain: 'example.com',
        defaultLocale: 'en-US',
      },
      {
        domain: 'example.fr',
        defaultLocale: 'fr',
      },
      {
        domain: 'example.nl',
        defaultLocale: 'nl-NL',
        // specify other locales that should be redirected
        // to this domain
        locales: ['nl-BE'],
      },
    ],
  },
}

Follow https://nextjs.org/docs/advanced-features/i18n-routing for more details.

Tagmeme answered 27/3, 2022 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.