i18n.scm
1 | ;;; Nani Project website |
2 | ;;; Copyright © 2019, 2021 Julien Lepiller <julien@lepiller.eu> |
3 | ;;; |
4 | ;;; This file is part of the Nani Project website. |
5 | ;;; |
6 | ;;; The Nani Project website is free software; you can redistribute it and/or modify it |
7 | ;;; under the terms of the GNU Affero General Public License as published by |
8 | ;;; the Free Software Foundation; either version 3 of the License, or (at |
9 | ;;; your option) any later version. |
10 | ;;; |
11 | ;;; The Nani Project website is distributed in the hope that it will be useful, but |
12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | ;;; GNU Affero General Public License for more details. |
15 | ;;; |
16 | ;;; You should have received a copy of the GNU Affero General Public License |
17 | ;;; along with the Nani Project website. If not, see <http://www.gnu.org/licenses/>. |
18 | |
19 | (define-module (tools i18n) |
20 | #:use-module (sxml simple) |
21 | #:use-module (ice-9 match) |
22 | #:export (languages tr _ site-locale translate)) |
23 | |
24 | (define language-map |
25 | `(("" . "en_US.UTF-8") |
26 | ("en" . "en_US.UTF-8") |
27 | ("fr" . "fr_FR.UTF-8") |
28 | ("uk" . "uk_UA.UTF-8") |
29 | ("zh_CN" . "zh_CN.UTF-8"))) |
30 | |
31 | (define languages |
32 | (map car language-map)) |
33 | |
34 | (define (site-locale locale) |
35 | (setlocale LC_ALL |
36 | (assoc-ref language-map locale)) |
37 | (bindtextdomain "nani" (string-append (dirname (current-filename)) "/../po")) |
38 | (textdomain "nani")) |
39 | |
40 | (site-locale "fr") |
41 | |
42 | (define (__ tr . args) |
43 | (xml->sxml (apply format #f (format #f "<tr>~a</tr>" (gettext tr)) args))) |
44 | |
45 | (define (_ tr . args) |
46 | (let ((res (apply __ tr args))) |
47 | (match res |
48 | ((*TOP* (tr rest)) rest) |
49 | ((*TOP* (tr rest ...)) rest)))) |
50 | |
51 | (define (tr body) |
52 | (match body |
53 | (('_ rest) (_ rest)) |
54 | (('_ rest ...) (apply _ rest)) |
55 | ((tag ('@ opt ...) rest ...) (apply list tag (apply list '@ opt) (map tr rest))) |
56 | ((tag rest ...) (apply list tag (map tr rest))) |
57 | (foo foo))) |
58 | |
59 | (define (translate str lang) |
60 | (site-locale lang) |
61 | (tr str)) |
62 |