DEV Community

Daniel Newell for Localazy

Posted on • Edited on

Localise your NodeJS app easily with i18n

So, you have a NodeJS app, but you only have access to a small subset of people: those who speak your language. Or maybe you have translations, but they are inefficient to implement and take up valuable time you can be using for fixing issues or adding features. In this article, you will learn how to implement Localazy to your NodeJS project.

Localazy is a translation management platform and it's a great option for developers because of two key features:

  • It comes with ShareTM allowing you to automatically translate your app into 80+ languages for free by sharing translations with other developers.
  • With its CLI, it can be easily integrated into any workflow. And we are using the CLI today.

By the way, this tool is free. Pretty cool, right?

Part 1 - Getting Ready

Create a Localazy account and follow prompts. Create an app, add your languages, and add translators.

Now, install.

Create a file called localazy.json and paste the following. Make sure to gather your keys!

{

  "writeKey": "your-apps-write-key", 
  "readKey": "your-apps-read-key",

  "upload": {
    "type": "json",
    "files": "locales/en.json"
  },

  "download": {
    "files": "locales/${lang}.json"
  }
Enter fullscreen mode Exit fullscreen mode

Now, we can upload translations. Create en.json and edit as needed.

{
"appName": "Your Cool App",
"error": "An error has occurred.",
"Hello": "Hello {{name}}"
}
Enter fullscreen mode Exit fullscreen mode

Now run localazy upload and you should see your translation strings on your home screen. You can use automatic or manual translations. Automatic translations may not be the most accurate, but are a start. When complete, use localazy download to gather edited files.

Part 2 - Development

Install i18n via npm. In your code add the following.

const i18n = require('i18n')
i18n.configure({
  locales: ['en', 'es', 'de'] // your languages
  directory: path.join(__dirname, '/locales'),
  register: global
})
function translate (p, l, ph) {
  return __({ phrase: p, locale: l}, ph)
}
Enter fullscreen mode Exit fullscreen mode

Now, to translate anything, use the translate() function, like so:

console.log(translate('hello', 'de', { name: 'Daniel' }))
Enter fullscreen mode Exit fullscreen mode

Implement this to fit your code, and you are complete!

If you run into issues, please leave a message and I will fix it as soon as possible.

Edited on 15 September: Basic style changes and fixed an oversight

Top comments (1)

Collapse
 
anandpatel504 profile image
Anand

Hey I'm trying to convert the value of key in other lang, and after converting the successful my value I want to store in a JSON file, could you please help me how can I do it with i18n.

I had tried to convert like you but not able to convert my value.

Thank you :)