From 7497c3c9aec6debcfa91304457ca4bed1b5ee1ce Mon Sep 17 00:00:00 2001 From: Will Barrett Date: Mon, 16 Apr 2018 13:22:22 -0700 Subject: [PATCH] Add mix formatting --- .formatter.exs | 3 +++ config/config.exs | 3 +-- lib/linguist.ex | 3 +-- lib/linguist/compiler.ex | 30 ++++++++++++++++++++---------- lib/linguist/vocabulary.ex | 3 +-- mix.exs | 2 +- test/en.exs | 4 +--- test/escaping_test.exs | 20 +++++++++----------- test/vocabulary_test.exs | 30 +++++++++++++++++++----------- 9 files changed, 56 insertions(+), 42 deletions(-) create mode 100644 .formatter.exs diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..d304ff3 --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,3 @@ +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/config/config.exs b/config/config.exs index 0af69db..b9b77ee 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,4 +1,3 @@ use Mix.Config -config :linguist, - pluralization_key: :count +config :linguist, pluralization_key: :count diff --git a/lib/linguist.ex b/lib/linguist.ex index fc41e38..d5ab005 100644 --- a/lib/linguist.ex +++ b/lib/linguist.ex @@ -1,10 +1,9 @@ defmodule Linguist do - defmodule NoTranslationError do defexception [:message] + def exception(message) do %NoTranslationError{message: "No translation found for #{message}"} end end - end diff --git a/lib/linguist/compiler.ex b/lib/linguist/compiler.ex index d17c5be..ff69dd5 100644 --- a/lib/linguist/compiler.ex +++ b/lib/linguist/compiler.ex @@ -29,7 +29,7 @@ defmodule Linguist.Compiler do end """ - @interpol_rgx ~r/ + @interpol_rgx ~r/ (?) (?) @@ -39,7 +39,8 @@ defmodule Linguist.Compiler do @simple_interpol "%{" def compile(translations) do - langs = Keyword.keys translations + langs = Keyword.keys(translations) + translations = for {locale, source} <- translations do deftranslations(to_string(locale), "", source) @@ -49,13 +50,17 @@ defmodule Linguist.Compiler do def t(locale, path, binding \\ []) unquote(translations) def do_t(_locale, _path, _bindings), do: {:error, :no_translation} + def t!(locale, path, bindings \\ []) do case t(locale, path, bindings) do - {:ok, translation} -> translation + {:ok, translation} -> + translation + {:error, :no_translation} -> raise %NoTranslationError{message: "#{locale}: #{path}"} end end + def locales do unquote(langs) end @@ -72,12 +77,14 @@ defmodule Linguist.Compiler do quote do def t(locale, path, bindings) do pluralization_key = Application.fetch_env!(:linguist, :pluralization_key) + if Keyword.has_key?(bindings, pluralization_key) do plural_atom = Cardinal.plural_rule( Keyword.get(bindings, pluralization_key), locale ) + new_path = "#{path}.#{plural_atom}" do_t(locale, new_path, bindings) else @@ -94,23 +101,26 @@ defmodule Linguist.Compiler do end defp interpolate(string, var) do - @interpol_rgx - |> Regex.split(string, on: [:head, :tail]) - |> Enum.reduce( "", fn + @interpol_rgx + |> Regex.split(string, on: [:head, :tail]) + |> Enum.reduce("", fn <<"%{" <> rest>>, acc -> - key = String.to_atom(String.rstrip(rest, ?})) + key = String.to_atom(String.rstrip(rest, ?})) bindings = Macro.var(var, __MODULE__) + quote do unquote(acc) <> to_string(Keyword.fetch!(unquote(bindings), unquote(key))) end - segment, acc -> quote do: (unquote(acc) <> unquote(unescape(segment))) - end ) + + segment, acc -> + quote do: unquote(acc) <> unquote(unescape(segment)) + end) end defp append_path("", next), do: to_string(next) defp append_path(current, next), do: "#{current}.#{next}" defp unescape(segment) do - Regex.replace @escaped_interpol_rgx, segment, @simple_interpol + Regex.replace(@escaped_interpol_rgx, segment, @simple_interpol) end end diff --git a/lib/linguist/vocabulary.ex b/lib/linguist/vocabulary.ex index 3986516..268553d 100644 --- a/lib/linguist/vocabulary.ex +++ b/lib/linguist/vocabulary.ex @@ -73,9 +73,8 @@ defmodule Linguist.Vocabulary do else source end + @locales {name, loaded_source} end end - end - diff --git a/mix.exs b/mix.exs index 37140b9..f4158db 100644 --- a/mix.exs +++ b/mix.exs @@ -1,4 +1,4 @@ -Code.ensure_loaded?(Hex) and Hex.start +Code.ensure_loaded?(Hex) and Hex.start() defmodule Linguist.Mixfile do use Mix.Project diff --git a/test/en.exs b/test/en.exs index 4acb0af..6cbed43 100644 --- a/test/en.exs +++ b/test/en.exs @@ -10,7 +10,7 @@ users: [ title: "Users", profiles: [ - title: "Profiles", + title: "Profiles" ] ], escaped: "%%{escaped}", @@ -19,5 +19,3 @@ other: "%{count} apples" ] ] - - diff --git a/test/escaping_test.exs b/test/escaping_test.exs index 459eaca..9fbcc2d 100644 --- a/test/escaping_test.exs +++ b/test/escaping_test.exs @@ -4,34 +4,32 @@ defmodule EscapingTest do defmodule Esc do use Linguist.Vocabulary - locale "en", [] + locale("en", []) - locale "fr", [ - level: - [ + locale( + "fr", + level: [ basic: "%%{escaped}", mixed: "%{a} %%{a} %{a} %%{a}" ] - ] + ) end test "t does not escape %%{ but replaces %% by %" do - assert Esc.t!("fr", "level.basic" ) == "%{escaped}" + assert Esc.t!("fr", "level.basic") == "%{escaped}" end test "even if key is in the binding" do - assert Esc.t!("fr", "level.basic", escaped: "Does not matter" ) == "%{escaped}" + assert Esc.t!("fr", "level.basic", escaped: "Does not matter") == "%{escaped}" end test "mixed form" do - assert Esc.t!("fr", "level.mixed", a: 42 ) == "42 %{a} 42 %{a}" + assert Esc.t!("fr", "level.mixed", a: 42) == "42 %{a} 42 %{a}" end test "mixed form, no values" do assert_raise KeyError, fn -> - Esc.t! "fr", "level.mixed" + Esc.t!("fr", "level.mixed") end end end - - diff --git a/test/vocabulary_test.exs b/test/vocabulary_test.exs index b31dd7a..899b7ae 100644 --- a/test/vocabulary_test.exs +++ b/test/vocabulary_test.exs @@ -4,24 +4,25 @@ defmodule LinguistTest do defmodule I18n do use Linguist.Vocabulary - locale "en", Path.join([__DIR__, "en.exs"]) + locale("en", Path.join([__DIR__, "en.exs"])) - locale "fr", [ + locale( + "fr", flash: [ notice: [ hello: "salut %{first} %{last}" ], - interpolation_at_beginning: "%{name} at beginning", + interpolation_at_beginning: "%{name} at beginning" ], apple: [ one: "%{count} Pomme", other: "%{count} pommes" ] - ] + ) end test "it returns locales" do - assert ["fr", "en"] == I18n.locales + assert ["fr", "en"] == I18n.locales() end test "it handles translations at rool level" do @@ -42,8 +43,12 @@ defmodule LinguistTest do end test "it interpolates bindings" do - assert I18n.t!("en", "flash.notice.hello", first: "chris", last: "mccord") == "hello chris mccord" - assert I18n.t("en", "flash.notice.hello", first: "chris", last: "mccord") == {:ok, "hello chris mccord"} + assert I18n.t!("en", "flash.notice.hello", first: "chris", last: "mccord") == + "hello chris mccord" + + assert I18n.t("en", "flash.notice.hello", first: "chris", last: "mccord") == + {:ok, "hello chris mccord"} + assert I18n.t!("en", "flash.notice.bye", name: "chris") == "bye now, chris!" assert I18n.t("en", "flash.notice.bye", name: "chris") == {:ok, "bye now, chris!"} end @@ -61,8 +66,11 @@ defmodule LinguistTest do end test "it compiles all locales" do - assert I18n.t!("fr", "flash.notice.hello", first: "chris", last: "mccord") == "salut chris mccord" - assert I18n.t("fr", "flash.notice.hello", first: "chris", last: "mccord") == {:ok, "salut chris mccord"} + assert I18n.t!("fr", "flash.notice.hello", first: "chris", last: "mccord") == + "salut chris mccord" + + assert I18n.t("fr", "flash.notice.hello", first: "chris", last: "mccord") == + {:ok, "salut chris mccord"} end test "t! raises NoTranslationError when translation is missing" do @@ -80,7 +88,8 @@ defmodule LinguistTest do end test "interpolations can exist as the first segment of the translation" do - assert I18n.t!("fr", "flash.interpolation_at_beginning", name: "chris") == "chris at beginning" + assert I18n.t!("fr", "flash.interpolation_at_beginning", name: "chris") == + "chris at beginning" end describe "pluralizations" do @@ -95,5 +104,4 @@ defmodule LinguistTest do end end end - end