Skip to content

Commit

Permalink
feat: add Idiom.direction/1 (#23)
Browse files Browse the repository at this point in the history
closes #23
  • Loading branch information
cschmatzler authored Sep 2, 2023
1 parent 1b4db7a commit 7654c03
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/idiom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ defmodule Idiom do
@doc false
defdelegate child_spec(options), to: Idiom.Supervisor

defdelegate direction(locale), to: Locales

@type translate_opts() :: [
namespace: String.t(),
to: String.t(),
Expand Down
21 changes: 21 additions & 0 deletions lib/idiom/locales.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule Idiom.Locales do
Locale identifiers consist of a language code, an optional script code, and an optional region code, separated by a hyphen (-).
"""

@rtl_languages ~w(ar dv fa ha he ks ps sd ur yi)

@doc """
Constructs a hierarchical list of locale identifiers from the given locale.
Expand Down Expand Up @@ -79,4 +81,23 @@ defmodule Idiom.Locales do
parts -> Enum.take(parts, 2) |> Enum.join("-")
end
end

@doc """
Returns the writing direction of the script belonging to the locale.
## Examples
```elixir
iex> Idiom.Locales.direction("en-US")
:ltr
iex> Idiom.Locales.direction("ar")
:rtl
```
"""
@spec direction(String.t()) :: :ltr | :rtl
def direction(locale) do
language = get_language(locale)
if Enum.member?(@rtl_languages, language), do: :rtl, else: :ltr
end
end
3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ defmodule Idiom.MixProject do

defp docs do
[
main: "Idiom",
extras: ["Cheatsheet.cheatmd"]
main: "Idiom"
]
end
end

0 comments on commit 7654c03

Please sign in to comment.