mashpie / i18n-node

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.
MIT License
3.09k stars 421 forks source link

i18n creating local files on url access #477

Closed Macha-orange closed 3 years ago

Macha-orange commented 3 years ago

Hello,

I'm using i18n for localisation on my nodejs website. My language parameter is in url so if you access :

mydomain.com/en/

You will get language from

./locales/en.json

My problem is there is some bot that try this :

mydomain.com/htdocs/
mydomain.com/mysql/
mydomain.com/php_myadmin/
mydomain.com/wordpress/
etc.

So my server is restarting and create a .locales/htdocs.json file that is a copy from my main file EN.JSON.

How to stop this behavior?


const i18n = require('i18n')
app.use(i18n.init)

i18n.configure({
    locales: ['en', 'it'],
    directory: ('./locales')
})
app.all('/', (req, res) => {
    res.redirect(`/${i18n.getLocale(req)}`)
  })

   // set language based on prefix path
  app.param('lang', (req, res, next, lang) => {
    i18n.setLocale(req, lang)
    next()
  })

  //publish current language for html attribute

  app.use((req, res, next) => {
    res.locals.lang = i18n.getLocale(req)
    next()
  })

//Render a page
app.get('/:lang/', (req, response) => {
    lang = req.params.lang;
    app.locals.lang = req.params.lang;
    response.render('pages/home')
})
mashpie commented 3 years ago

Please add

updateFiles: false to configure in Production

Macha-orange commented 3 years ago

Thank you !