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.
const name = parsedArgs.n || parsedArgs.name; const language = parsedArgs.l || parsedArgs.language;
const t = i18n(language); if (name) { console.log(t("salutationWithName", { name })); } else { console.log(t("salutation")); }
Ok, what's the result?
1 2 3 4 5 6 7 8 9 10 11
# if we execute the cli command without any parameters... deno run --allow-read mod.ts sayhi # result: Hello World!
# if we execute the cli command with a language parameter... deno run --allow-read mod.ts sayhi --language de # result: Hallo Welt!
# if we execute the cli command with a language parameter and a name parameter... deno run --allow-read mod.ts sayhi --language de --name John # result: Hallo John!
Easy, isn't it?
You can also i.e. use the i18next-fs-backend to dynamically load your translations, for example like this:
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.