diff options
author | Joris | 2017-03-26 21:10:42 +0200 |
---|---|---|
committer | Joris | 2017-03-26 21:10:42 +0200 |
commit | 1e47a7754ca38bd1a6c74765d8378caf68ce4619 (patch) | |
tree | d0d9238479dc2529a1b558bbbcde346e7e8c2935 /src/client/Model/Translations.elm | |
parent | c0ac16a713c4e53cf6af8e72a6d5f6b8ac5d6456 (diff) | |
download | budget-1e47a7754ca38bd1a6c74765d8378caf68ce4619.tar.gz budget-1e47a7754ca38bd1a6c74765d8378caf68ce4619.tar.bz2 budget-1e47a7754ca38bd1a6c74765d8378caf68ce4619.zip |
Separate client and server watch
Diffstat (limited to 'src/client/Model/Translations.elm')
-rw-r--r-- | src/client/Model/Translations.elm | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/client/Model/Translations.elm b/src/client/Model/Translations.elm new file mode 100644 index 0000000..9b314e1 --- /dev/null +++ b/src/client/Model/Translations.elm @@ -0,0 +1,68 @@ +module Model.Translations exposing + ( translationsDecoder + , Translations + , Translation + , getMessage + , getParamMessage + ) + +import Maybe exposing (withDefault) +import Json.Decode as Decode exposing (Decoder) +import String + +type alias Translations = List Translation + +translationsDecoder : Decoder Translations +translationsDecoder = Decode.list translationDecoder + +type alias Translation = + { key : String + , message : List MessagePart + } + +getTranslation : String -> Translations -> Maybe (List MessagePart) +getTranslation key translations = + translations + |> List.filter (\translation -> String.toLower translation.key == String.toLower key) + |> List.head + |> Maybe.map .message + +translationDecoder : Decoder Translation +translationDecoder = + Decode.map2 Translation + (Decode.field "key" Decode.string) + (Decode.field "message" (Decode.list partDecoder)) + +type MessagePart = + Order Int + | Str String + +partDecoder : Decoder MessagePart +partDecoder = (Decode.field "tag" Decode.string) |> Decode.andThen partDecoderWithTag + +partDecoderWithTag : String -> Decoder MessagePart +partDecoderWithTag tag = + case tag of + "Order" -> Decode.map Order (Decode.field "contents" Decode.int) + _ -> Decode.map Str (Decode.field "contents" Decode.string) + +----- + +getMessage : Translations -> String -> String +getMessage = getParamMessage [] + +getParamMessage : List String -> Translations -> String -> String +getParamMessage values translations key = + 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) ++ "}") |