From 4e30f24974a2aa698afcb7b3e639030c816e0a41 Mon Sep 17 00:00:00 2001
From: John MacFarlane <fiddlosopher@gmail.com>
Date: Sun, 1 Jan 2012 14:32:27 -0800
Subject: [PATCH] EPUB writer: Allow `lang` variable to set language in
 metadata.

Defaults to locale language if `lang` is not set.
---
 README                          | 7 ++++---
 src/Text/Pandoc/Writers/EPUB.hs | 7 +++++--
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/README b/README
index 5b4de3942..177b40ada 100644
--- a/README
+++ b/README
@@ -418,9 +418,10 @@ Options
 
     By default, pandoc will include the following metadata elements:
     `<dc:title>` (from the document title), `<dc:creator>` (from the
-    document authors), `<dc:language>` (from the locale), and
-    `<dc:identifier id="BookId">` (a randomly generated UUID). Any of
-    these may be overridden by elements in the metadata file.
+    document authors), `<dc:language>` (from the `lang` variable, or,
+    if is not set, the locale), and `<dc:identifier id="BookId">` (a
+    randomly generated UUID). Any of these may be overridden by elements
+    in the metadata file.
 
 `-D` *FORMAT*, `--print-default-template=`*FORMAT*
 :   Print the default template for an output *FORMAT*. (See `-t`
diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs
index a817f07a5..af907a809 100644
--- a/src/Text/Pandoc/Writers/EPUB.hs
+++ b/src/Text/Pandoc/Writers/EPUB.hs
@@ -120,8 +120,11 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do
   let chapterEntries = zipWith chapterToEntry [1..] chapters
 
   -- contents.opf
-  lang <- catch (liftM (takeWhile (/='.')) $ getEnv "LANG")
-                (\_ -> return "en-US")
+  localeLang <- catch (liftM (takeWhile (/='.')) $ getEnv "LANG")
+                    (\_ -> return "en-US")
+  let lang = case lookup "lang" (writerVariables opts') of
+                     Just x  -> x
+                     Nothing -> localeLang
   uuid <- getRandomUUID
   let chapterNode ent = unode "item" !
                            [("id", takeBaseName $ eRelativePath ent),