Skip to content

Commit

Permalink
Use Logger.bare_log/3 during locale installation. Closes #62
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jun 8, 2018
1 parent 403ae68 commit 244a6db
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 88 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Changelog for Cldr v1.6.1

This is the changelog for Cldr v1.6.1 released on June 8th, 2018. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/cldr/tags)

### Bug Fixes

* Use `Logger.bare_log/3` instead of the logger macros when installing locales. This is because the installation occurs at compile time and its possible that the log level configured is resolved in Distillery at a later stage. Thanks to @erikreedstrom. Closes #62.

# Changelog for Cldr v1.6.0

This is the changelog for Cldr v1.6.0 released on May 18th, 2018. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/cldr/tags)
Expand Down
59 changes: 32 additions & 27 deletions lib/cldr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ defmodule Cldr do
Set the current locale to be used for `Cldr` functions that
take an optional locale parameter for which a locale is not supplied.
## Options
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct returned by `Cldr.Locale.new!/1`
Expand Down Expand Up @@ -307,7 +307,7 @@ defmodule Cldr do
Returns a boolean indicating if the specified locale
name is configured and available in Cldr.
## Options
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
Expand All @@ -330,7 +330,7 @@ defmodule Cldr do
name is configured and available in Cldr and supports
rules based number formats (RBNF).
## Options
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
Expand All @@ -352,7 +352,7 @@ defmodule Cldr do
Returns a boolean indicating if the specified locale
name is configured and available in Gettext.
## Options
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
Expand All @@ -377,7 +377,7 @@ defmodule Cldr do
This is helpful when building a list of `or` expressions
to return the first known locale name from a list.
## Options
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
Expand All @@ -404,7 +404,7 @@ defmodule Cldr do
whether the locale name is configured in `Cldr`
and has RBNF rules defined.
## Options
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
Expand All @@ -431,7 +431,7 @@ defmodule Cldr do
`false` based upon whether the locale name is configured in
`GetText`.
## Options
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
Expand All @@ -458,7 +458,7 @@ defmodule Cldr do
mean the locale is configured for Cldr. See also
`Cldr.known_locale?/1`.
## Options
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct returned by `Cldr.Locale.new!/1`
Expand All @@ -484,7 +484,7 @@ defmodule Cldr do
@doc """
Normalise and validate a locale name.
## Options
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct returned by `Cldr.Locale.new!/1`
Expand Down Expand Up @@ -577,7 +577,7 @@ defmodule Cldr do
@doc """
Normalise and validate a gettext locale name.
## Options
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct returned by `Cldr.Locale.new!/1`
Expand Down Expand Up @@ -631,7 +631,7 @@ defmodule Cldr do
@doc """
Normalise and validate a calendar name.
## Options
## Arguments
* `calendar` is any calendar name returned by `Cldr.known_calendars/0`
Expand Down Expand Up @@ -677,7 +677,7 @@ defmodule Cldr do
@doc """
Returns an error tuple for an invalid calendar.
## Options
## Arguments
* `calendar` is any calendar name **not** returned by `Cldr.known_calendars/0`
Expand Down Expand Up @@ -739,7 +739,7 @@ defmodule Cldr do
@doc """
Normalise and validate a territory code.
## Options
## Arguments
* `territory` is any territory code returned by `Cldr.known_territories/0`
Expand Down Expand Up @@ -803,7 +803,7 @@ defmodule Cldr do
@doc """
Returns an error tuple for an unknown territory.
## Options
## Arguments
* `territory` is any territory code **not** returned by `Cldr.known_territories/0`
Expand Down Expand Up @@ -863,7 +863,7 @@ defmodule Cldr do
@doc """
Normalize and validate a currency code.
## Options
## Arguments
* `currency` is any ISO 4217 currency code as returned by `Cldr.known_currencies/0`
or any valid private use ISO4217 code which is a three-letter alphabetic code that
Expand Down Expand Up @@ -905,22 +905,27 @@ defmodule Cldr do

def validate_currency(currency) when is_atom(currency) do
currency
|> Atom.to_string
|> Atom.to_string()
|> validate_currency
|> case do
{:error, _} -> {:error, unknown_currency_error(currency)}
ok -> ok
end
end

def validate_currency(<< char_1 :: integer-size(8), char_2 :: integer-size(8), char_3 :: integer-size(8) >> = currency)
when Config.is_alphabetic(char_1) and Config.is_alphabetic(char_2) and Config.is_alphabetic(char_3) and
char_1 in [?x, ?X] do
def validate_currency(
<<char_1::integer-size(8), char_2::integer-size(8), char_3::integer-size(8)>> = currency
)
when Config.is_alphabetic(char_1) and Config.is_alphabetic(char_2) and
Config.is_alphabetic(char_3) and char_1 in [?x, ?X] do
{:ok, String.to_atom(String.upcase(currency))}
end

def validate_currency(<< char_1 :: integer-size(8), char_2 :: integer-size(8), char_3 :: integer-size(8) >> = currency)
when Config.is_alphabetic(char_1) and Config.is_alphabetic(char_2) and Config.is_alphabetic(char_3) do
def validate_currency(
<<char_1::integer-size(8), char_2::integer-size(8), char_3::integer-size(8)>> = currency
)
when Config.is_alphabetic(char_1) and Config.is_alphabetic(char_2) and
Config.is_alphabetic(char_3) do
currency_code =
currency
|> String.upcase()
Expand All @@ -943,7 +948,7 @@ defmodule Cldr do
@doc """
Returns an error tuple for an invalid currency.
## Options
## Arguments
* `currency` is any currency code **not** returned by `Cldr.known_currencies/0`
Expand Down Expand Up @@ -987,7 +992,7 @@ defmodule Cldr do
@doc """
Normalize and validate a number system name.
## Options
## Arguments
* `number_system` is any number system name returned by
`Cldr.known_number_systems/0`
Expand All @@ -1013,7 +1018,7 @@ defmodule Cldr do
}
"""
@spec validate_number_system(String.t() | any()) ::
@spec validate_number_system(atom() | binary()) ::
{:ok, String.t()} | {:error, {Exception.t(), String.t()}}

