aboutsummaryrefslogtreecommitdiff
path: root/src/client/Model
diff options
context:
space:
mode:
authorJoris Guyonvarch2015-08-11 21:32:10 +0200
committerJoris Guyonvarch2015-08-11 21:32:10 +0200
commit892a7dd19a92fc18767984e624b8a5026dce61e4 (patch)
tree7d59d58d96958e91a4e9114be0effb9ec7a082a8 /src/client/Model
parenta3bab4f2a0cc8a6a95753dc91d8e862f40c80dcc (diff)
Showing server sides generated messages from the client
Diffstat (limited to 'src/client/Model')
-rw-r--r--src/client/Model/Translations.elm70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/client/Model/Translations.elm b/src/client/Model/Translations.elm
new file mode 100644
index 0000000..2a8a3a7
--- /dev/null
+++ b/src/client/Model/Translations.elm
@@ -0,0 +1,70 @@
+module Model.Translations
+ ( translationsDecoder
+ , Translations
+ , Translation
+ , getMessage
+ , getVarMessage
+ ) where
+
+import Maybe exposing (withDefault)
+import Json.Decode as Json exposing ((:=))
+import String
+
+type alias Translations = List Translation
+
+translationsDecoder : Json.Decoder Translations
+translationsDecoder =
+ ("translations" := Json.list translationDecoder)
+
+type alias Translation =
+ { key : String
+ , message : List MessagePart
+ }
+
+getTranslation : String -> Translations -> Maybe (List MessagePart)
+getTranslation key translations =
+ translations
+ |> List.filter (\translation -> translation.key == key)
+ |> List.head
+ |> Maybe.map .message
+
+translationDecoder : Json.Decoder Translation
+translationDecoder =
+ Json.object2 Translation
+ ("key" := Json.string)
+ ("message" := Json.list partDecoder)
+
+type MessagePart =
+ Order Int
+ | Str String
+
+partDecoder : Json.Decoder MessagePart
+partDecoder =
+ ("tag" := Json.string) `Json.andThen` partDecoderWithTag
+
+partDecoderWithTag : String -> Json.Decoder MessagePart
+partDecoderWithTag tag =
+ case tag of
+ "Order" -> Json.object1 Order ("contents" := Json.int)
+ "Str" -> Json.object1 Str ("contents" := Json.string)
+
+-----
+
+getMessage : String -> Translations -> String
+getMessage = getVarMessage []
+
+getVarMessage : List String -> String -> Translations -> String
+getVarMessage values key translations =
+ getTranslation key translations
+ |> Maybe.map (\parts -> String.concat (List.map (replacePart values) parts))
+ |> withDefault key
+
+replacePart : List String -> MessagePart -> String
+replacePart values part =
+ case part of
+ Str str -> str
+ Order n ->
+ values
+ |> List.drop (n - 1)
+ |> List.head
+ |> withDefault ("{" ++ (toString n) ++ "}")