gdritter repos khuzd / d49cf9e
Added more general bilingual feature Getty Ritter 11 years ago
2 changed file(s) with 40 addition(s) and 44 deletion(s). Collapse all Expand all
88 import Data.Monoid ((<>))
99 import Data.Text (Text)
1010 import qualified Data.Text as T
11 import Data.Time (UTCTime)
11 import Data.Time (UTCTime(..))
12 import Data.Time.Calendar (toGregorian)
1213 import Data.Time.Format (formatTime)
1314 import Prelude (String, ($), (++), (==), return, Bool(..), Maybe(..), show)
1415 import qualified Prelude as P
154155 editLink _ = return ()
155156 editURL = toValue (urlForPost post <> "/edit")
156157
157 formatDate :: UTCTime -> String
158 formatDate = formatTime defaultTimeLocale "%e %B, %Y"
158 toMonth :: P.Int -> Html
159 toMonth n = span ! class_ "bilingual"
160 ! dataAttribute "english" ename
161 ! dataAttribute "dwarvish" dname
162 $ return ()
163 where ename = [ "January", "February", "March"
164 , "April", "May", "June"
165 , "July", "August", "September"
166 , "October", "November", "December"
167 ] P.!! n
168 dname = [ "Granite", "Slate", "Felsite"
169 , "Hematite", "Malachite", "Galena"
170 , "Limestone", "Sandstone", "Timber"
171 , "Moonstone", "Opal", "Obsidian"
172 ] P.!! n
173
174 formatDate :: UTCTime -> Html
175 formatDate t = do
176 toMarkup (show day)
177 " "
178 toMonth month
179 ", "
180 toMarkup (show year)
181 where (year, month, day) = toGregorian (utctDay t)
159182
160183 listBody :: [PostRef] -> Html
161184 listBody ps = div ! class_ "list" $ P.mapM_ go ps
1 function toDwarfMonth(t){
2 return(t.replace('January','Granite')
3 .replace('February','Slate')
4 .replace('March','Felsite')
5 .replace('April','Hematite')
6 .replace('May','Malachite')
7 .replace('June','Galena')
8 .replace('July','Limestone')
9 .replace('August','Sandstone')
10 .replace('September','Timber')
11 .replace('October','Moonstone')
12 .replace('November','Opal')
13 .replace('December','Obsidian'));
14 };
15
16 function fromDwarfMonth(t) {
17 return(t.replace('Granite','January')
18 .replace('Slate','February')
19 .replace('Felsite','March')
20 .replace('Hematite','April')
21 .replace('Malachite','May')
22 .replace('Galena','June')
23 .replace('Limestone','July')
24 .replace('Sandstone','August')
25 .replace('Timber','September')
26 .replace('Moonstone','October')
27 .replace('Opal','November')
28 .replace('Obsidian','December'));
29 };
30
31 function dateReplace(f){
32 var spans=document.getElementsByTagName('span');
1 functionateReplace(toDwarvish){
2 var spans=document.getElementsByClassName('bilingual');
333 for(var i in spans) {
34 if(spans[i].className&&spans[i].className.search('date')!== -1)
35 spans[i].innerHTML=f(spans[i].innerHTML);
4 var elem = spans[i];
5 if (! (elem && elem.dataset)) continue;
6
7 if (toDwarvish)
8 elem.innerHTML = elem.dataset['dwarvish'];
9 else
10 elem.innerHTML = elem.dataset['english'];
3611 }
3712 };
3813
4116 is_dwarf=!is_dwarf;
4217 if (window.localStorage)
4318 window.localStorage.setItem('dwarvish',is_dwarf);
44 dateReplace(is_dwarf?toDwarfMonth:fromDwarfMonth);
19 dateReplace(is_dwarf);
4520 };
4621 function scroll() {
4722 var posY=( document.documentElement.scrollTop
5126 };
5227 var bg=document.getElementById('bg');
5328 var is_dwarf=(window.localStorage&&window.localStorage.getItem('dwarvish')=='true');
54 var spans=document.getElementsByTagName('span');
55 for (var i in spans) {
56 if (spans[i].className&&spans[i].className.search('date')!==-1) {
57 spans[i].onclick=toggle
58 }
29 var elems=document.getElementsByClassName('bilingual');
30 for (var i in elems) {
31 elems[i].onclick=toggle;
5932 }
60 dateReplace(is_dwarf?toDwarfMonth:fromDwarfMonth);
33 dateReplace(is_dwarf);
6134 scroll();
6235
6336 window.onscroll=scroll;