
How to make multilingual node.js app?!
Hello!
You are looking how toeasy makemultilingual website with seo urls? You are in right place! I will tell you about"smws" module.
So we are going to test this module, full test code can be foundon Github.
Checknpmjs.com for documentation.
First off all, setup app.js."smws" module work with Express, body-parser, cookie-parser and view engine (tested with Eta and ejs).
Module install:
npm i express body-parser cookie-parser eta smws
app.js example:
constexpress=require('express'),cookieParser=require('cookie-parser'),bodyParser=require("body-parser"),smws=require("smws"),app=express();app.set('view engine','Eta');app.use(bodyParser.urlencoded({extended:true}));app.use(express.static(__dirname+'/public'));app.use(cookieParser());app.listen(process.env.PORT||3000,()=>{console.log('Server running on port 3000');});
Next step, we need to configure"smws":
smws.config({languages:['en','ru'],defaultLang:'en'});
So, I will use two languages and default is 'en'. I don't need to add other options in config because they can stay with default values.
Language switcher
To change language from front-end setup view engine templates. We need form to send chosen language to the server:
<!-- action will be "/<%= smws.lang %>/language" if you use ejs --><!-- I'm using eta view engine --><formaction="/<%= it.smws.lang %>/language"method="post"><buttonclass="en-button"type="submit"name="lang"value="en">EN</button><buttonclass="ru-button"type="submit"name="lang"value="ru">RU</button></form>
To receive this language changes, add to yourapp.js:
app.post('/:lang/language',(req,res)=>{smws.switcher(req,res);});
smws.switcher(req,res);
controles language changes.
GET requests with smws
Next we add our paths toapp.js:
app.get('/',function(req,res){smws.run(req,res,{page:'index'});});app.get('/:lang',function(req,res){smws.run(req,res,{page:'index'});});app.get(smws.split('/:lang/:category'),function(req,res){smws.run(req,res,{page:'category',useParams:['lang','category']});});
smws.split('path')
use for your paths where you need to translate and controlmore than only language parameter.smws.run(req,req,{options});
use to response your template and control parameters.page:
option is mandatory.
I'm using two paths for homepage to cover main url likedomain.com
and with language param likedomain.com/en
.
When userfirst time visit your main page, it will be rendered in default language which chosen insmws.config({...
.
If someone visit your websiteusing someurl. For example click somewheredomain.com/ru/kategoriya
, page will be rendered inru
language, so chosen from params.
Language files
In the end we need language files. By default"smws" use.json files from"languages" folder in root directory. But you can change it in:
smws.config({...langDir:'your/dir/name'...});
Language file names must be same with your website languages. In this tutorialen.json andru.json.
Example:
//en.json{ "smws": { "category": "category", "lang": "en" }, "hello": "This is main page in 'EN'"}
//ru.json{ "smws": { "category": "kategoriya", "lang": "ru" }, "hello": "Главная страница на 'RU'"}
So in the end we get:
- website with changeable languages,
- can set SEO friendly urls,
- only one
app.get(...
for all languages, - no page duplicates like
domain.com/ru/kategoriya
anddomain.com/ru/category
. Last one will send 404 status because param:category
does not match:lang
param "ru".
Thank you for attention! Hope this will be helpful for someone!
Top comments(4)

- Email
- LocationUzbekistan
- EducationMBA in Finance
- WorkEpam Systems
- Joined
What about database? Do I have to store date in multilanguages in db? For example, products table both in Russian and English, but in different tables? And does the package change api call to the specific table according to the chosen language?

So this package are used with view engines and get content for each language from separate language files. Check GitHub code example (link on top of the post).
This should not change any third party API calls. So if you use data base with date, picture, this should be called for each language from one table.
For further actions, you may consider blocking this person and/orreporting abuse