diff options
| author | bjorn <bjorn@bringert.net> | 2008-09-17 12:22:17 +0000 |
|---|---|---|
| committer | bjorn <bjorn@bringert.net> | 2008-09-17 12:22:17 +0000 |
| commit | 417ada31f54e04e01e30ab111dbba95b6d7413bf (patch) | |
| tree | 38c59fda8fbc9f2b6044c3d8098824e9c69e2572 /src/server/MainFastCGI.hs | |
| parent | 50e600763f7e0f8bcce7252bbd197f97b77bc67d (diff) | |
fastcgi server: add /mylanguage resource, which selects the language that best matches the Accept-language header.
Diffstat (limited to 'src/server/MainFastCGI.hs')
| -rw-r--r-- | src/server/MainFastCGI.hs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/server/MainFastCGI.hs b/src/server/MainFastCGI.hs index a769e23e0..181af7836 100644 --- a/src/server/MainFastCGI.hs +++ b/src/server/MainFastCGI.hs @@ -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 |
