Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default language to system locale (suggestion by @rmsrosa) #29

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/supported_languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
Current languages to choose from are:
- English
- Variants
- `:en_US` (using `:en` is an alias for `:en_US`)
- `:en_UK`
- `:en_US` (aliases: `:en`)
- `:en_UK` (aliases: `:en_GB`; equivalencies: `:en_NZ`, `:en_AU`)
- Dictionaries supported include `:modern`, `:british`, and `:european`, and default to the former.
19 changes: 14 additions & 5 deletions src/SpelledOut.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ include("en.jl")

"""
```julia
spelled_out(number; lang = :en, dict = :modern)
function spelled_out(
number::Number;
lang::Symbol = <locale language>,
dict::Symbol = :modern
)
```

Spells out a number in words, in lowercase, given a specified language. The `dict` keyword argument only applies to some languages; see "Supported Languages" in the docs for more information.
Spells out a number in words, in lowercase, given a specified language. The default language is the one used on your system. The `dict` keyword argument only applies to some languages; see "Supported Languages" in the docs for more information.

---

Expand All @@ -31,10 +35,15 @@ julia> spelled_out(112, lang = :en, dict = :european)
"one hundred twelve"
```
"""
function spelled_out(number::Number; lang::Symbol = :en, dict::Symbol = :modern)
if lang == :en || lang == :en_US
function spelled_out(
number::Number;
lang::Symbol = Symbol(first(split(ENV["LANG"], '.'))),
dict::Symbol = :modern
)

if lang ∈ (:en, :en_US)
return spelled_out_en(number, british = false, dict = dict)
elseif lang == :en_UK
elseif lang ∈ (:en_UK, :en_GB, :en_NZ, :en_AU)
return spelled_out_en(number, british = true, dict = dict)
end

Expand Down
8 changes: 2 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# include(joinpath(dirname(dirname(@__FILE__)), "src", "SpelledOut.jl")); using .SpelledOut
include(joinpath(dirname(dirname(@__FILE__)), "src", "SpelledOut.jl")); using .SpelledOut
using Test
using SpelledOut
# using SpelledOut

@testset "English" begin
@test spelled_out(1234, lang = :en) == "one thousand, two hundred thirty-four"
@test spelled_out(1234, lang = :en_US) == "one thousand, two hundred thirty-four"
@test spelled_out(1234) == "one thousand, two hundred thirty-four" # default to english
@test spelled_out(1234, lang = :en_UK) == "one thousand, two hundred and thirty-four"
@test Spelled_out(1234, lang = :en) == "One thousand, two hundred thirty-four"
@test Spelled_Out(1234, lang = :en) == "One Thousand, Two Hundred Thirty-Four"
@test SPELLED_OUT(1234, lang = :en) == "ONE THOUSAND, TWO HUNDRED THIRTY-FOUR"
@test spelled_out(123456789, lang = :en) == "one hundred twenty-three million, four hundred fifty-six thousand, seven hundred eighty-nine"
@test spelled_out(123456789, lang = :en_UK) == "one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine"
@test spelled_out(0, lang = :en) == "zero"
Expand Down