Skip to content

Commit

Permalink
Merge pull request #14 from koterpillar/language-parse
Browse files Browse the repository at this point in the history
Parse language variants properly
  • Loading branch information
koterpillar committed Mar 11, 2015
2 parents ef8b900 + 00b5bc3 commit 2a8cf55
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Language.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ parseLanguage :: MonadPlus m => String -> m Language
parseLanguage [c1, c2] = case fromChars c1 c2 of
Just lang -> return lang
Nothing -> mzero
parseLanguage (c1:c2:'-':_) = parseLanguage [c1, c2]
parseLanguage _ = mzero

showLanguage :: Language -> String
Expand Down
17 changes: 17 additions & 0 deletions testsuite/TestLanguage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ test_languageHeader = do
(languageHeader $ Just "ru,zh;q=0.8,en;q=0.6")
(mkPreference [(RU, 1), (ZH, 0.8), (EN, 0.6)])

test_parseLanguage = do
assertEqual
(Just EN)
(parseLanguage "en")
assertEqual
(Just IT)
(parseLanguage "it")
assertEqual
Nothing
(parseLanguage "zz")
assertEqual
(Just EN)
(parseLanguage "en-AU")
assertEqual
(Just ZH)
(parseLanguage "zh-Hans")

instance Arbitrary LanguagePreference where
arbitrary = mkPreference <$> arbitrary

Expand Down

0 comments on commit 2a8cf55

Please sign in to comment.