From 1ec7a07e19a0f99ac68edcc9696822dff4501f26 Mon Sep 17 00:00:00 2001 From: ChristophP Date: Thu, 17 Dec 2020 23:56:32 +0100 Subject: [PATCH] change argument order again This seems to make the most sense. Explanation here:https://github.com/ChristophP/elm-i18next/issues/26 --- README.md | 9 +++++++-- src/I18Next.elm | 13 ++++++++----- tests/Tests.elm | 28 ++++++++++++++-------------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 5a4d7d9..3ec2c9b 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,13 @@ instead do the same as in the simple example but apply the decoder to the Http c ## Advanced Stuff: Placeholders and fallback languages -There is also support for placeholders and fallback languages. Check the -official [docs](http://package.elm-lang.org/packages/ChristophP/elm-i18next/latest/I18Next) +Here are some supported features for advanced use cases: +- Support for string placeholders +- Support for non-string placeholders such as `Html` +- Fallback languages + +Check the officialj +[docs](http://package.elm-lang.org/packages/ChristophP/elm-i18next/latest/I18Next) for usage examples. ## Adding Type safety diff --git a/src/I18Next.elm b/src/I18Next.elm index cc61af8..1821edd 100644 --- a/src/I18Next.elm +++ b/src/I18Next.elm @@ -251,25 +251,28 @@ type CustomTranslationElement a {-| Sometimes it can be useful to replace placeholders with other things than just `String`s. Imagine you have translations containing a sentence with a link and you want to provide the proper markup. +_Hint:_ The third argument is a function which will be called for any string pieces that +AREN'T placeholders, so that the types of replacements and the other other string parts match. +In most cases you'll just pass `Html.text` here. {- If your translations are { "call-to-action": "Go to {{elm-website}} for more information." } ... -} import Html exposing (text, a) - customTr translationsEn Curly "call-to-action" text [ ( "elm-website", a [href "https://elm-lang.org"] [text "https://elm-lang.org"] ) ] + customTr translationsEn Curly text "call-to-action" [ ( "elm-website", a [href "https://elm-lang.org"] [text "https://elm-lang.org"] ) ] -- Go to https://elm-lang.org for more information. If you only want `String`s though, use [`tr`](I18Next#tr) instead. -} -customTr : Translations -> Delims -> String -> (String -> a) -> CustomReplacements a -> List a +customTr : Translations -> Delims -> (String -> a) -> String -> CustomReplacements a -> List a customTr (Translations translations) = customReplace (\translationKey -> Dict.get translationKey translations) -customReplace : (String -> Maybe String) -> Delims -> String -> (String -> a) -> CustomReplacements a -> List a -customReplace getTranslations delims translationKey lift replacements = +customReplace : (String -> Maybe String) -> Delims -> (String -> a) -> String -> CustomReplacements a -> List a +customReplace getTranslations delims lift translationKey replacements = case getTranslations translationKey of Just rawString -> let @@ -337,7 +340,7 @@ customReplace getTranslations delims translationKey lift replacements = {-| Like [`customTr`](I18Next#customTr) but with support for fallback languages. -} -customTrf : List Translations -> Delims -> String -> (String -> a) -> CustomReplacements a -> List a +customTrf : List Translations -> Delims -> (String -> a) -> String -> CustomReplacements a -> List a customTrf translationsList = customReplace (\translationsKey -> diff --git a/tests/Tests.elm b/tests/Tests.elm index 753898e..e463474 100644 --- a/tests/Tests.elm +++ b/tests/Tests.elm @@ -162,11 +162,11 @@ translateWithCustomizedReturnType = describe "the customTr function" [ test "translates and replaces placeholders" <| \_ -> - customTr translationsEn Curly "greetings.goodDay" Text [ ( "firstName", Link "Test" ), ( "lastName", Link "Max" ) ] + customTr translationsEn Curly Text "greetings.goodDay" [ ( "firstName", Link "Test" ), ( "lastName", Link "Max" ) ] |> Expect.equal [ Text "Good Day ", Link "Test", Text " ", Link "Max", Text "" ] , test "tr does not replace if the match can't be found" <| \_ -> - customTr translationsEn Curly "greetings.goodDay" Text [ ( "lastName", Link "Max" ) ] + customTr translationsEn Curly Text "greetings.goodDay" [ ( "lastName", Link "Max" ) ] |> Expect.equal [ Text "Good Day {{firstName}} ", Link "Max", Text "" ] , test "can handle malformed nested inner placeholders" <| \_ -> @@ -174,7 +174,7 @@ translateWithCustomizedReturnType = malformedTranslations = I18Next.fromTree [ ( "test", I18Next.string "pre __weird __stuff__ __ suff" ) ] in - customTr malformedTranslations Underscore "test" Text [ ( "stuff", Link "Max" ) ] + customTr malformedTranslations Underscore Text "test" [ ( "stuff", Link "Max" ) ] |> Expect.equal [ Text "pre __weird ", Link "Max", Text " __ suff" ] , test "can handle malformed nested outer placeholders" <| \_ -> @@ -182,7 +182,7 @@ translateWithCustomizedReturnType = malformedTranslations = I18Next.fromTree [ ( "test", I18Next.string "pre __weird __stuff__ __ suff" ) ] in - customTr malformedTranslations Underscore "test" Text [ ( "weird __stuff__ ", Link "Max" ) ] + customTr malformedTranslations Underscore Text "test" [ ( "weird __stuff__ ", Link "Max" ) ] |> Expect.equal [ Text "pre ", Link "Max", Text " suff" ] , test "can handle malformed replacement, that looks like a placeholder" <| \_ -> @@ -190,7 +190,7 @@ translateWithCustomizedReturnType = malformedTranslations = I18Next.fromTree [ ( "test", I18Next.string "pre __placeholder__ suff" ) ] in - customTr malformedTranslations Underscore "test" Text [ ( "__placeholder__", Text "__placeholder__" ) ] + customTr malformedTranslations Underscore Text "test" [ ( "__placeholder__", Text "__placeholder__" ) ] |> Expect.equal [ Text "pre __placeholder__ suff" ] , Test.fuzz3 Fuzz.string (Fuzz.tuple ( Fuzz.string, Fuzz.string )) Fuzz.string "can replace an arbitrary placeholder" <| \pre ( placeholder, replacement ) post -> @@ -198,7 +198,7 @@ translateWithCustomizedReturnType = arbitraryTranslation = I18Next.fromTree [ ( "test", I18Next.string <| pre ++ "__" ++ placeholder ++ "__" ++ post ) ] in - customTr arbitraryTranslation Underscore "test" Text [ ( placeholder, Link replacement ) ] + customTr arbitraryTranslation Underscore Text "test" [ ( placeholder, Link replacement ) ] |> Expect.equal [ Text pre, Link replacement, Text post ] , test "can handle multiple occurences" <| \_ -> @@ -206,11 +206,11 @@ translateWithCustomizedReturnType = multipleOccurences = I18Next.fromTree [ ( "test", I18Next.string "pre __placeholder1__ a __placeholder2__ b __placeholder1__ c __placeholder2__ suff" ) ] in - customTr multipleOccurences Underscore "test" Text [ ( "placeholder1", Link "Max" ), ( "placeholder2", Link "Pattern" ) ] + customTr multipleOccurences Underscore Text "test" [ ( "placeholder1", Link "Max" ), ( "placeholder2", Link "Pattern" ) ] |> Expect.equal [ Text "pre ", Link "Max", Text " a ", Link "Pattern", Text " b ", Link "Max", Text " c ", Link "Pattern", Text " suff" ] , test "tr returns the key if it doesn not exists" <| \() -> - customTr translationsEn Curly "some.non-existing.key" Text [] + customTr translationsEn Curly Text "some.non-existing.key" [] |> Expect.equal [ Text "some.non-existing.key" ] ] @@ -220,27 +220,27 @@ translateWithPlaceholdersAndFallbackAndCustomizedReturnType = describe "the customTrf function" [ test "uses the german when the key exists" <| \() -> - customTrf langList Curly "greetings.hello" Text [] + customTrf langList Curly Text "greetings.hello" [] |> Expect.equal [ Text "Hallo" ] , test "uses english as a fallback" <| \() -> - customTrf langList Curly "englishOnly" Text [] + customTrf langList Curly Text "englishOnly" [] |> Expect.equal [ Text "This key only exists in english" ] , test "uses the key if none is found" <| \() -> - customTrf langList Curly "some.non-existing.key" Text [] + customTrf langList Curly Text "some.non-existing.key" [] |> Expect.equal [ Text "some.non-existing.key" ] , test "translates and replaces in german when key is found" <| \() -> - customTrf langList Curly "greetings.goodDay" Text [ ( "firstName", Link "Peter" ), ( "lastName", Link "Griffin" ) ] + customTrf langList Curly Text "greetings.goodDay" [ ( "firstName", Link "Peter" ), ( "lastName", Link "Griffin" ) ] |> Expect.equal [ Text "Guten Tag ", Link "Peter", Text " ", Link "Griffin", Text "" ] , test "translates and replaces in fallback when key is not found" <| \() -> - customTrf langList Curly "englishOnlyPlaceholder" Text [ ( "firstName", Link "Peter" ), ( "lastName", Link "Griffin" ) ] + customTrf langList Curly Text "englishOnlyPlaceholder" [ ( "firstName", Link "Peter" ), ( "lastName", Link "Griffin" ) ] |> Expect.equal [ Text "Only english with ", Link "Peter", Text " ", Link "Griffin", Text "" ] , test "does not replace if the match can't be found" <| \_ -> - customTrf langList Curly "greetings.goodDay" Text [ ( "lastName", Link "Max" ) ] + customTrf langList Curly Text "greetings.goodDay" [ ( "lastName", Link "Max" ) ] |> Expect.equal [ Text "Guten Tag {{firstName}} ", Link "Max", Text "" ] ]