Skip to content

Commit

Permalink
Add bang functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kaitsh committed Oct 22, 2019
1 parent f8e311f commit 0c7faa7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
32 changes: 23 additions & 9 deletions lib/fluex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ defmodule Fluex do
### Fluex API
Fluex provides a `translate/3` and `ltranslate/3` macro to your own Fluex module, like `MyApp.Fluex`.
Fluex provides `translate/3` and `ltranslate/3` macros to your own Fluex module, like `MyApp.Fluex`.
These macros call the `translate/3` and `ltranslate/3` functions from the `Fluex` module
A simple example is:
Expand All @@ -102,10 +102,10 @@ defmodule Fluex do
Fluex.put_locale(MyApp.Fluex, "pt_BR")
msgid = "Hello"
MyApp.Fluex.translate(msgid, %{user: "mundo"})
MyApp.Fluex.translate!(msgid, %{user: "mundo"})
#=> "Olá \u{2068}mundo\u{2069}"
MyApp.Fluex.ltranslate("en", msgid, %{user: "world"})
MyApp.Fluex.ltranslate!("en", msgid, %{user: "world"})
#=> "Hello \u{2068}world\u{2069}"
Expand Down Expand Up @@ -141,27 +141,41 @@ defmodule Fluex do
)
end

def translate!(translator, id, bindings \\ %{}) do
ltranslate!(translator, get_locale(translator), id, bindings)
end

def translate(translator, id, bindings \\ %{}) do
ltranslate(translator, get_locale(translator), id, bindings)
end

def ltranslate!(translator, locale, id, bindings \\ %{}) do
case ltranslate(translator, locale, id, bindings) do
{:ok, msg} ->
msg

{:error, _} ->
raise(
RuntimeError,
"bundles in translator #{translator} do no contain a message with id: #{id}"
)
end
end

def ltranslate(translator, locale, id, bindings \\ %{}) do
bundles = Fluex.Registry.lookup(translator)
locale = Map.get(bundles, locale)
fallback = Map.get(bundles, translator.__fluex__(:default_locale))

cond do
locale && FluentNIF.has_message?(locale, id) ->
FluentNIF.format_pattern(locale, id, stringify(bindings))
{:ok, FluentNIF.format_pattern(locale, id, stringify(bindings))}

fallback && FluentNIF.has_message?(fallback, id) ->
FluentNIF.format_pattern(fallback, id, stringify(bindings))
{:ok, FluentNIF.format_pattern(fallback, id, stringify(bindings))}

true ->
raise(
RuntimeError,
"bundles in translator #{translator} do no contain a message with id: #{id}"
)
{:error, :not_found}
end
end

Expand Down
8 changes: 8 additions & 0 deletions lib/fluex/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,18 @@ defmodule Fluex.Compiler do
Fluex.start_link(unquote(env.module), opts)
end

def translate!(id, bindings \\ %{}) do
Fluex.translate!(unquote(env.module), id, bindings)
end

def translate(id, bindings \\ %{}) do
Fluex.translate(unquote(env.module), id, bindings)
end

def ltranslate!(locale, id, bindings \\ %{}) do
Fluex.ltranslate!(unquote(env.module), locale, id, bindings)
end

def ltranslate(locale, id, bindings \\ %{}) do
Fluex.ltranslate(unquote(env.module), locale, id, bindings)
end
Expand Down
20 changes: 14 additions & 6 deletions test/fluex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,29 @@ defmodule FluexTest do
end
end

describe "translate/3" do
describe "ltranslate/3" do
test "returns message for id and locale" do
assert Translator.ltranslate("en", "tabs-close-tooltip", %{tabCount: 5}) ===
assert Translator.ltranslate!("en", "tabs-close-tooltip", %{tabCount: 5}) ===
"Close \u{2068}5\u{2069} tabs"

assert Translator.ltranslate("it", "tabs-close-tooltip", %{tabCount: 2}) ===
assert Translator.ltranslate!("it", "tabs-close-tooltip", %{tabCount: 2}) ===
"Chiudi \u{2068}2\u{2069} schede"

assert TranslatorWithDefaultConfig.translate("hello", %{world: "World"}) ===
assert TranslatorWithDefaultConfig.translate!("hello", %{world: "World"}) ===
"Hello \u{2068}World\u{2069}"
end

test "returns fallback to default locale if message is not translated" do
assert Translator.ltranslate("it", "not-translated") ===
test "returns fallback if message is not translated" do
assert Translator.ltranslate!("it", "not-translated") ===
"Only available in en"
end

test "returns nil or raises if message id does not exist" do
assert Translator.ltranslate("en", "not-existing-id") == {:error, :not_found}

assert_raise RuntimeError, fn ->
Translator.ltranslate!("en", "not-existing-id")
end
end
end
end

0 comments on commit 0c7faa7

Please sign in to comment.