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: pelzflorian (Florian Pelz)
Subject: Re: Web site i18n with Haunt
Date: Sun, 11 Feb 2018 14:52:30 +0100
User-agent: NeoMutt/20171208

On Sat, Feb 10, 2018 at 12:14:26AM +0100, Ricardo Wurmus wrote:
> 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.  […]

I agree.

> The translatable text could be written as two or more strings (it
> doesn’t matter that they end up in the same paragraph).  […]

Yes, you are right, the context will still be clear for translators
because the consecutive line numbers are next to each other in the PO
file and they do not appear at two unrelated places in the code.

> 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---
>

So I took your approach and ran with it:

https://pelzflorian.de/git/pelzfloriande-website/commit/?id=77e1c30532f0bf834ddc6709c7883c17bb46740f

I did not use SXPath.  This one still looks ugly:

(div (@ (id "powered-by"))
     ,@(__ "Powered by <link \
url=\"https://www.gnu.org/software/guile/\";>GNU Guile</link> and \
<link url=\"https://haunt.dthompson.us/\";>Haunt</link>."
           `(link
             . ,(lambda (tag attr text)
                  (a-href
                   (cadadr attr)
                   text)))))

I suppose I should have used SXPath for it to get the URL out of the
(@ (url "…")) attribute?

Thank you all so much for your code and your help.

Regards,
Florian

Attachment: signature.asc
Description: PGP signature


reply via email to

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