Permalink
Browse files

Add multilingual example

  • Loading branch information...
1 parent 33935a1 commit 5efe99b31e34fc54f6b276acc45d411e05e24d31 @jaspervdj committed May 31, 2012
View
1 README.markdown
@@ -16,3 +16,4 @@ More or less in order of increasing complexity:
references/ Explains how to use a bibliography
timedblog/ Adds conditional page generation and timed releases
to the simple blog.
+ multilingual/ A simple implementation of a multilingual website
View
5 multilingual/en/index.markdown
@@ -0,0 +1,5 @@
+---
+title: About us
+---
+
+Welcome to our webpage!
View
5 multilingual/fr/index.markdown
@@ -0,0 +1,5 @@
+---
+title: Sur nous
+---
+
+Bienvenue dans notre site web!
View
5 multilingual/nl/index.markdown
@@ -0,0 +1,5 @@
+---
+title: Over ons
+---
+
+Welkom op onze pagina!
View
71 multilingual/site.hs
@@ -0,0 +1,71 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
+
+import Control.Arrow (arr, second, (&&&), (>>>))
+import Control.Category (id)
+import Control.Monad (forM_)
+import Data.Maybe (fromMaybe)
+import Prelude hiding (id)
+import Text.Blaze.Html (Html, (!))
+import Text.Blaze.Html.Renderer.String (renderHtml)
+import qualified Text.Blaze.Html5 as H
+import qualified Text.Blaze.Html5.Attributes as A
+
+import Hakyll
+
+
+-- | Converts the page path, e.g. 'en/about.html' to the 'en' language code and
+-- the 'about.html' path.
+getLanguageCode :: Compiler (Page a) (String, String)
+getLanguageCode = getRoute >>>
+ arr (fmap splitLanguageCode) >>>
+ arr (fromMaybe ("en", ""))
+ where
+ splitLanguageCode = second (drop 1) . break (== '/')
+
+-- | List of all supported languages
+languages :: [(String, String)]
+languages =
+ [ ("en", "English")
+ , ("nl", "Dutch")
+ , ("fr", "Français")
+ ]
+
+-- | Generate an HTML menu for all languages except the current one using
+-- blaze-html.
+languageNavigation :: String -> String -> Html
+languageNavigation code path =
+ forM_ translations $ \(c, name) ->
+ H.a ! A.href (H.toValue $ "/" ++ c ++ "/" ++ path) $
+ H.toHtml name
+ where
+ translations = filter ((/= code) . fst) languages
+
+-- | Compiler version of 'languageNavigation'
+getLanguageNavigation :: Compiler (Page a) String
+getLanguageNavigation = getLanguageCode >>>
+ arr (uncurry languageNavigation) >>>
+ arr renderHtml
+
+-- | Sets $languages$ to the language navigation menu en $language$ to the
+-- current language code.
+addLanguages :: Compiler (Page a) (Page a)
+addLanguages =
+ (getLanguageCode >>> arr fst) &&& id >>>
+ arr (uncurry (setField "language")) >>>
+ getLanguageNavigation &&& id >>>
+ arr (uncurry (setField "languages"))
+
+main :: IO ()
+main = hakyll $ do
+ match "**.markdown" $ do
+ route $ setExtension "html"
+ compile $ pageCompiler
+ >>> addLanguages
+ >>> applyTemplateCompiler "templates/default.html"
+
+ match "templates/*" $ compile templateCompiler
+
+ match "style/*.css" $ do
+ route idRoute
+ compile compressCssCompiler
View
41 multilingual/style/screen.css
@@ -0,0 +1,41 @@
+html {
+ font-size: 16px;
+ font-family: sans-serif;
+}
+
+body {
+ background-color: #690011;
+ margin: 0px;
+}
+
+div#header {
+ margin: 24px auto 12px auto;
+ width: 624px;
+}
+
+div#header a {
+ color: #f2d99c;
+ font-size: 30px;
+ font-weight: bold;
+ text-decoration: none;
+}
+
+div#content {
+ background-color: #f2d99c;
+ border-bottom: solid 6px #e5b96f;
+ border-top: solid 6px #e5b96f;
+ color: black;
+ margin: 12px auto 12px auto;
+ padding: 6px 12px 6px 12px;
+ width: 600px;
+}
+
+div#languages {
+ text-align: center;
+ font-size: 14px;
+}
+
+div#languages a {
+ color: #f2d99c;
+ margin: 12px 6px 12px 6px;
+}
View
13 multilingual/templates/default.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <link rel="stylesheet" href="/style/screen.css" type="text/css">
+ <title>$title$</title>
+ </head>
+ <body>
+ <div id="header"><a href="/$language$">$title$</a></div>
+ <div id="content">$body$</div>
+ <div id="languages">$languages$</div>
+ </body>
+</html>

0 comments on commit 5efe99b

Please sign in to comment.