Skip to content

Commit beb4919

Browse files
committed
Allow to pass locale as atom into t/t! functions
1 parent dde41fa commit beb4919

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

lib/linguist/compiler.ex

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule Linguist.Compiler do
4242
@simple_interpol "%{"
4343

4444
def compile(translations) do
45-
langs = Keyword.keys(translations)
45+
langs = Keyword.keys(translations) |> Enum.map(&to_string/1)
4646

4747
translations =
4848
for {locale, source} <- translations do
@@ -51,6 +51,9 @@ defmodule Linguist.Compiler do
5151

5252
quote do
5353
def t(locale, path, binding \\ [])
54+
def t(locale, path, binding) when is_atom(locale) do
55+
t(to_string(locale), path, binding)
56+
end
5457
unquote(translations)
5558
def do_t(_locale, _path, _bindings), do: {:error, :no_translation}
5659

lib/linguist/memorized_vocabulary.ex

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ defmodule Linguist.MemorizedVocabulary do
3737
def t(locale, path, bindings \\ [])
3838
def t(nil, _, _), do: raise(LocaleError, nil)
3939

40+
def t(locale, path, binding) when is_atom(locale) do
41+
t(to_string(locale), path, binding)
42+
end
43+
4044
def t(locale, path, bindings) do
4145
pluralization_key = Application.fetch_env!(:linguist, :pluralization_key)
4246
norm_locale = normalize_locale(locale)

test/vocabulary_test.exs

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ defmodule VocabularyTest do
2626
assert ["fr", "en", "es"] == I18n.locales()
2727
end
2828

29+
test "it handles both string and atom locales" do
30+
assert I18n.t!("en", "foo") == I18n.t!(:en, "foo")
31+
assert I18n.t("en", "foo") == I18n.t(:en, "foo")
32+
end
33+
2934
test "it handles translations at rool level" do
3035
assert I18n.t!("en", "foo") == "bar"
3136
assert I18n.t("en", "foo") == {:ok, "bar"}

0 commit comments

Comments
 (0)