You may already know how to properly internationalize a client side application, like described in this React based tutorial, this Angular based tutorial or this Vue based tutorial.
In this blog post we will shed light on Deno.
Why do I need to handle i18n in Deno?
Think of all user faced content not directly rendered in your browser...
- For example you're building a command line interface (CLI)?
- You're using server side rendering (SSR)?
- Or you're sending some emails?
- etc.
Let's check that out...
We will show some examples that uses i18next as i18n framework. If you're curious to know why we suggest i18next, have a look at this page.
Command line interface (CLI)
Let's start with something simple: a verry small CLI app.
We are defining a sayhi
command with optional language and name parameters that should respond with a salutation in the appropriate language.
1 | import { parse } from "https://deno.land/std/flags/mod.ts"; |
Ok, now let's create a new i18n.ts
file and setup i18next accordingly:
1 | import i18next from "https://deno.land/x/i18next/index.js"; |
And also our translation resources:
1 | // locales/en/translations.json |
Now we can use the i18n.ts
export like that:
1 | import { parse } from "https://deno.land/std/flags/mod.ts"; |
Ok, what's the result?
1 | ## if we execute the cli command without any parameters... |
Easy, isn't it?
You can also i.e. use the i18next-fs-backend to dynamically load your translations, for example like this:
1 | import i18next from "https://deno.land/x/i18next/index.js"; |
🧑💻 A code example can be found here.
A possible next step...
A possible next step could be to professionalize the translation management. This means the translations would be "managed" (add new languages, new translations etc...) in a translation management system (TMS), like locize and synchronized with your code. To see how this could look like, check out Step 1 in this tutorial.
Server Side Rendering (SSR)
For this example we will use the http framework abc (created by 木杉, but any other framework will also work.
This time we will use a different i18next module, i18next-http-middleware. It can be used for all Deno web frameworks, like abc or ServestJS, but also for Node.js web frameworks, like express or Fastify.
As already said, here we will use abc.
Let's again start with the i18n.js
file:
1 | import i18next from 'https://deno.land/x/i18next/index.js' |
And our translation resources...
1 | // locales/en/translations.json |
A simple ejs template:
1 | <html> |
Our "main" file index.js
:
1 | // deno run --allow-net --allow-read index.js |
Now start the app and check what language you're seeing...
If you check the console output you'll also see something like this:
1 | node app.js |
Yes, if you like, you can also internationalize your log statements 😁
🧑💻 A code example can be found here.
A possible next step...
Do you wish to manage your translations in a translation management system (TMS), like locize?
Just use this cli to synchronize the translations with your code. To see how this could look like check out Step 1 in this tutorial.
Alternatively, use i18next-locize-backend instead of the i18next-fs-backend. If you're running your code in a serverless environment, make sure you read this advice first!
There's also an i18next crash course video.
🎉🥳 Conclusion 🎊🎁
As you see i18n is also important for Deno.
I hope you’ve learned a few new things about Deno server side internationalization and modern localization workflows.
So if you want to take your i18n topic to the next level, it's worth to try i18next and also locize.
👍