fastcgi server: add /mylanguage resource, which selects the language that best matches the Accept-language header.

This commit is contained in:
bjorn
2008-09-17 12:22:17 +00:00
parent 50e600763f
commit 417ada31f5
2 changed files with 20 additions and 5 deletions

View File

@@ -38,6 +38,7 @@ cgiMain pgf =
"/translate" -> return (doTranslate pgf) `ap` getText `ap` getCat `ap` getFrom `ap` getTo
"/categories" -> return $ doCategories pgf
"/languages" -> return $ doLanguages pgf
"/mylanguage" -> return (doMyLanguage pgf) `ap` requestAcceptLanguage
_ -> throwCGIError 404 "Not Found" ["Resource not found: " ++ path]
outputJSON json
where
@@ -112,6 +113,8 @@ doCategories :: PGF -> JSValue
doCategories pgf = showJSON $ map toJSObject
[[("cat",cat)] | cat <- PGF.categories pgf]
doMyLanguage :: PGF -> Maybe (Accept Language) -> JSValue
doMyLanguage pgf macc = showJSON $ toJSObject [("languageName", selectLanguage pgf macc)]
-- * PGF utilities
@@ -147,6 +150,18 @@ linearize' pgf mto tree =
Nothing -> PGF.linearizeAllLang pgf tree
Just to -> [(to,PGF.linearize pgf to tree)]
selectLanguage :: PGF -> Maybe (Accept Language) -> PGF.Language
selectLanguage pgf macc = case acceptable of
[] -> case PGF.languages pgf of
[] -> "" -- FIXME: error?
l:_ -> l
Language c:_ -> fromJust (langCodeLanguage pgf c)
where langCodes = mapMaybe (PGF.languageCode pgf) (PGF.languages pgf)
acceptable = negotiate (map Language langCodes) macc
langCodeLanguage :: PGF -> String -> Maybe PGF.Language
langCodeLanguage pgf code = listToMaybe [l | l <- PGF.languages pgf, PGF.languageCode pgf l == Just code]
-- * General CGI and JSON stuff
outputJSON :: JSON a => a -> CGI CGIResult

View File

@@ -12,11 +12,11 @@ executable gf.fcgi
unix,
directory,
containers,
gf,
cgi,
fastcgi,
json,
utf8-string
gf >= 3.0,
cgi >= 3001.1.7.0,
fastcgi >= 3001.0.2.1,
json >= 0.3.3,
utf8-string >= 0.3.1.1
main-is: MainFastCGI.hs
other-modules:
FastCGIUtils