~ruther/guix-local

guix-local/build-aux/keep-only-translated.scm -rw-r--r-- 2.7 KiB
56daff7e — Rutherther news: Add information about %desktop-services changes. a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2025 Florian Pelz <pelzflorian@pelzflorian.de>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

;; Minify Gettext PO files when synced from
;; <https://codeberg.org/guix/translations/>,
;; keeping only actually translated messages.

;; Note: This does not work for PO files of the guix domain, which needed
;; support for plural forms in (@ (guix build po) read-po-file).  The guix
;; domain's files are comparatively small and the read-po-file API would
;; have to be expanded to use records or such; it is not worth it.

(use-modules (guix build po)
             (ice-9 match)
             (ice-9 textual-ports))

(define (escape str)
  "Escape msgid or msgstr.  Replace by C-style escape sequences."
  (let* ((in (open-input-string str))
         (text (get-string-all in))
         (escaped-text-as-list
          (string-fold-right
           (lambda (char result)
             (cons (case char
                     ((#\") "\\\"")
                     ((#\\) "\\\\")
                     ((#\linefeed) "\\n")
                     ((#\return) "\\r")
                     ((#\tab) "\\t")
                     (else (string char)))
                   result))
           '()
           text))
         (escaped-text (apply string-append escaped-text-as-list)))
    (display escaped-text)))

(match (command-line)
  ((program pofile)
   (let ((input (open-input-file pofile)))
     ;; Just copy until an empty line.
     (letrec ((copy
               (lambda ()
                 (let ((next-line (get-line input)))
                   (display next-line)
                   (newline)
                   (when (> (string-length next-line) 0)
                     (copy))))))
       (copy))
     ;; Then print only translated messages.
     (for-each
      (lambda (msg)
        (match msg
          ((msgid . msgstr)
           (display "msgid \"")
           (escape msgid)
           (display "\"")
           (newline)
           (display "msgstr \"")
           (escape msgstr)
           (display "\"")
           (newline)
           (newline))))
      (read-po-file input)))))