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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
| | ;;; i18n-message.el --- internationalization of messages -*- lexical-binding: t; -*-
;; Copyright (C) 2019 Free Software Foundation, Inc.
;; Author: Juri Linkov <juri@linkov.net>
;; Maintainer: emacs-devel@gnu.org
;; Keywords: i18n, multilingual
;; This file is part of GNU Emacs.
;; GNU Emacs 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 Emacs 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 Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(defcustom i18n-fallbacks
'(("en" "English"))
"An alist mapping the current language to possible fallbacks.
Each element should look like (\"LANG\" . FALLBACK-LIST), where
FALLBACK-LIST is a list of languages to try to find a translation."
:type '(alist :key-type (string :tag "Current language")
:value-type (repeat :tag "A list of fallbacks" string))
:group 'i18n
:version "27.1")
(defvar i18n-dictionaries (make-hash-table :test 'equal))
(defun i18n-add-dictionary (lang)
(unless (gethash lang i18n-dictionaries)
(puthash lang (make-hash-table :test 'equal) i18n-dictionaries)))
;;;###autoload
(defun i18n-add-translation (lang from to)
(let ((dict (gethash lang i18n-dictionaries)))
(unless dict
(setq dict (i18n-add-dictionary lang)))
(puthash from to dict)))
;;;###autoload
(defun i18n-add-translations (lang translations)
(dolist (translation translations)
(i18n-add-translation lang (nth 0 translation) (nth 1 translation))))
(defun i18n-get-plural (lang n)
;; Source: (info "(gettext) Plural forms")
(pcase lang
((or "Japanese" "Vietnamese" "Korean" "Thai")
0)
((or "English" "German" "Dutch" "Swedish" "Danish" "Norwegian"
"Faroese" "Spanish" "Portuguese" "Italian" "Bulgarian" "Greek"
"Finnish" "Estonian" "Hebrew" "Bahasa Indonesian" "Esperanto"
"Hungarian" "Turkish")
(if (/= n 1) 1 0))
((or "Brazilian Portuguese" "French")
(if (> n 1) 1 0))
((or "Latvian")
(if (and (= (% n 10) 1) (/= (% n 100) 11)) 0 (if (/= n 0) 1 2)))
((or "Gaeilge" "Irish")
(if (= n 1) 0 (if (= n 2) 1 2)))
((or "Romanian")
(if (= n 1) 0 (if (or (= n 0) (and (> (% n 100) 0) (< (% n 100) 20))) 1 2)))
((or "Lithuanian")
(if (and (= (% n 10) 1) (/= (% n 100) 11)) 0
(if (and (>= (% n 10) 2) (or (< (% n 100) 10) (>= (% n 100) 20))) 1 2)))
((or "Russian" "Ukrainian" "Belarusian" "Serbian" "Croatian")
(if (and (= (% n 10) 1) (/= (% n 100) 11)) 0
(if (and (>= (% n 10) 2) (<= (% n 10) 4) (or (< (% n 100) 10) (>= (% n 100) 20))) 1 2)))
((or "Czech" "Slovak")
(if (= n 1) 0 (if (and (>= n 2) (<= n 4)) 1 2)))
((or "Polish")
(if (= n 1) 0
(if (and (>= (% n 10) 2) (<= (% n 10) 4) (or (< (% n 100) 10) (>= (% n 100) 20))) 1 2)))
((or "Slovenian")
(if (= (% n 100) 1) 0 (if (= (% n 100) 2) 1 (if (or (= (% n 100) 3) (= (% n 100) 4)) 2 3))))
((or "Arabic")
(if (= n 0) 0 (if (= n 1) 1 (if (= n 2) 2 (if (and (>= (% n 100) 3) (<= (% n 100) 10)) 3
(if (>= (% n 100) 11) 4 5))))))))
(defun i18n-get-translation (format-string &rest args)
(let* ((lang current-language-environment)
(fallbacks (cdr (assoc lang i18n-fallbacks)))
dict found)
(while (and (not found) lang)
(when (setq dict (gethash lang i18n-dictionaries))
(setq found
(pcase (gethash format-string dict)
((and (pred functionp) f) (apply f format-string args))
((and (pred stringp) s) s)
((and (pred consp) l)
(let ((n (i18n-get-plural lang (car args))))
(when n (nth n l)))))))
(unless found
(setq lang (pop fallbacks))))
(or found format-string)))
(defun i18n-message-translate (&rest args)
(apply 'i18n-get-translation args))
(defvar message-translate-function)
(setq message-translate-function 'i18n-message-translate)
(provide 'i18n-message)
;;; i18n-message.el ends here
|