Skip to content

Commit

Permalink
change argument order again
Browse files Browse the repository at this point in the history
This seems to make the most sense.
Explanation here:#26
  • Loading branch information
ChristophP committed Dec 17, 2020
1 parent e874db8 commit 1ec7a07
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions src/I18Next.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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 : 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
Expand Down Expand Up @@ -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 ->
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 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" <|
\_ ->
let
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" <|
\_ ->
let
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" <|
\_ ->
let
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 ->
let
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" <|
\_ ->
let
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" ]
]

Expand All @@ -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 "" ]
]

Expand Down

0 comments on commit 1ec7a07

Please sign in to comment.