blob: 1673b22eba35c46649dad397150fc8e17374b823 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
module Message
( getMessage
, getVarMessage
) where
import Data.Maybe (listToMaybe, fromMaybe)
import Text.ParserCombinators.Parsec
import Message.Key (Key)
import Message.Lang
import Message.Translations (getNonFormattedMessage)
getMessage :: Key -> String
getMessage = getVarMessage []
getVarMessage :: [String] -> Key -> String
getVarMessage values key =
replaceParts values (getParts (getNonFormattedMessage French key))
replaceParts :: [String] -> [Part] -> String
replaceParts values = concatMap (replacePart values)
replacePart :: [String] -> Part -> String
replacePart _ (Str str) = str
replacePart values (Num n) =
fromMaybe ("{" ++ show n ++ "}") . listToMaybe . drop (n - 1) $ values
data Part =
Num Int
| Str String
getParts :: String -> [Part]
getParts str =
case parse partsParser "" str of
Right parts -> parts
Left _ -> []
partsParser :: Parser [Part]
partsParser = many partParser
partParser :: Parser Part
partParser =
(do _ <- string "{"; n <- read <$> many1 digit; _ <- string "}"; return (Num n))
<|> (do str <- many1 (noneOf "{"); return (Str str))
|