guix-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Web site i18n with Haunt


From: Ricardo Wurmus
Subject: Re: Web site i18n with Haunt
Date: Sat, 10 Feb 2018 00:14:26 +0100
User-agent: mu4e 0.9.18; emacs 25.3.1

pelzflorian (Florian Pelz) <address@hidden> writes:

> How for example would this excerpt from my website better be rendered
> as XML or is this or another non-XML approach better?

I’d much prefer XML here.  One reason is that I find it very hard to
understand the syntax and how it is processed in your example.  Even
after looking at the example for some time, it’s still difficult to
understand.  You have non-translatable tags that end on an underscore,
but the number of pipe characters is not clear to me.  How is the
sentence with “register_” parsed?  Which parts are bound to
“before-link”, “link-text”, and “after-link”?

> (p ,@(__ "Thank you for your interest in my workshop \
> “GUI Programming with GTK+”. ||register_|To register please go |here|. ||For \
> more information see ||link_|here||."
>          `(("register_" .
>             ,(lambda (before-link link-text after-link)
>                (if enable-registration
>                    `(span
>                      ,before-link
>                      ,(a-href
>                        "/gui-prog-anmelden/"
>                        link-text)
>                      ,after-link)
>                    "")))
>            ("link_" .
>             ,(lambda (text)
>                (a-href
>                 (poster-url-for-lingua current-lingua)
>                 text))))))

The translatable text could be written as two or more strings (it
doesn’t matter that they end up in the same paragraph).  The first one
is easy:

  "Thank you for your interest in my workshop “GUI Programming with GTK+”."

The second one might be:

  "To register please go <register-link>here</register-link>."

The third:

  "For more information see <link>here</link>."

(Whatever tag name is used is arbitrary and only has to be unique within
the context of a single translatable string.)  Or they could all be one
big XML snippet.

With sxpath we can easily pick tagged substrings by specifying their
path.  Or we could fold over the parse tree and transform it by applying
an arbitrary transformation.

Here’s a trivial example of such a transform:

--8<---------------cut here---------------start------------->8---
(use-modules (sxml simple) (sxml transform))
;; This is the translated string, wrapped in some tag to make it a valid
;; XML fragment.
(define tr "<translation>Click <link>here</link></translation>")

;; Convert to SXML, then transform it.
(pre-post-order (xml->sxml tr)
 ;; When we find the tag “link” wrap the contents in a URL anchor.
 `((link . ,(lambda (tag . kids)
              `(a (@ (href "http://gnu.org";)) ,kids)))
   ;; Just wrap the contents in a tag for everything else
   (*default*  . ,(lambda (tag . kids) `(,tag ,@kids)))
   ;; Unwrap all text
   (*text*     . ,(lambda (_ txt) txt))))

=> (*TOP* (translation "Click " (a (@ (href "http://gnu.org";)) "here")))
--8<---------------cut here---------------end--------------->8---

Using “pre-post-order” directly is verbose as you need to specify the
*default* and *text* handlers, but this can easily be hidden by a
friendlier procedure that would present a user interface that wouldn’t
look too different from what your macro presents to users.

--8<---------------cut here---------------start------------->8---
(define (foo str . handlers)
 (pre-post-order (xml->sxml (string-append "<translation>" str 
"</translation>"))
  `(,@handlers
    (*default*  . ,(lambda (tag . kids) `(,tag ,@kids)))
    (*text*     . ,(lambda (_ txt) txt)))))

(foo "Click <link>here</link>"
 `(link . ,(lambda (tag . contents)
           `(a (@ (href "/help")) ,@contents))))
--8<---------------cut here---------------end--------------->8---

--
Ricardo

GPG: BCA6 89B6 3655 3801 C3C6  2150 197A 5888 235F ACAC
https://elephly.net





reply via email to

[Prev in Thread] Current Thread [Next in Thread]