flolac-tw/main_site

Improve the bilingual design

L-TChen opened this issue · 3 comments

Ideally, a bilingual page should consist of a skeleton page (a html tree where leaves are text variables) and two sets of actual texts in different languages (which can be stored in the yaml front matter in the same file). It follows that each version of the same page will have exactly the same html structure and style.

An easy solution:

a locale file can be loaded as a context to replace variables in a template using

metadataFieldFrom :: FilePath -> Context a
metadataFieldFrom fp = Context $ \k _ _ -> do
    let id     = fromFilePath fp
        empty' = noResult $ "No '" ++ k ++ "' field in metadata " ++
            "of item " ++ show id
    value <- getMetadataField id k
    maybe empty' (return . StringField) value

Then, we can combine this context when loading the template.

It is better to write a specialised template loader with type:

Identifier -> Lang -> Context a -> Item a -> Compiler (Item String)

and it should pick the right locale file and set lang variable accordingly.

Also, the following functions will be used:

loadAndApplyTemplateLC :: Identifier -> String -> Context a -> Item a -> Compiler (Item String)
loadAndApplyTemplateLC id lc cxt = 
    let cxt' = constField "lang" lc <> metadataFieldFrom (toFilePath id ++ '.':lc) <> cxt
    in loadAndApplyTemplate id cxt' 

applyAsTemplateLC :: String -> Context String -> Item String -> Compiler (Item String)
applyAsTemplateLC lc cxt item = 
    let locale = metadataFieldFrom $ toFilePath (itemIdentifier item) ++ '.':lc 
    in applyAsTemplate (constField "lang" lc <> locale <> cxt) item

These two variants load a file or a template filename with its localisation file filename.lc.

Closed by 6c958bf.