1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-01-06 11:50:51 -08:00

i18n: Add function ngettext for pluralization.

* lisp/international/mule-cmds.el (ngettext): New function.
https://lists.gnu.org/archive/html/emacs-devel/2019-03/msg00586.html

* lisp/replace.el (flush-lines, how-many, occur-1, occur-engine)
(perform-replace): Use ngettext.

* lisp/progmodes/grep.el (grep-exit-message): Use ngettext.
(grep-mode-font-lock-keywords): Match both singular and plural form
of "matches".
This commit is contained in:
Juri Linkov 2019-03-24 23:55:07 +02:00
parent 36535caf96
commit 5fdf4fc07d
4 changed files with 54 additions and 23 deletions

View file

@ -1364,6 +1364,11 @@ Now, such rx expressions generate an error.
* Lisp Changes in Emacs 27.1 * Lisp Changes in Emacs 27.1
** i18n (internationalization)
*** ngettext can be used now to return the right plural form
according to the given numeric value.
+++ +++
** inhibit-null-byte-detection is renamed to inhibit-nul-byte-detection ** inhibit-null-byte-detection is renamed to inhibit-nul-byte-detection

View file

@ -2798,6 +2798,20 @@ See also `locale-charset-language-names', `locale-language-names',
'a4))))))) 'a4)))))))
nil) nil)
;;; i18n (internationalization)
(defun ngettext (msgid msgid_plural n)
"Return the plural form of the translation of the string.
This function is similar to the `gettext' function as it finds the message
catalogs in the same way. But it takes two extra arguments. The MSGID
parameter must contain the singular form of the string to be converted.
It is also used as the key for the search in the catalog.
The MSGID_PLURAL parameter is the plural form. The parameter N is used
to determine the plural form. If no message catalog is found MSGID is
returned if N is equal to 1, otherwise MSGID_PLURAL."
(if (= n 1) msgid msgid_plural))
;;; Character property ;;; Character property
(put 'char-code-property-table 'char-table-extra-slots 5) (put 'char-code-property-table 'char-table-extra-slots 5)

View file

@ -459,7 +459,7 @@ abbreviated part can also be toggled with
;; remove match from grep-regexp-alist before fontifying ;; remove match from grep-regexp-alist before fontifying
("^Grep[/a-zA-Z]* started.*" ("^Grep[/a-zA-Z]* started.*"
(0 '(face nil compilation-message nil help-echo nil mouse-face nil) t)) (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t))
("^Grep[/a-zA-Z]* finished with \\(?:\\(\\(?:[0-9]+ \\)?matches found\\)\\|\\(no matches found\\)\\).*" ("^Grep[/a-zA-Z]* finished with \\(?:\\(\\(?:[0-9]+ \\)?match\\(?:es\\)? found\\)\\|\\(no matches found\\)\\).*"
(0 '(face nil compilation-message nil help-echo nil mouse-face nil) t) (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t)
(1 compilation-info-face nil t) (1 compilation-info-face nil t)
(2 compilation-warning-face nil t)) (2 compilation-warning-face nil t))
@ -552,7 +552,10 @@ Set up `compilation-exit-message-function' and run `grep-setup-hook'."
;; so the buffer is still unmodified if there is no output. ;; so the buffer is still unmodified if there is no output.
(cond ((and (zerop code) (buffer-modified-p)) (cond ((and (zerop code) (buffer-modified-p))
(if (> grep-num-matches-found 0) (if (> grep-num-matches-found 0)
(cons (format "finished with %d matches found\n" grep-num-matches-found) (cons (format (ngettext "finished with %d match found\n"
"finished with %d matches found\n"
grep-num-matches-found)
grep-num-matches-found)
"matched") "matched")
'("finished with matches found\n" . "matched"))) '("finished with matches found\n" . "matched")))
((not (buffer-modified-p)) ((not (buffer-modified-p))

View file

@ -983,7 +983,10 @@ also print the number."
(progn (forward-line 1) (point))) (progn (forward-line 1) (point)))
(setq count (1+ count)))) (setq count (1+ count))))
(set-marker rend nil) (set-marker rend nil)
(when interactive (message "Deleted %d matching lines" count)) (when interactive (message (ngettext "Deleted %d matching line"
"Deleted %d matching lines"
count)
count))
count)) count))
(defun how-many (regexp &optional rstart rend interactive) (defun how-many (regexp &optional rstart rend interactive)
@ -1032,9 +1035,10 @@ a previously found match."
(if (= opoint (point)) (if (= opoint (point))
(forward-char 1) (forward-char 1)
(setq count (1+ count)))) (setq count (1+ count))))
(when interactive (message "%d occurrence%s" (when interactive (message (ngettext "%d occurrence"
count "%d occurrences"
(if (= count 1) "" "s"))) count)
count))
count))) count)))
@ -1617,11 +1621,12 @@ See also `multi-occur'."
(not (eq occur-excluded-properties t)))))) (not (eq occur-excluded-properties t))))))
(let* ((bufcount (length active-bufs)) (let* ((bufcount (length active-bufs))
(diff (- (length bufs) bufcount))) (diff (- (length bufs) bufcount)))
(message "Searched %d buffer%s%s; %s match%s%s" (message "Searched %d %s%s; %s %s%s"
bufcount (if (= bufcount 1) "" "s") bufcount
(ngettext "buffer" "buffers" bufcount)
(if (zerop diff) "" (format " (%d killed)" diff)) (if (zerop diff) "" (format " (%d killed)" diff))
(if (zerop count) "no" (format "%d" count)) (if (zerop count) "no" (format "%d" count))
(if (= count 1) "" "es") (ngettext "match" "matches" count)
;; Don't display regexp if with remaining text ;; Don't display regexp if with remaining text
;; it is longer than window-width. ;; it is longer than window-width.
(if (> (+ (length (or (get-text-property 0 'isearch-string regexp) (if (> (+ (length (or (get-text-property 0 'isearch-string regexp)
@ -1856,14 +1861,15 @@ See also `multi-occur'."
(let ((beg (point)) (let ((beg (point))
end) end)
(insert (propertize (insert (propertize
(format "%d match%s%s%s in buffer: %s%s\n" (format "%d %s%s%s in buffer: %s%s\n"
matches (if (= matches 1) "" "es") matches
(ngettext "match" "matches" matches)
;; Don't display the same number of lines ;; Don't display the same number of lines
;; and matches in case of 1 match per line. ;; and matches in case of 1 match per line.
(if (= lines matches) (if (= lines matches)
"" (format " in %d line%s" "" (format " in %d %s"
lines lines
(if (= lines 1) "" "s"))) (ngettext "line" "lines" lines)))
;; Don't display regexp for multi-buffer. ;; Don't display regexp for multi-buffer.
(if (> (length buffers) 1) (if (> (length buffers) 1)
"" (occur-regexp-descr regexp)) "" (occur-regexp-descr regexp))
@ -1889,13 +1895,15 @@ See also `multi-occur'."
(goto-char (point-min)) (goto-char (point-min))
(let ((beg (point)) (let ((beg (point))
end) end)
(insert (format "%d match%s%s total%s:\n" (insert (format "%d %s%s total%s:\n"
global-matches (if (= global-matches 1) "" "es") global-matches
(ngettext "match" "matches" global-matches)
;; Don't display the same number of lines ;; Don't display the same number of lines
;; and matches in case of 1 match per line. ;; and matches in case of 1 match per line.
(if (= global-lines global-matches) (if (= global-lines global-matches)
"" (format " in %d line%s" "" (format " in %d %s"
global-lines (if (= global-lines 1) "" "s"))) global-lines
(ngettext "line" "lines" global-lines)))
(occur-regexp-descr regexp))) (occur-regexp-descr regexp)))
(setq end (point)) (setq end (point))
(when title-face (when title-face
@ -2730,10 +2738,10 @@ characters."
(1+ num-replacements)))))) (1+ num-replacements))))))
(when (and (eq def 'undo-all) (when (and (eq def 'undo-all)
(null (zerop num-replacements))) (null (zerop num-replacements)))
(message "Undid %d %s" num-replacements (message (ngettext "Undid %d replacement"
(if (= num-replacements 1) "Undid %d replacements"
"replacement" num-replacements)
"replacements")) num-replacements)
(ding 'no-terminate) (ding 'no-terminate)
(sit-for 1))) (sit-for 1)))
(setq replaced nil last-was-undo t last-was-act-and-show nil))) (setq replaced nil last-was-undo t last-was-act-and-show nil)))
@ -2859,9 +2867,10 @@ characters."
last-was-act-and-show nil)))))) last-was-act-and-show nil))))))
(replace-dehighlight)) (replace-dehighlight))
(or unread-command-events (or unread-command-events
(message "Replaced %d occurrence%s%s" (message (ngettext "Replaced %d occurrence%s"
"Replaced %d occurrences%s"
replace-count)
replace-count replace-count
(if (= replace-count 1) "" "s")
(if (> (+ skip-read-only-count (if (> (+ skip-read-only-count
skip-filtered-count skip-filtered-count
skip-invisible-count) skip-invisible-count)