Skip to content

Commit

Permalink
Add mix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Barrett committed Apr 16, 2018
1 parent 085f67a commit 7497c3c
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
3 changes: 1 addition & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use Mix.Config

config :linguist,
pluralization_key: :count
config :linguist, pluralization_key: :count
3 changes: 1 addition & 2 deletions lib/linguist.ex
Original file line number Diff line number Diff line change
@@ -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
30 changes: 20 additions & 10 deletions lib/linguist/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule Linguist.Compiler do
end
"""

@interpol_rgx ~r/
@interpol_rgx ~r/
(?<head>)
(?<!%) %{.+?}
(?<tail>)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
3 changes: 1 addition & 2 deletions lib/linguist/vocabulary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ defmodule Linguist.Vocabulary do
else
source
end

@locales {name, loaded_source}
end
end

end

2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 1 addition & 3 deletions test/en.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
users: [
title: "Users",
profiles: [
title: "Profiles",
title: "Profiles"
]
],
escaped: "%%{escaped}",
Expand All @@ -19,5 +19,3 @@
other: "%{count} apples"
]
]


20 changes: 9 additions & 11 deletions test/escaping_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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


30 changes: 19 additions & 11 deletions test/vocabulary_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -95,5 +104,4 @@ defmodule LinguistTest do
end
end
end

end

0 comments on commit 7497c3c

Please sign in to comment.