Skip to content

Commit

Permalink
Add translation, locale and resource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaitsh committed Oct 21, 2019
1 parent 11b1f53 commit 855687b
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 19 deletions.
6 changes: 5 additions & 1 deletion lib/fluex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ defmodule Fluex do
end

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

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

cond do
Expand Down
10 changes: 9 additions & 1 deletion lib/fluex/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Fluex.Compiler do
default_locale =
opts[:default_locale] || quote(do: Application.fetch_env!(:fluex, :default_locale))

requested_locales = Keyword.get(opts, :requested, [])
requested_locales = Keyword.get(opts, :locales, [])
known_locales = known_locales(translations_dir)
resolved_locales = resolve_locales(known_locales, requested_locales)

Expand Down Expand Up @@ -53,6 +53,10 @@ defmodule Fluex.Compiler do
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
end
end

Expand All @@ -67,6 +71,10 @@ defmodule Fluex.Compiler do
end
end

defp resolve_locales(available, []) do
available
end

defp resolve_locales(available, requested) do
MapSet.intersection(Enum.into(available, MapSet.new()), Enum.into(requested, MapSet.new()))
|> MapSet.to_list()
Expand Down
9 changes: 4 additions & 5 deletions lib/fluex/resources.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ defmodule Fluex.Resources do
end

defp resources_in_dir(dir, resources) do
resources = "{#{Enum.join(resources, ",")}}"

dir
|> Path.join(resources)
|> Path.wildcard()
resources
|> Enum.map(fn path ->
Path.join(dir, path)
end)
end

defp resource_from_path(root, path) do
Expand Down
1 change: 1 addition & 0 deletions priv/fluex/en/fluex_test.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello = Hello {$world}
22 changes: 22 additions & 0 deletions test/fluex/resources_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Fluex.ResourcesTest do
use ExUnit.Case
alias FluexTest.TranslatorWithMultipleResources, as: Translator
alias Fluex.Resources

test "merge_resources/3 merges all resources to build one bundle" do
merged = Resources.merge_resources(Translator, "en", ["test.ftl", "second/second.ftl"])
assert merged =~ "tabs-close-button"
assert merged =~ "second-level"
end

test "build_resources/3 loads resource files and returns a list of ftl funtions" do
assert [
{_, _, [{:ftl, [context: Fluex.Resources], ["en", "test.ftl"]}, [_]]},
{_, _, [{:ftl, [context: Fluex.Resources], ["en", "second/second.ftl"]}, [_]]}
] =
Resources.build_resources(Translator.__fluex__(:dir), "en", [
"test.ftl",
"second/second.ftl"
])
end
end
73 changes: 61 additions & 12 deletions test/fluex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,78 @@ defmodule FluexTest do

defmodule Translator do
use Fluex,
otp_app: :fluex,
otp_app: :fluex_test,
dir: "test/resources",
resources: ["fluex.ftl"],
requested: ["en", "it"]
resources: ["test.ftl"],
locales: ["en", "de", "it"]
end

defmodule TranslatorWithMultipleResources do
use Fluex,
otp_app: :fluex_test,
dir: "test/resources",
resources: ["test.ftl", "second/second.ftl"]
end

defmodule TranslatorWithDefaultConfig do
use Fluex,
otp_app: :fluex_test
end

setup_all do
{:ok, _} = Translator.start_link()
{:ok, _} = TranslatorWithDefaultConfig.start_link()
:ok
end

test "translate/3 returns message for id and locale" do
assert Fluex.translate(Translator, "tabs-close-tooltip", %{tabCount: 5}) ===
"Close \u{2068}5\u{2069} tabs"
describe "get_locale/1 and set_locale/1" do
test "default locale/fallback is \"en\"" do
assert Fluex.get_locale() === "en"
assert Fluex.get_locale(Translator) === "en"
end

test "sets and returns locale" do
Fluex.put_locale(Translator, "it")
assert Fluex.get_locale(Translator) === "it"
end

test "only affects the provided translator" do
Fluex.put_locale(Translator, "it")
assert Fluex.get_locale(Translator) == "it"
assert Fluex.get_locale(TranslatorWithDefaultConfig) == "en"
assert Fluex.get_locale() == "en"
end

Fluex.put_locale(Translator, "it")
test "global locale only affects translators that have no specific locale" do
Fluex.put_locale(Translator, "de")
Fluex.put_locale("it")
assert Fluex.get_locale() == "it"
assert Fluex.get_locale(Translator) == "de"
assert Fluex.get_locale(TranslatorWithDefaultConfig) == "it"
end

assert Fluex.translate(Translator, "tabs-close-tooltip", %{tabCount: 2}) ===
"Chiudi \u{2068}2\u{2069} schede"
test "only accepts binary locale" do
assert_raise ArgumentError, fn ->
Fluex.put_locale(:it)
end
end
end

test "get_locale/1 returns default locale from config" do
assert Fluex.get_locale() === "en"
assert Fluex.get_locale(Translator) === "en"
describe "translate/3" do
test "returns message for id and locale" do
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}) ===
"Chiudi \u{2068}2\u{2069} schede"

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") ===
"Only available in en"
end
end
end
1 change: 1 addition & 0 deletions test/resources/en/second/second.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
second-level = Second level resource
4 changes: 4 additions & 0 deletions test/resources/en/fluex.ftl → test/resources/en/test.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ sync-headline-title =
your data always with you
sync-signedout-title =
Connect with your {-sync-brand-name}
## Testing

not-translated = Only available in en
1 change: 1 addition & 0 deletions test/resources/it/second/second.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
second-level = Risorsa di secondo livello
File renamed without changes.

0 comments on commit 855687b

Please sign in to comment.