def validate_number_system(number_system)
Expand All @@ -1038,7 +1043,7 @@ defmodule Cldr do
@doc """
Returns an error tuple for an unknown number system.
## Options
## Arguments
* `number_system` is any number system name **not** returned by `Cldr.known_number_systems/0`
Expand Down Expand Up @@ -1081,7 +1086,7 @@ defmodule Cldr do
@doc """
Normalise and validate a number system type.
## Options
## Arguments
* `number_system_type` is any number system type returned by
`Cldr.known_number_system_types/0`
Expand All @@ -1107,7 +1112,7 @@ defmodule Cldr do
}
"""
@spec validate_number_system_type(String.t() | any()) ::
@spec validate_number_system_type(String.t() | atom()) ::
{:ok, String.t()} | {:error, {Exception.t(), String.t()}}

def validate_number_system_type(number_system_type)
Expand Down
27 changes: 13 additions & 14 deletions lib/cldr/config/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,9 @@ defmodule Cldr.Config do
"languages"
]


defmacro is_alphabetic(char) do
quote do
(unquote(char) in ?a..?z or unquote(char) in ?A..?Z)
unquote(char) in ?a..?z or unquote(char) in ?A..?Z
end
end

Expand Down Expand Up @@ -148,7 +147,7 @@ defmodule Cldr.Config do
end
)

@app_name Mix.Project.config[:app]
@app_name Mix.Project.config()[:app]
def app_name do
@app_name
end
Expand All @@ -160,17 +159,17 @@ defmodule Cldr.Config do
message =
if is_nil(@json_lib) do
"""
A json library has not been configured. Please configure one in
your `mix.exs` file. Two common packages are Poison and Jason.
For example in your `mix.exs`:
def deps() do
[
{:jason, "~> 1.0"},
...
]
end
"""
A json library has not been configured. Please configure one in
your `mix.exs` file. Two common packages are Poison and Jason.
For example in your `mix.exs`:
def deps() do
[
{:jason, "~> 1.0"},
...
]
end
"""
else
"""
The json library #{inspect(@json_lib)} is either
Expand Down
5 changes: 4 additions & 1 deletion lib/cldr/gettext/gettext.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ if Mix.env() in [:dev, :test] do
Implements a Gettext-compatible module but using Cldr locales. Its an example
only.
"""
use Gettext, otp_app: Cldr.Config.app_name, priv: "priv/gettext_test", plural_forms: Cldr.Gettext.Plural
use Gettext,
otp_app: Cldr.Config.app_name(),
priv: "priv/gettext_test",
plural_forms: Cldr.Gettext.Plural
end
end
8 changes: 4 additions & 4 deletions lib/cldr/install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,19 @@ defmodule Cldr.Install do
output_file_name
|> File.write!(:erlang.list_to_binary(body))

Logger.info("Downloaded locale #{inspect(locale_name)}")
Logger.bare_log(:info, "Downloaded locale #{inspect(locale_name)}")
{:ok, output_file_name}

{_, {{_version, code, message}, _headers, _body}} ->
Logger.error(
Logger.bare_log(:error,
"Failed to download locale #{inspect(locale_name)} from #{url}. " <>
"HTTP Error: (#{code}) #{inspect(message)}"
)

{:error, code}

{:error, {:failed_connect, [{_, {host, _port}}, {_, _, sys_message}]}} ->
Logger.error(
Logger.bare_log(:error,
"Failed to connect to #{inspect(host)} to download " <>
"locale #{inspect(locale_name)}. Reason: #{inspect(sys_message)}"
)
Expand Down Expand Up @@ -128,7 +128,7 @@ defmodule Cldr.Install do
# Returns the version of ex_cldr
defp app_version do
cond do
spec = Application.spec(Cldr.Config.app_name) ->
spec = Application.spec(Cldr.Config.app_name()) ->
Keyword.get(spec, :vsn) |> :erlang.list_to_binary()

Code.ensure_loaded?(Cldr.Mixfile) ->
Expand Down
4 changes: 2 additions & 2 deletions lib/cldr/language_tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ defmodule Cldr.LanguageTag do
* `{:error, reason}`
"""
@spec parse(Locale.local_name()) ::
@spec parse(Cldr.Locale.locale_name()) ::
{:ok, LanguageTag.t()} | {:error, {Exception.t(), String.t()}}
def parse(locale_name) when is_binary(locale_name) do
Parser.parse(locale_name)
Expand All @@ -215,7 +215,7 @@ defmodule Cldr.LanguageTag do
* raises and exception
"""
@spec parse(Locale.local_name()) :: LanguageTag.t() | none()
@spec parse(Cldr.Locale.locale_name()) :: t() | none()
def parse!(locale_string) when is_binary(locale_string) do
Parser.parse!(locale_string)
end
Expand Down
Loading

0 comments on commit 244a6db

Please sign in to comment.