org-export: Implement multi-backend dictionary

* contrib/lisp/org-export.el (org-export-translate): New function.
(org-export-dictionary): New variable.
* contrib/lisp/org-e-ascii.el (org-e-ascii--build-toc,
  org-e-ascii--list-listings, org-e-ascii--list-tables,
  org-e-ascii-template, org-e-ascii--translate): Use new translation
  system.
(org-e-ascii-dictionary): Remove variable.
This commit is contained in:
Nicolas Goaziou 2012-07-12 13:20:09 +02:00
parent 9fb530dc48
commit 1216df61cc
2 changed files with 102 additions and 130 deletions

View File

@ -116,109 +116,6 @@ structure of the values.")
See `org-export-filters-alist' for more information.")
;;; Internal Variables
(defconst org-e-ascii-dictionary
'(("Footnotes\n"
("en"
:ascii "Footnotes\n"
:latin1 "Footnotes\n"
:utf-8 "Footnotes\n")
("fr"
:ascii "Notes de bas de page\n"
:latin1 "Notes de bas de page\n"
:utf-8 "Notes de bas de page\n"))
("Listing %d: %s"
("en"
:ascii "Listing %d: %s"
:latin1 "Listing %d: %s"
:utf-8 "Listing %d: %s")
("fr"
:ascii "Programme %d : %s"
:latin1 "Programme %d : %s"
:utf-8 "Programme nº %d : %s"))
("List Of Listings\n"
("en"
:ascii "List Of Listings\n"
:latin1 "List Of Listings\n"
:utf-8 "List Of Listings\n")
("fr"
:ascii "Liste des programmes\n"
:latin1 "Liste des programmes\n"
:utf-8 "Liste des programmes\n"))
("List Of Tables\n"
("en"
:ascii "List Of Tables\n"
:latin1 "List Of Tables\n"
:utf-8 "List Of Tables\n")
("fr"
:ascii "Liste des tableaux\n"
:latin1 "Liste des tableaux\n"
:utf-8 "Liste des tableaux\n"))
("Listing %d: "
("en"
:ascii "Listing %d: "
:latin1 "Listing %d: "
:utf-8 "Listing %d: ")
("fr"
:ascii "Programme %d : "
:latin1 "Programme %d : "
:utf-8 "Programme nº %d : "))
("Table Of Contents\n"
("en"
:ascii "Table Of Contents\n"
:latin1 "Table Of Contents\n"
:utf-8 "Table Of Contents\n")
("fr"
:ascii "Sommaire\n"
:latin1 "Table des matières\n"
:utf-8 "Table des matières\n"))
("Table %d: %s"
("en"
:ascii "Table %d: %s"
:latin1 "Table %d: %s"
:utf-8 "Table %d: %s")
("fr"
:ascii "Tableau %d : %s"
:latin1 "Tableau %d : %s"
:utf-8 "Tableau nº %d : %s"))
("See section %s"
("en"
:ascii "See section %s"
:latin1 "See section %s"
:utf-8 "See section %s")
("fr"
:ascii "cf. section %s"
:latin1 "cf. section %s"
:utf-8 "cf. section %s"))
("Table %d: "
("en"
:ascii "Table %d: "
:latin1 "Table %d: "
:utf-8 "Table %d: ")
("fr"
:ascii "Tableau %d : "
:latin1 "Tableau %d : "
:utf-8 "Tableau nº %d : "))
("Unknown reference"
("en"
:ascii "Unknown reference"
:latin1 "Unknown reference"
:utf-8 "Unknown reference")
("fr"
:ascii "Destination inconnue"
:latin1 "Référence inconnue"
:utf-8 "Référence inconnue")))
"Dictionary for ASCII back-end.
Alist whose car is the string to translate and cdr is an alist
whose car is the language string and cdr is a plist whose
properties are possible charsets and value the translated term.
It is used as a database for `org-e-ascii--translate'.")
;;; User Configurable Variables
@ -686,10 +583,10 @@ depth of the table.
Optional argument KEYWORD specifies the TOC keyword, if any, from
which the table of contents generation has been initiated."
(let ((title (org-e-ascii--translate "Table Of Contents\n" info)))
(let ((title (org-e-ascii--translate "Table of Contents" info)))
(concat
title
(make-string (1- (length title))
title "\n"
(make-string (length title)
(if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
"\n\n"
(let ((text-width
@ -711,10 +608,10 @@ which the table of contents generation has been initiated."
KEYWORD is the keyword that initiated the list of listings
generation. INFO is a plist used as a communication channel."
(let ((title (org-e-ascii--translate "List Of Listings\n" info)))
(let ((title (org-e-ascii--translate "List of Listings" info)))
(concat
title
(make-string (1- (length title))
title "\n"
(make-string (length title)
(if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
"\n\n"
(let ((text-width
@ -729,10 +626,10 @@ generation. INFO is a plist used as a communication channel."
;; used to properly align caption right to it in case of
;; filling (like contents of a description list item).
(let ((initial-text
(format (org-e-ascii--translate "Listing %d: " info)
(format (org-e-ascii--translate "Listing %d:" info)
(incf count))))
(concat
initial-text
initial-text " "
(org-trim
(org-e-ascii--indent-string
(org-e-ascii--fill-string
@ -748,10 +645,10 @@ generation. INFO is a plist used as a communication channel."
KEYWORD is the keyword that initiated the list of listings
generation. INFO is a plist used as a communication channel."
(let ((title (org-e-ascii--translate "List Of Tables\n" info)))
(let ((title (org-e-ascii--translate "List of Tables" info)))
(concat
title
(make-string (1- (length title))
title "\n"
(make-string (length title)
(if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
"\n\n"
(let ((text-width
@ -766,10 +663,10 @@ generation. INFO is a plist used as a communication channel."
;; used to properly align caption right to it in case of
;; filling (like contents of a description list item).
(let ((initial-text
(format (org-e-ascii--translate "Table %d: " info)
(format (org-e-ascii--translate "Table %d:" info)
(incf count))))
(concat
initial-text
initial-text " "
(org-trim
(org-e-ascii--indent-string
(org-e-ascii--fill-string
@ -952,11 +849,11 @@ holding export options."
(when definitions
(concat
"\n\n\n"
(let ((title (org-e-ascii--translate "Footnotes\n" info)))
(let ((title (org-e-ascii--translate "Footnotes" info)))
(concat
title
title "\n"
(make-string
(1- (length title))
(length title)
(if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
"\n\n"
(mapconcat
@ -993,17 +890,10 @@ holding export options."
org-e-ascii-global-margin)))
(defun org-e-ascii--translate (s info)
"Translate string S.
INFO is a plist used as a communication channel.
Translation depends on `:language' property and allowed charset.
If no translation in found for a given language and a given
charset, fall-back to S."
(let* ((charset (intern (format ":%s" (plist-get info :ascii-charset))))
(lang (plist-get info :language))
(translations (cdr (assoc s org-e-ascii-dictionary))))
(or (plist-get (cdr (assoc lang translations)) charset) s)))
"Translate string S according to specified language and charset.
INFO is a plist used as a communication channel."
(let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
(org-export-translate s charset info)))

View File

@ -4020,6 +4020,88 @@ a string, or nil."
(cadr (memq blob (org-element-contents parent)))))
;;;; Translation
;;
;; `org-export-translate' translates a string according to language
;; specified by LANGUAGE keyword or `org-export-language-setup'
;; variable and a specified charset. `org-export-dictionary' contains
;; the dictionary used for the translation.
(defun org-export-translate (s encoding info)
"Translate string S according to language specification.
ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
and `:utf8'. INFO is a plist used as a communication channel.
Translation depends on `:language' property. If no translation
in found for a given language and a given encoding, return S."
(let ((lang (plist-get info :language))
(translations (cdr (assoc s org-export-dictionary))))
(or (plist-get (cdr (assoc lang translations)) encoding) s)))
(defconst org-export-dictionary
'(("Footnotes"
("fr"
:ascii "Notes de bas de page"
:latin1 "Notes de bas de page"
:utf-8 "Notes de bas de page"))
("Listing %d: %s"
("fr"
:ascii "Programme %d : %s"
:latin1 "Programme %d : %s"
:utf-8 "Programme nº %d : %s"))
("List of Listings"
("fr"
:ascii "Liste des programmes"
:latin1 "Liste des programmes"
:utf-8 "Liste des programmes"))
("List of Tables"
("fr"
:ascii "Liste des tableaux"
:latin1 "Liste des tableaux"
:utf-8 "Liste des tableaux"))
("Listing %d:"
("fr"
:ascii "Programme %d :"
:latin1 "Programme %d :"
:utf-8 "Programme nº %d :"))
("Table of Contents"
("fr"
:ascii "Sommaire"
:latin1 "Table des matières"
:utf-8 "Table des matières"))
("Table %d: %s"
("fr"
:ascii "Tableau %d : %s"
:latin1 "Tableau %d : %s"
:utf-8 "Tableau nº %d : %s"))
("See section %s"
("fr"
:ascii "cf. section %s"
:latin1 "cf. section %s"
:utf-8 "cf. section %s"))
("Table %d:"
("fr"
:ascii "Tableau %d :"
:latin1 "Tableau %d :"
:utf-8 "Tableau nº %d :"))
("Unknown reference"
("fr"
:ascii "Destination inconnue"
:latin1 "Référence inconnue"
:utf-8 "Référence inconnue")))
"Dictionary for export engine.
Alist whose CAR is the string to translate and CDR is an alist
whose CAR is the language string and CDR is a plist whose
properties are possible charsets and values translated terms.
It is used as a database for `org-export-translate'. Since this
function returns the string as-is if no translation was found,
the variable only needs to record values different from the
entry.")
;;; The Dispatcher
;;