translate

2018-02-28

Abstraction layer for translations

Upstream URL

gitlab.common-lisp.net/dkochmanski/translate

Author

Daniel 'jackdaniel' Kochmański <daniel@turtleware.eu>

License

LLGPLv2
README

1Introduction

TRANSLATE is a thin abstraction layer over the ordinary strings which allows the creation of a seamless translation for your project. The code is written in a plain Common Lisp without any dependencies. A system definition is provided in ASD format, although ASDF isn't required to run this software.

This library is licensed under LLGPLv2, which means that it may be used in any project for any purpose although any significant modifications to the library should be published. It doesn't impose it's license on software depending on it.

To illustrate library usage some imaginary functions are used: MAKE-BUTTON and MAKE-LABEL. The following sample code convention is used:

  • lines preceded with ==> indicate reader input
  • all other lines represent printer output

2Description

2.1Basic usage

The library pollutes *READTABLE* with dispatch macro character #t, where "t" is abbreviation of "translate". Instead of writing ordinary constant strings in his/her application, the user instead precedes them with #t:

     ==> #t"dum dum dum, piję rum"
     "la la la, I drink in the spa"

If TRANSLATE:*LANGUAGE* is bound to NIL and TRANSLATE:*RESOLUTION-TIME* to :LOAD-TIME, then #t"hello" will resolve to the string "hello" and the translation will have no effect. It is convenient, because programmer can put #t"strings" for the future translation with no functional consequences at run-time while keeping application innards visually separated from the messages meant to be presented to user.

     ==> (setf translate:*language* nil)
     NIL

     ==> (make-button :label #t"hello")
     #<button "hello">

Binding TRANSLATE:*LANGUAGE* to a non-NIL value enables string translation. If no translation exists it will be temporarily translated to the same value enclosed in a curly brackets:

     ==> (translate:define-language :pl)
     (#<hash-table 00000000054323c0> NIL #<compiled-function 0000000005fe08c0>)

     ==> (setf translate:*language* :pl)
     :PL

     ==> (make-label :text #t"Greetings, programs")
     ;;; Warning: phrase "Greetings, programs" isn't defined for language :PL.
     #<label "{Greetings, programs}">

     ==> (make-label :text #t"Leave the grid")
     ;;; Warning: phrase "Leave the grid" isn't defined for language :PL.
     #<label "{Leave the grid}">

Using a string that has not been translated will cause a warning at resolution time and string will be added to the special list of phrases not yet translated (which may be evaluated for future processing, like adding the missing translations). If a language put in the TRANSLATE:*LANGUAGE* isn't defined yet, CERROR will be signaled with a restart allowing language creation.

To distinguish dictionaries, the predicate EQL is used and phrases are distinguished using the predicate EQUAL, so the most convenient is using symbols as the language designators. It also implies, that phrases meant for translation are case sensitive.

2.2Adding translation

To add translations, two interfaces are provided. The ADD-SINGLE-TRANSLATION function takes three arguments, where the first is the language for which we provide translation, the second is a translated phrase and the third is an actual translation. Phrase type must be a string, but the translation might be any kind of object (although it is advised to use strings, the user is free to shoot himself in the foot by abusing the translation mechanism).

     ==> (translate:add-single-translation 'pl "hello" "Witaj!")
     ;;; Warning: Implicitly creating language PL.
     [PL] hello -> Witaj!
     "Witaj!"

     ==> (translate:add-single-translation 'en "hello" "Welcome!")
     ;;; Warning: Implicitly creating language EN.
     [EN] hello -> Welcome!
     "Welcome!"

     ==> (translate:add-single-translation
          'pl "bang"
          (let ((i 1))
            (lambda ()
              (format nil "bah ~A" (incf i)))))
     bang -> #<bytecompiled-closure #<bytecompiled-function 00000000067d9f50>> (PL)
     #<bytecompiled-closure #<bytecompiled-function 00000000067d9f50>>

ADD-TRANSLATIONS is a simple wrapper around ADD-SINGLE-TRANSLATION that allows the translation of multiple phrases in one form. The first argument is once again the translation language, while all further arguments are alternately phrases and translations.

     ==> (translate:add-translations 'pl
            "header-about"   "O firmie"
            "header-offer"   "Oferta"
            "header-blog"    "Blog"
            "header-pricing" "Cennik"
            "header-contact" "Kontakt")
     [PL] header-about -> O firmie
     [PL] header-offer -> Oferta
     [PL] header-blog -> Blog
     [PL] header-pricing -> Cennik
     [PL] header-contact -> Kontakt
     T

     ==> (translate:add-translations 'en
            "header-about"   "About"
            "header-offer"   "Offer"
            "header-blog"    "Blog"
            "header-pricing" "Prices"
            "header-contact" "Contact")
     [EN] header-about -> About
     [EN] header-offer -> Offer
     [EN] header-blog -> Blog
     [EN] header-pricing -> Prices
     [EN] header-contact -> Contact
     T

Generally it is advised to use symbolic and meaningful names for phrases to be translated, not the final phrases written in English. Providing "translation-tags" of concise form is easier to comprehend for people who will translate the application.

2.3Interactive fixing of the missing phrases

Loading the code is enough to catch all not yet translated phrases for the active language (bound to TRANSLATE:*LANGUAGE*) if resolution is performed at load time. Otherwise, an untranslated phrase is saved after it's first evaluation. To list saved phrases without translations, the function MISSING-TRANSLATIONS is available. It returns a list of the form {((LANG (PHRASES*))*)}.

     ==> (translate:missing-translations)
     ((PL ("phrase-1" "phrase-2" "phrase-3"))
      (BG ("phrase-1" "phrase-3")))

Such output means, that language PL doesn't have translations for "phrase-1", "phrase-2" and "phrase-3", while BG doesn't have translations for "phrase-1" and "phrase-3". Languages which have all translations are filtered and they don't appear in the result.

2.4Different times of the resolution

The library may work in two different modes which dictate the time when the actual translation is performed. Strings may be translated at load-time, or at run-time.

The first approach is faster, because it doesn't require any processing at run-time, while the second is much more flexible allowing the change of dictionaries and translations when the program is running or depending on lexically scoped value of the parameter TRANSLATE:*LANGUAGE*.

It is important to remember that, when translations are done at run-time, strings preceded by #t are transformed to the function calls and they may work not as expected in the context where enclosing macro prevents their evaluation.

     ==> (setf translate:*resolution-time* :run-time)
     :RUN-TIME

     ==> (setf translate:*language* :en)
     :EN

     ==> (translate:add-single-translation :en "hello" "Hello")
     [EN] hello -> Hello

     ==> (translate:add-single-translation :pl "hello" "Cześć")
     [PL] hello -> Cześć

     ==> (let ((translate:*language* :en))
           #t"hello")
     "Hello"

     ==> (let ((translate:*language* :pl))
           #t"hello")
     "Cześć"

     ==> (quote #t"hello")
     (TRANSLATE:TRANSLATE "hello")

When translation is performed at load-time, the translation has to be present before the actual phrase is used (e.g. in a lambda expression), because phrases are resolved to their translations immediately. That also means that changing TRANSLATE:*LANGUAGE* in the future won't affect translations resolved earlier.

     ==> (setf translate:*resolution-time* :load-time)
     :LOAD-TIME

     ==> (setf translate:*language* :en)
     :EN

     ==> (defparameter *my-function-1*
           (lambda () #t"hello"))
     ;;; Warning: phrase "hello" isn't defined for language EN.
     *MY-FUNCTION-1*

     ==> (translate:add-single-translation :en "hello" "Hello")
     hello -> Hello (EN)

     ==> (translate:add-single-translation :pl "hello" "Cześć")
     hello -> Cześć (PL)

     ==> (let ((translate:*language* :en))
           #t"hello")
     "Hello"

     ==> (let ((translate:*language* :pl))              ; lexical scope is ignored
           #t"hello")
     "Hello"

     ==> (defparameter *my-function-2*
           (lambda () #t"hello"))
     *MY-FUNCTION-2*

     ==> (funcall *my-function-1*) ; phrase wasn't translated when function was created
     "{hello}"

     ==> (funcall *my-function-2*)
     "Hello"

     ==> (quote #t"hello")
     "Hello"

Translation at run-time is better when the programmer wants to add translations ad-hoc or wants to switch languages when the application is running. Load-time translation is more suitable for static translations for deployed applications or where macros prevent necessary evaluation of the expressions. Also when the programmer wants to add translations in future (if language is bound to nil and resolution is performed at load-time the expression #t"hello world" means the same as the "hello world").

3Reference

3.1Parameters

3.1.1*LANGUAGE*

      This variable holds the current language designator (the predicate
      used for comparison is EQL). If bound to NIL, translation works the
      same way as the IDENTITY function.

3.1.2*RESOLUTION-TIME*

      Applicable values are :LOAD-TIME and :RUN-TIME (the latter is the
      default). The variable controls time of actual resolution.

      If it's the :LOAD-TIME, then resolution is performed when the reader
      encounters the #t dispatch macro character, while setting the variable
      to :RUN-TIME translates #t"string" to the form (TRANSLATE "string")
      and resolution takes place at the time of the form evaluation.

3.2Functions

3.2.1DEFINE-LANGUAGE

      DEFINE-LANGUAGE - external symbol in TRANSLATE package
      -----------------------------------------------------------------------------
      DEFINE-LANGUAGE (NAME &REST TRANSLATIONS)                          [Function]
      Define language NAME with provided TRANSLATIONS

      If LANGUAGE exists, a continuable error is signalled, which allows either
      dropping the operation or superseding the language which is already defined.
      TRANSLATIONS are alternating phrases and their corresponding objects.
      -----------------------------------------------------------------------------

3.2.2ADD-SINGLE-TRANSLATION

      ADD-SINGLE-TRANSLATION - external symbol in TRANSLATE package
      -----------------------------------------------------------------------------
      ADD-SINGLE-TRANSLATION (LANGUAGE PHRASE TRANSLATION)               [Function]
      Add TRANSLATION of PHRASE for given LANGUAGE

      If LANGUAGE doesn't exist, it is implicitly created and a warning is
      emmited.
      -----------------------------------------------------------------------------

3.2.3ADD-TRANSLATIONS

      ADD-TRANSLATIONS - external symbol in TRANSLATE package
      -----------------------------------------------------------------------------
      ADD-TRANSLATIONS (LANGUAGE &REST TRANSLATIONS)                     [Function]
      Add any number of TRANSLATIONS for the given LANGUAGE
      -----------------------------------------------------------------------------

3.2.4TRANSLATE

      TRANSLATE - external symbol in TRANSLATE package
      -----------------------------------------------------------------------------
      TRANSLATE (PHRASE &OPTIONAL (LANGUAGE *LANGUAGE*))                 [Function]
      Find the translation of PHRASE in the store associated with LANGUAGE

      If LANGUAGE is NIL, then this is the same as the IDENTITY function. If
      the provided LANGUAGE isn't defined, the store is explicitly
      created. If no PHRASE is defined for a given language, it is stored
      for later translation and replaced by PHRASE surrunded by curly
      brackets.
      -----------------------------------------------------------------------------

3.2.5MISSING-TRANSLATIONS

      MISSING-TRANSLATIONS - external symbol in TRANSLATE package
      -----------------------------------------------------------------------------
      MISSING-TRANSLATIONS                                               [Function]
      Creates a list of phrases which aren't translated for the defined
      languages. Returns a list of form: ({(LANG ({PHRASE}*))}*)
      -----------------------------------------------------------------------------

Dependencies (1)

  • fiveam

Dependents (1)

  • GitHub
  • Quicklisp