Previous: , Up: Localization   [Contents][Index]


2.29.2 Example Usage

Here is a simple example of how to localize your code. Let the file intl-ex.lisp contain:


(intl:textdomain "example")  

(defun foo (x y)
  "Cool function foo of x and y"
  (let ((result (bar x y)))
    ;; TRANSLATORS:  One line comment about bar.
    (format t _"bar of ~A and ~A = ~A~%" x y result)
    #| TRANSLATORS:  Multiline comment about
    how many Xs there are
    |#
    (format t (intl:ngettext "There is one X"
                             "There are many Xs"
                             x))
    result))

The call to textdomain sets the default domain for all translatable strings following the call.

Here is a sample session for creating a template file:

* (intl:install)

T
* (intl:translation-enable)

T
* (compile-file "intl-ex")

#P"/Volumes/share/cmucl/cvs/intl-ex.sse2f"
NIL
NIL
* (intl::dump-pot-files :output-directory "./")

Dumping 3 messages for domain "example"
NIL
*

When this file is compiled, all of the translatable strings are recorded. This includes the docstring for foo, the string for the first format, and the string marked by the call to intl:ngettext.

A file named “example.pot” in the directory “./” is created. The contents of this file are:

# example

# SOME DESCRIPTIVE TITLE
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION"
"Report-Msgid-Bugs-To: "
"PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>"
"Language-Team: LANGUAGE <LL@li.org>"
"MIME-Version: 1.0"
"Content-Type: text/plain; charset=UTF-8"
"Content-Transfer-Encoding: 8bit"

#.  One line comment about bar.
#: intl-ex.lisp
msgid "bar of ~A and ~A = ~A~%"
msgstr ""

#.  Multiline comment about
    how many Xs there are
#: intl-ex.lisp
msgid "Cool function foo of x and y"
msgstr ""

#: intl-ex.lisp
msgid "There is one X"
msgid_plural "There are many Xs"
msgstr[0] ""

To finish the translation, a corresponding “example.po” file needs to be created with the appropriate translations for the given strings. This file must be placed in some directory that is included in intl:*locale-directories*.

Suppose the translation is done for Korean. Then the user can set the environment variables appropriately or call (intl:setlocale "ko"). Note that the external format for the standard streams needs to be set up appropriately too. It is up to the user to set this correctly. Once this is all done, the output from the function foo will now be in Korean instead of English as in the original source file.

For further information, we refer the reader to documentation on

gettext.


Previous: Dictionary, Up: Localization   [Contents][Index]