How to i18n with specify language?

Hi, I want to pass the i18n stuff to JS, so that I don’t need to store the i18n messages in somewhere else again.

Use case:

Currently, I render the template via partial/template.

<div class="my-modal">
{{ i18n "modal_title" }}

It works fine, but the modal will be a part of the every single HTML page, so I want to use JS to render that my-modal. This approach only increases the JS file size.

// pseudo-code
import { default as params } from '@params';
const lang = document.documentElement.getAttribute('lang')
const modalTitle = params.i18n.lang.modal_title
const myModal = `<div class="my-modal">${modalTitle}</div>`

But I don’t know if it’s possible to pass the i18n in all translations.

{{ $i18n := newScratch }}
{{ range .Sites }}
  {{ $i18n.SetInMap .Lang "modal_title" (i18n "modal_title" .Lang) }}
{{ end }}

Figure it out by storing i18n translations into /data (the data can be overridden by users), and pass it to JS via the params option.

I also wrote an i18n JS module for this, hope it helps who come to here.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.