Skip to content

Commit

Permalink
change argument order of customTr and customTrf
Browse files Browse the repository at this point in the history
Reason:
1. Match argument order of tr and trf which all start with translations
   (helps partial application)
2. Clarfies that the lift function and the CustomReplacements kind of go
   togehter since they require the same type. One is for lifting
   parts in between placeholders and one is for providing custom values
   for placeholders.
  • Loading branch information
ChristophP committed Dec 14, 2020
1 parent 7cb0a56 commit e874db8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
28 changes: 14 additions & 14 deletions src/I18Next.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module I18Next exposing
( Translations, Delims(..), Replacements, CustomReplacements
, initialTranslations
( Translations, Delims(..), Replacements, CustomReplacements, initialTranslations
, translationsDecoder
, t, tr, tf, trf
, t, tr, tf, trf, customTr, customTrf
, keys, hasKey
, Tree, fromTree, string, object
, customTr, customTrf
)

{-| This library provides a solution to load and display translations in your
Expand Down Expand Up @@ -259,18 +257,19 @@ provide the proper markup.
-}
import Html exposing (text, a)
customTr text translationsEn Curly "call-to-action" [ ( "elm-website", a [href "https://elm-lang.org"] [text "https://elm-lang.org"] ) ]
customTr translationsEn Curly "call-to-action" text [ ( "elm-website", a [href "https://elm-lang.org"] [text "https://elm-lang.org"] ) ]
-- Go to <a href="https://elm-lang.org">https://elm-lang.org</a> for more information.
If you only want `String`s though, use [`tr`](I18Next#tr) instead.
-}
customTr : (String -> a) -> Translations -> Delims -> String -> CustomReplacements a -> List a
customTr lift (Translations translations) =
customReplace lift <| \translationKey -> Dict.get translationKey translations
customTr : Translations -> Delims -> String -> (String -> a) -> CustomReplacements a -> List a
customTr (Translations translations) =
customReplace (\translationKey -> Dict.get translationKey translations)


customReplace : (String -> a) -> (String -> Maybe String) -> Delims -> String -> CustomReplacements a -> List a
customReplace lift getTranslations delims translationKey replacements =
customReplace : (String -> Maybe String) -> Delims -> String -> (String -> a) -> CustomReplacements a -> List a
customReplace getTranslations delims translationKey lift replacements =
case getTranslations translationKey of
Just rawString ->
let
Expand Down Expand Up @@ -338,10 +337,10 @@ customReplace lift getTranslations delims translationKey replacements =

{-| Like [`customTr`](I18Next#customTr) but with support for fallback languages.
-}
customTrf : (String -> a) -> List Translations -> Delims -> String -> CustomReplacements a -> List a
customTrf lift translationsList =
customReplace lift <|
\translationsKey ->
customTrf : List Translations -> Delims -> String -> (String -> a) -> CustomReplacements a -> List a
customTrf translationsList =
customReplace
(\translationsKey ->
let
getByKey (Translations translations) =
Dict.get translationsKey translations
Expand All @@ -351,6 +350,7 @@ customTrf lift translationsList =
|> List.head
|> Maybe.withDefault translationsKey
|> Just
)


{-| Translate a value at a key, while replacing placeholders.
Expand Down
28 changes: 14 additions & 14 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -162,55 +162,55 @@ translateWithCustomizedReturnType =
describe "the customTr function"
[ test "translates and replaces placeholders" <|
\_ ->
customTr Text translationsEn Curly "greetings.goodDay" [ ( "firstName", Link "Test" ), ( "lastName", Link "Max" ) ]
customTr translationsEn Curly "greetings.goodDay" Text [ ( "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 Text translationsEn Curly "greetings.goodDay" [ ( "lastName", Link "Max" ) ]
customTr translationsEn Curly "greetings.goodDay" Text [ ( "lastName", Link "Max" ) ]
|> Expect.equal [ Text "Good Day {{firstName}} ", Link "Max", Text "" ]
, test "can handle malformed nested inner placeholders" <|
\_ ->
let
malformedTranslations =
I18Next.fromTree [ ( "test", I18Next.string "pre __weird __stuff__ __ suff" ) ]
in
customTr Text malformedTranslations Underscore "test" [ ( "stuff", Link "Max" ) ]
customTr malformedTranslations Underscore "test" Text [ ( "stuff", Link "Max" ) ]
|> Expect.equal [ Text "pre __weird ", Link "Max", Text " __ suff" ]
, test "can handle malformed nested outer placeholders" <|
\_ ->
let
malformedTranslations =
I18Next.fromTree [ ( "test", I18Next.string "pre __weird __stuff__ __ suff" ) ]
in
customTr Text malformedTranslations Underscore "test" [ ( "weird __stuff__ ", Link "Max" ) ]
customTr malformedTranslations Underscore "test" Text [ ( "weird __stuff__ ", Link "Max" ) ]
|> Expect.equal [ Text "pre ", Link "Max", Text " suff" ]
, test "can handle malformed replacement, that looks like a placeholder" <|
\_ ->
let
malformedTranslations =
I18Next.fromTree [ ( "test", I18Next.string "pre __placeholder__ suff" ) ]
in
customTr Text malformedTranslations Underscore "test" [ ( "__placeholder__", Text "__placeholder__" ) ]
customTr malformedTranslations Underscore "test" Text [ ( "__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 ->
let
arbitraryTranslation =
I18Next.fromTree [ ( "test", I18Next.string <| pre ++ "__" ++ placeholder ++ "__" ++ post ) ]
in
customTr Text arbitraryTranslation Underscore "test" [ ( placeholder, Link replacement ) ]
customTr arbitraryTranslation Underscore "test" Text [ ( placeholder, Link replacement ) ]
|> Expect.equal [ Text pre, Link replacement, Text post ]
, test "can handle multiple occurences" <|
\_ ->
let
multipleOccurences =
I18Next.fromTree [ ( "test", I18Next.string "pre __placeholder1__ a __placeholder2__ b __placeholder1__ c __placeholder2__ suff" ) ]
in
customTr Text multipleOccurences Underscore "test" [ ( "placeholder1", Link "Max" ), ( "placeholder2", Link "Pattern" ) ]
customTr multipleOccurences Underscore "test" Text [ ( "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 Text translationsEn Curly "some.non-existing.key" []
customTr translationsEn Curly "some.non-existing.key" Text []
|> Expect.equal [ Text "some.non-existing.key" ]
]

Expand All @@ -220,27 +220,27 @@ translateWithPlaceholdersAndFallbackAndCustomizedReturnType =
describe "the customTrf function"
[ test "uses the german when the key exists" <|
\() ->
customTrf Text langList Curly "greetings.hello" []
customTrf langList Curly "greetings.hello" Text []
|> Expect.equal [ Text "Hallo" ]
, test "uses english as a fallback" <|
\() ->
customTrf Text langList Curly "englishOnly" []
customTrf langList Curly "englishOnly" Text []
|> Expect.equal [ Text "This key only exists in english" ]
, test "uses the key if none is found" <|
\() ->
customTrf Text langList Curly "some.non-existing.key" []
customTrf langList Curly "some.non-existing.key" Text []
|> Expect.equal [ Text "some.non-existing.key" ]
, test "translates and replaces in german when key is found" <|
\() ->
customTrf Text langList Curly "greetings.goodDay" [ ( "firstName", Link "Peter" ), ( "lastName", Link "Griffin" ) ]
customTrf langList Curly "greetings.goodDay" Text [ ( "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 Text langList Curly "englishOnlyPlaceholder" [ ( "firstName", Link "Peter" ), ( "lastName", Link "Griffin" ) ]
customTrf langList Curly "englishOnlyPlaceholder" Text [ ( "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 Text langList Curly "greetings.goodDay" [ ( "lastName", Link "Max" ) ]
customTrf langList Curly "greetings.goodDay" Text [ ( "lastName", Link "Max" ) ]
|> Expect.equal [ Text "Guten Tag {{firstName}} ", Link "Max", Text "" ]
]

Expand Down

0 comments on commit e874db8

Please sign in to comment.