From 892a7dd19a92fc18767984e624b8a5026dce61e4 Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Tue, 11 Aug 2015 21:32:10 +0200 Subject: Showing server sides generated messages from the client --- src/client/Main.elm | 8 ++++- src/client/Model.elm | 11 ++++-- src/client/Model/Translations.elm | 70 +++++++++++++++++++++++++++++++++++++++ src/client/View/Header.elm | 3 +- src/client/View/Page.elm | 2 +- src/client/View/SignIn.elm | 8 +++-- 6 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 src/client/Model/Translations.elm (limited to 'src/client') diff --git a/src/client/Main.elm b/src/client/Main.elm index fd0cec7..226ae13 100644 --- a/src/client/Main.elm +++ b/src/client/Main.elm @@ -11,10 +11,12 @@ import Html exposing (Html) import Http import Task exposing (..) import Time exposing (..) +import Json.Decode as Json import Model exposing (Model, initialModel) import Model.Payment exposing (Payments, paymentsDecoder) import Model.Message exposing (messageDecoder) +import Model.Translations exposing (..) import Update exposing (Action(..), actions, updateModel) import Update.SignIn exposing (..) @@ -29,7 +31,7 @@ main : Signal Html main = Signal.map renderPage model model : Signal Model -model = Signal.foldp updateModel (initialModel initialTime) update +model = Signal.foldp updateModel (initialModel initialTime translations) update update : Signal Action update = Signal.mergeMany @@ -47,6 +49,10 @@ port initialTime : Time --------------------------------------- +port translations : String + +--------------------------------------- + port initView : Task Http.Error () port initView = case signInError of diff --git a/src/client/Model.elm b/src/client/Model.elm index 45fdf87..72db56a 100644 --- a/src/client/Model.elm +++ b/src/client/Model.elm @@ -4,16 +4,23 @@ module Model ) where import Time exposing (Time) +import Json.Decode as Json import Model.View exposing (..) +import Model.Translations exposing (..) type alias Model = { view : View , currentTime : Time + , translations : Translations } -initialModel : Time -> Model -initialModel initialTime = +initialModel : Time -> String -> Model +initialModel initialTime translationsValue = { view = LoadingView , currentTime = initialTime + , translations = + case Json.decodeString translationsDecoder translationsValue of + Ok translations -> translations + Err err -> [] } 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) ++ "}") diff --git a/src/client/View/Header.elm b/src/client/View/Header.elm index 788a473..1738d71 100644 --- a/src/client/View/Header.elm +++ b/src/client/View/Header.elm @@ -11,6 +11,7 @@ import ServerCommunication exposing (serverCommunications) import Model exposing (Model) import Model.View exposing (..) +import Model.Translations exposing (getMessage) import View.Icon exposing (renderIcon) @@ -20,7 +21,7 @@ renderHeader model = [] [ h1 [] - [ text "Shared Cost" ] + [ text (getMessage "SharedCost" model.translations) ] , case model.view of LoadingView -> text "" diff --git a/src/client/View/Page.elm b/src/client/View/Page.elm index 59c21a2..f7292ed 100644 --- a/src/client/View/Page.elm +++ b/src/client/View/Page.elm @@ -26,6 +26,6 @@ renderMain model = LoadingView -> renderLoading SignInView signInView -> - renderSignIn signInView + renderSignIn model signInView PaymentView paymentsView -> renderPayments paymentsView diff --git a/src/client/View/SignIn.elm b/src/client/View/SignIn.elm index a790f0a..6fb809d 100644 --- a/src/client/View/SignIn.elm +++ b/src/client/View/SignIn.elm @@ -14,12 +14,14 @@ import Update.SignIn exposing (..) import ServerCommunication as SC import ServerCommunication exposing (serverCommunications) +import Model exposing (Model) import Model.View.SignInView exposing (..) +import Model.Translations exposing (getMessage) import View.Events exposing (onSubmitPrevDefault) -renderSignIn : SignInView -> Html -renderSignIn signInView = +renderSignIn : Model -> SignInView -> Html +renderSignIn model signInView = div [ class "signIn" ] [ H.form @@ -31,7 +33,7 @@ renderSignIn signInView = [] , button [] - [ text "Sign in" ] + [ text (getMessage "SignIn" model.translations)] ] , div [ class "result" ] -- cgit v1.2.3