Implement gettext function on front-end (browsers) by Go and GopherJS,
which compiles Golang program to JavaScript. The gettext function translates
a text string into the user's native language. Before you start, you need to
prepare PO files (see [4], [5], [6]) and convert the PO files to JSON
format (see [7]). The translations in JSON format will be used by our
gettext function.
<!doctype html><html><head><title>Golang gettext on front-end (browser)</title></head><body><buttontype="button"value="vi_VN">Vietnamese</button><buttontype="button"value="zh_TW">Traditional Chinese</button><buttontype="button"value="en">English</button><divdata-default-string="Home">Home</div><divdata-default-string="Canon">Canon</div><divdata-default-string="About">About</div><divdata-default-string="Setting">Setting</div><divdata-default-string="Translation">Translation</div><scriptsrc="demo.js"></script></body></html>
In our HTML file, we wrap a text string to be translated in div element, and
set the data-default-string attribute of div element to the text string. For
example, Home is a text string to be translated, it will be wrapped as:
<divdata-default-string="Home">Home</div>
You can also wrap a text string in span element or other elements instead of
div.
We put translations converted from PO files in the source code. The translations
are used by gettext function to translate a text string.
When a user clicks the language button, we will select the div elements with
data-default-string attribute. The string in the attribute will be translated
by gettext function and replace the original string in the div.