Skip to content

Commit

Permalink
fix: actually support ordinal plurals
Browse files Browse the repository at this point in the history
  • Loading branch information
cschmatzler committed Sep 6, 2023
1 parent ac5c0c5 commit 2eda631
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/idiom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Idiom do
to: String.t(),
fallback: String.t() | list(String.t()),
count: integer() | float() | Decimal.t() | String.t(),
plural: :cardinal | :ordinal,
cache_table_name: atom()
]

Expand Down Expand Up @@ -91,6 +92,7 @@ defmodule Idiom do
Keyword.get(opts, :fallback) || Application.get_env(:idiom, :default_fallback)

count = Keyword.get(opts, :count)
plural = Keyword.get(opts, :plural, :cardinal)
bindings = Map.put_new(bindings, :count, count)

locale_resolve_hierarchy =
Expand All @@ -106,7 +108,7 @@ defmodule Idiom do
|> Enum.flat_map(fn key ->
[
{locale, namespace, key},
{locale, namespace, "#{key}_#{Plural.get_suffix(locale, count)}"}
{locale, namespace, "#{key}_#{Plural.get_suffix(locale, count, type: plural)}"}
]
end)

Expand Down
3 changes: 3 additions & 0 deletions test/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"Natural language: the colon-ing": "Colons",
"carrot_one": "1 carrot",
"carrot_other": "{{count}} carrots",
"cake_one": "1st cake",
"cake_two": "2nd cake",
"cake_other": "{{count}}th cake",
"welcome": "welcome, {{name}}",
"Welcome to our site 😊": "Welcome to our site 😊"
},
Expand Down
5 changes: 4 additions & 1 deletion test/idiom/cache/init_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ defmodule Idiom.Cache.InitTest do
{"ar", "default", "Hello world"} => "مرحبا بالعالم",
{"en", "default", "Welcome to our site 😊"} => "Welcome to our site 😊",
{"ja", "default", "Hello world"} => "こんにちは世界",
{"zh", "default", "Hello world"} => "你好世界"
{"zh", "default", "Hello world"} => "你好世界",
{"en", "default", "cake_one"} => "1st cake",
{"en", "default", "cake_other"} => "{{count}}th cake",
{"en", "default", "cake_two"} => "2nd cake"
}
end
end
16 changes: 16 additions & 0 deletions test/idiom/idiom/t_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ defmodule Idiom.Idiom.TTest do
opts: [count: 2, cache_table_name: :t_test],
expected: "2 carrots"
},
# With ordinal plural
%{
key: "cake",
opts: [count: 1, plural: :ordinal, cache_table_name: :t_test],
expected: "1st cake"
},
%{
key: "cake",
opts: [count: 2, plural: :ordinal, cache_table_name: :t_test],
expected: "2nd cake"
},
%{
key: "cake",
opts: [count: 10, plural: :ordinal, cache_table_name: :t_test],
expected: "10th cake"
},
# With plural key and explicit suffix
%{
key: "carrot_one",
Expand Down

0 comments on commit 2eda631

Please sign in to comment.