GNU gettext tools are great for creating i18n (web) applications. In this post,
assume that PO and MO files are ready and we will use the PO and MO files to
let Go applications speak local languages. If you do not know what PO or MO
files are, or you do not know how to create them by gettext tools, please read
my previous post  first. If you want to know how to render HTML by
html/template with the translated texts in PO and MO files in your Go web
application, please refer to .
I will show how to use MO and PO files by example. In the example, we support
two locale, zh_TW (Traditional Chinese) and vi_VN (Vietnamese). The zh_TW
PO file are located at locale/zh_TW/LC_MESSAGES/messages.po and vi_VN PO
file are located at locale/vi_VN/LC_MESSAGES/messages.po.
zh_TW PO file locale/zh_TW/LC_MESSAGES/messages.po:
# Chinese translations for PACKAGE package.# Copyright (C) 2013 THE PACKAGE'S COPYRIGHT HOLDER# This file is distributed under the same license as the PACKAGE package.# Automatically generated, 2013.#msgid""msgstr"""Project-Id-Version: PACKAGE VERSION\n""Report-Msgid-Bugs-To: \n""POT-Creation-Date: 2013-06-04 10:20+0800\n""PO-Revision-Date: 2013-03-10 05:19+0800\n""Last-Translator: Automatically generated\n""Language-Team: none\n""Language: zh_TW\n""MIME-Version: 1.0\n""Content-Type: text/plain; charset=UTF-8\n""Content-Transfer-Encoding: 8bit\n"msgid"Home"msgstr"首頁"msgid"Canon"msgstr"經典"msgid"About"msgstr"關於"msgid"Setting"msgstr"設定"msgid"Translation"msgstr"翻譯"
vi_VN PO file locale/vi_VN/LC_MESSAGES/messages.po:
In line 17 of above code, we use gettext.PGettext("", input) instead of
gettext.Gettext(input) because no msgctxt in our PO file (only msgid
and msgstr), so specify context as "" in PGettext. If you use
Gettext in this case, original string will be returned. For furthur
detail, see issue #1 of gettext-go on GitHub.
=== RUN TestAll
--- PASS: TestAll (0.00s)
gettext_test.go:17: Trang chính
gettext_test.go:18: Kinh điển
gettext_test.go:19: Giới thiệu
gettext_test.go:20: Thiết lập