package module
v0.0.0-...-db0e14d Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2020 License: Unlicense Imports: 4 Imported by: 0


Go Template With Gettext Support

.. image::

.. image::

.. image::

.. image::

.. image::

.. image::

Go_ `html/template`_ with gettext_ support.

Development Environment:

  - `Ubuntu 20.04`_
  - `Go 1.14.4`_


.. code-block:: bash

  go get -u


The gettext_ feature is provided by `chai2010/gettext-go`_, which is pure Go
solution for gettext_.

Follow `gettext workflow`_ to prepare PO files, we need to mark translatable
strings from input files. In our cases here, the translatable string
``Pāḷi Dictionary`` is marked as ``{{gettext "Pāḷi Dictionary"}}`` (see
`theme/template-i18n/index.html <theme/template-i18n/index.html>`_). If we
run xgettext_ directly on the Go template, we get nothing [7]_. We can use sed_
to convert the marked strings in Go template to the extractable syntax as

.. code-block:: bash

  $ sed "s/{{gettext \(".*"\)}}/{{gettext(\1)}}/g" theme/template-i18n/index.html | xgettext --no-wrap --language=c --from-code=UTF-8 --output=locale/messages.pot -

Then we follow the normal workflow again to finish the translation and get the
PO files to be used by `chai2010/gettext-go`_ package.

See `parse_test.go <parse_test.go>`_ for how to create HTML files.


Released in public domain. See UNLICENSE_.


.. [1] `Golang Template Inheritance (Python Jinja2 extends & include) <>`_

.. [2] `GitHub - chai2010/gettext-go: GNU gettext for Go <>`_
       (`godoc <>`__)

.. [3] `io writer to string golang - Google search <>`_

.. [4] | `godoc code - Google search <>`_
       | `godoctricks - GoDoc <>`_

.. [5] | render_ and grender_
       | `grender: stdlib templates with additional "extends" support : golang <>`_
       | `GitHub - dannyvankooten/grender: Go package for easily rendering JSON/XML data and HTML templates <>`_
       | `GitHub - unrolled/render: Go package for easily rendering JSON, XML, binary data, and HTML templates responses. <>`_

.. [6] | `Go template rendering or Node.js template rendering? : golang <>`_
       | `GitHub - titpetric/egon: An ERB-style templating language for Go. <>`_
       | `books/12fa-docker-golang/chapter7 at master · titpetric/books · GitHub <>`_

.. [7] `xgettext Extract Translatable Strings From Golang html/template <>`_

.. _Go:
.. _html/template:
.. _gettext:
.. _Ubuntu 20.04:
.. _Go 1.14.4:
.. _chai2010/gettext-go:
.. _gettext workflow:
.. _xgettext:
.. _sed:
.. _grender:
.. _render:



Package gtmpl adds gettext support to Go standard html/template package.



This section is empty.


This section is empty.


func ParseDirectoryTree

func ParseDirectoryTree(domain, localeDir, dir string) (t *template.Template, err error)

ParseDirectoryTree will find all files in directory tree (including sub-directories) and pass filenames to ParseFiles.

func ParseFiles

func ParseFiles(domain, localeDir string, filenames ...string) (*template.Template, error)

ParseFiles is the same as ParseFiles except gettext function is installed via Funcs method provided by Go standard html/template package. the installed gettext function will use the data given by domain and localeDir arguments to translate the strings in the template.

func SetLanguage

func SetLanguage(locale string) string

SetLanguage sets the locale to be used to render the template.

see for more details.

func Translate

func Translate(msgid string) string

Translate returns gettext.PGettext("", msgid)

see for more details.


This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL