Skip to content

Commit 4b1778a

Browse files
committed
Merge branch 'manual-language-switch'
2 parents 97c7474 + c98a7b5 commit 4b1778a

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

src/App.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module App where
44
import Control.Monad.State
55

66
import qualified Data.ByteString.Char8 as B
7+
import Data.Maybe
78
import qualified Data.Text as T
89
import Data.Time
910

@@ -66,8 +67,12 @@ dailyIndex = articleList . byDate
6667
languageHeaderM :: AppPart LanguagePreference
6768
languageHeaderM = do
6869
request <- askRq
69-
let header = getHeader "Accept-Language" request
70-
return $ languageHeader $ liftM B.unpack header
70+
let header = B.unpack $ fromMaybe "" $ getHeader "Accept-Language" request
71+
param <- looks "lang"
72+
let langValue = listToMaybe $ catMaybes $ map notEmpty $ param ++ [header]
73+
return $ languageHeader langValue
74+
where notEmpty "" = Nothing
75+
notEmpty x = Just x
7176

7277
html :: ToMessage a => a -> AppPart Response
7378
html = ok . toResponse

src/Language.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ newtype LanguagePreference = LanguagePreference { unLanguagePreference :: Langua
3030
defaultLanguage :: Language
3131
defaultLanguage = EN
3232

33+
singleLanguage :: Language -> LanguagePreference
34+
singleLanguage lang = LanguagePreference $ M.singleton lang 1
35+
3336
rankLanguage :: Language -> LanguagePreference -> Float
3437
rankLanguage lang = fromMaybe 0 . M.lookup lang . unLanguagePreference
3538

testsuite/Integration/Base.hs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,44 @@ assertContains needle haystack =
4444
-- Happstack doesn't make it easy
4545
mkRequest :: String -> IO Request
4646
mkRequest rPath = do
47+
let (rUri, rParams) = splitUriParam rPath
4748
inputsBody <- newEmptyMVar
4849
rBody <- newMVar (Body LB.empty)
4950
return Request { rqSecure = False
5051
, rqMethod = GET
51-
, rqPaths = filter (/= "") $ splitOn "/" rPath
52+
, rqPaths = filter (/= "") $ splitOn "/" rUri
5253
, rqUri = rPath
53-
, rqQuery = ""
54-
, rqInputsQuery = []
54+
, rqQuery = "?" ++ rParams
55+
, rqInputsQuery = splitParams rParams
5556
, rqInputsBody = inputsBody
5657
, rqCookies = []
5758
, rqVersion = HttpVersion 1 1
5859
, rqHeaders = M.empty
5960
, rqBody = rBody
6061
, rqPeer = ("", 0)
6162
}
63+
where splitUriParam :: String -> (String, String)
64+
splitUriParam rPath = case splitOn "?" rPath of
65+
[rUri] -> (rUri, "")
66+
[rUri, rParams] -> (rUri, rParams)
67+
splitParams :: String -> [(String, Input)]
68+
splitParams = map (mkParamTuple . splitOn "=") . filter (/= "") . splitOn "&"
69+
mkParamTuple :: [String] -> (String, Input)
70+
mkParamTuple [k, v] = (k, mkInputValue v)
71+
mkParamTuple [k] = (k, mkInputValue "")
72+
mkInputValue str = Input { inputValue = Right (LB.fromStrict $ U.fromString str)
73+
, inputFilename = Nothing
74+
, inputContentType = ContentType {ctType = "text", ctSubtype = "plain", ctParameters = []}
75+
}
6276

6377
withLang :: LanguagePreference -> Request -> Request
6478
withLang lang req = req { rqHeaders = newHeaders }
6579
where newHeaders = M.insert "accept-language" (HeaderPair "Accept-Language" [U.fromString pref]) (rqHeaders req)
6680
pref = show lang
6781

82+
withLang1 :: Language -> Request -> Request
83+
withLang1 lang = withLang $ singleLanguage lang
84+
6885
-- Extract contents from a response
6986
responseContent :: Response -> IO String
7087
responseContent r@(Response _ _ _ _ _) = return $ U.toString $ LB.toStrict $ rsBody r

testsuite/Integration/TestHome.hs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,29 @@
22

33
module Integration.TestHome where
44

5-
import Integration.Base
5+
import Control.Monad
6+
7+
import Data.LanguageCodes
68

79
import Test.Framework
810

11+
import Integration.Base
12+
913

1014
test_home = do
1115
req <- mkRequest "/"
1216
home <- testRequest req
1317
resp <- responseContent home
1418
assertContains "Test site" resp
19+
20+
test_home_lang = do
21+
req <- liftM (withLang1 RU) (mkRequest "/")
22+
home <- testRequest req
23+
resp <- responseContent home
24+
assertContains "Главная" resp
25+
26+
test_explicit_lang = do
27+
req <- mkRequest "/?lang=ru"
28+
home <- testRequest req
29+
resp <- responseContent home
30+
assertContains "Главная" resp

0 commit comments

Comments
 (0)