;;; i18n-message.el --- internationalization of messages -*- lexical-binding: t; -*- ;; Copyright (C) 2019 Free Software Foundation, Inc. ;; Author: Juri Linkov ;; 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 . ;;; 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