Express.js is a small but powerful Web Framework for Node.js that allows you to create a robust set of features for web and mobile applications.īefore we install Express.js though we need to add a few more abstractions to our app in order to accommodate this change. Let’s see how can we utilize what we have in an example application using Express.js. For this example, we maintained the same name for readability. Tip: You can choose to name your resolved service as you like.
LocaleService.getCurrentLocale() // 'en'Ĭonsole.log(anslate('Hello')) Ĭonsole.log(anslatePlurals('You have %s message', 3)) $ node -experimental-modules index.mjs
Test the resolution of our service by replacing the calls in the index.mjs file with the ones using the container.įile: index.mjs import container from './app/container' Ĭonst localeService = container.resolve('localeService') Modify the constructor of the LocaleService class to accept an i18nProvider object:Ģ. Let’s hook our LocaleService and our i18n config together at the constructor so every time we resolve the everything is set up for us:ġ. There are more options for lifetime management in the awilix documentation. For this example we want the LocaleService class to be a singleton object and the i18n config to be just a value because we have just configured it. If (this.getLocales().indexOf(locale) != -1) )Īs you can see we have greater flexibility on how we want our objects to be instantiated. Must be from the list of available locales. * string The list of available locale codes Add the file contents with your favorite editor: $ touch localeService.mjs Create the service folder: $ mkdir -p app/services & cd app/servicesĢ. We need to have a service that will handle any i18n calls and will work as a mediator for any underlying i18n library we may use or may not use.ġ. $ mkdir node-i18n-example & cd node-i18n-example Initiate a new Node.js appįor the purposes of this tutorial, I’ve taken a lot of inspiration from this node API boilerplate where you start with a good, yet opinionated base project for your Node.js applications.Ĭreate the initial folder structure. That’s partly good because it allows us to have total control over the design decisions that may affect our project scope. So in order to start with i18n, we have to start from scratch. The Node.js runtime library is providing only the basic low-level primitives in order to write server applications.
You can achieve the same result using babel with preset-es2015. For convenience, we are going to use the –experimental-modules flag in order to use es6 imports in code.
#I18N JSON EDITOR CODE#
This tutorial will try to fill that gap by showing ways of integrating i18n and adapting to different cultural rules and habits in your Node.js applications in a sensible manner.įor the purposes of this tutorial, I will be using the latest Node.js LTS runtime v8.94, and the code for this tutorial is hosted on GitHub.
#I18N JSON EDITOR SOFTWARE#
So, when you have the need for scalable I18n solutions that are easy to use and implement, it pays to make some sensible software architecture demissions upfront. Although there are numerous tutorials online exploring Node.js and its libraries, the topic of Node internationalization is almost left behind. The Node ecosystem is vast and it relies on community projects. In essence, it allows JavaScript to run in the backend as a server-side code. Node.js is an asynchronous event-driven JavaScript runtime designed to help build scalable network applications.