Skip to content

Commit

Permalink
Deprecate :supress_warnings replaced with :suppress_warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Mar 7, 2023
1 parent d763262 commit 0d2dca8
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 37 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Changelog

## Cldr v2.35.0

This is the changelog for Cldr v2.35.0 released on March 7th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr/tags)

** Note that `ex_cldr` version 2.33.0 and later are supported on Elixir 1.11 and later only.**

### Bug Fixes

* Deprecates `:supress_warnings` backend configuration option in favour of the correctly spelt `:suppress_warning`.

### Enhancements

* No longer `IO.warn/2` when `Cldr.Number` and `Cldr.Currency` are both configured as providers. A console note is still issued but it will not raise a compiler warning.

* Add `:default_currency_format` option to a backend configuration. The value must be either `:currency`, `:accounting` or `nil` (the default). This option is used by `ex_cldr_numbers` from version 2.35.0 onwards. If `nil` the format will be derived from the locale.

## Cldr v2.34.2

This is the changelog for Cldr v2.34.2 released on February 25th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr/tags)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ In the backend configuration example above the `:otp_app` key has been defined.
```elixir
# cldr.ex
defmodule MyApp.Cldr do
use Cldr,
use Cldr,
otp_app: :my_app,
default_locale: "en",
gettext: MyApp.Gettext,
Expand Down Expand Up @@ -229,7 +229,7 @@ use Cldr,
* `:add_fallback_locales` is a boolean key which when `true` results in the fallback locales being added for each of the configured locales. The default is `false`. The reason to set this option to `true` is that some data such as rules based number formats and subdivision data are inherited from their language roots. For example, the locale `en-001` is inherited from the locale `en`. Locale `en-001` does not have any rules based number formats or subdivision data defined for it. However locale `en` does. Including the fallback locales maximises the opportunity to resolve localised data.

* `:gettext`: specifies the name of a Gettext module that informs `Cldr` to use that module as an additional source of locales you want to configure. Since `Gettext` uses the Posix locale name format (locales with an '\_' in them) and `Cldr` uses the Unicode format (a '-' as the subtag separator), `Cldr` will transliterate locale names from `Gettext` into the `Cldr` canonical form. For example:

```elixir
use Cldr,
default_locale: "en",
Expand All @@ -251,7 +251,7 @@ use Cldr,

* `:generate_docs` defines whether or not to generate documentation for the modules built as part of the backend. Since these modules represent the public API for `ex_cldr`, the default is `true`. Setting this key to `false` (the atom `false`, not a *falsy* value) which prevent the generation of docs for this backend.

* `:supress_warnings` defines whether warnings are logged when a provider module is configured but not available. It also controls whether warnings are logged when a number format is compiled at runtime. Its purpose is to help identify those formats which might best be added to the `:precompile_number_formats` configuration. The default is `false`. Warning are not logged when set to `true`.
* `:suppress_warnings` defines whether warnings are logged when a provider module is configured but not available. It also controls whether warnings are logged when a number format is compiled at runtime. Its purpose is to help identify those formats which might best be added to the `:precompile_number_formats` configuration. The default is `false`. Warning are not logged when set to `true`.

* `:force_locale_download` determines whether to always download locale files during compilation. Locale data is `ex_cldr` version dependent. When a new version of `ex_cldr` is installed, no locales are installed and therefore locales are downloaded at compilation time as required. This ensures that the right version of the locale data is always associated with the right version of `ex_cldr`. However if locale data is being cached in CI/CD there is some possibility that there can be a version mismatch. Since reproducible builds are important, setting the `force_locale_download: true` in a backend or in global configuration adds additional certainty. The default setting is `false` thereby retaining compatibility with existing behaviour. The configuration can also be made dependent on `mix` environment as shown in this example:

Expand Down
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
* [ ] Add a CLDR version to the generated locale files. Then when configuring a backend, if a locale is detected but its of an earlier version, force download the new version. This will overcome the issue some consumers have whereby the old locales are not removed when `ex_cldr` is updated (and reproducing this has not been possible)

* [ ] Implement a CLDR-locale-aware shim for [ex_phone_number](https://hex.pm/packages/ex_phone_number) to make it easy to integrate phone number parsing and printing

* [X] Add configuration option `:default_currency_format` to be either `:currency`, `:accounting` or `nil`. This is used by `ex_cldr_number`
80 changes: 56 additions & 24 deletions lib/cldr/config/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ defmodule Cldr.Config do
precompile_transliterations: [],
precompile_date_time_formats: [],
precompile_interval_formats: [],
default_currency_format: nil,
otp_app: nil,
generate_docs: true,
supress_warnings: false,
suppress_warnings: false,
message_formats: %{},
force_locale_download: false

Expand All @@ -40,10 +41,11 @@ defmodule Cldr.Config do
precompile_transliterations: [{atom(), atom()}, ...],
precompile_date_time_formats: [String.t(), ...],
precompile_interval_formats: [String.t(), ...],
default_currency_format: :currency | :accounting | nil,
otp_app: atom() | nil,
providers: [atom(), ...],
generate_docs: boolean(),
supress_warnings: boolean(),
suppress_warnings: boolean(),
message_formats: map(),
force_locale_download: boolean
}
Expand Down Expand Up @@ -2361,9 +2363,10 @@ defmodule Cldr.Config do
]
end

@doc false
# Since this uses Mix it is only valid at compile
# time
# time.

@doc false
def config_from_opts(module_config) do
config =
global_config()
Expand All @@ -2375,14 +2378,32 @@ defmodule Cldr.Config do
config
|> Map.put(:default_locale, default_locale_name(config))
|> Map.put(:data_dir, client_data_dir(config))
|> Map.put(:default_currency_format, nil)
|> fix_suppress_warnings_typo()
|> merge_locales_with_default()
|> remove_gettext_only_locales()
|> sort_locales()
|> dedup_provider_modules()
|> validate_default_currency_format()

struct(__MODULE__, config)
end

defp fix_suppress_warnings_typo(%{supress_warnings: suppress} = config) do
note(
"The option :supress_warnings has been deprecated and replaced with :suppress_warnings",
config
)

config
|> Map.delete(:supress_warnings)
|> Map.put(:suppress_warnings, suppress)
end

defp fix_suppress_warnings_typo(config) do
config
end

defp sort_locales(%{locales: :all} = config) do
config
end
Expand Down Expand Up @@ -2436,36 +2457,25 @@ defmodule Cldr.Config do
config
end

@doc false
def note(text, config) do
if config[:supress_warnings] do
[IO.ANSI.yellow(), "note: ", IO.ANSI.reset(), text]
|> :erlang.iolist_to_binary()
|> IO.puts
else
:ok
end
end

@doc false
def dedup_provider_modules(%{providers: []} = config) do
config
end

def dedup_provider_modules(%{providers: providers, backend: backend} = config) do
groups = Enum.group_by(providers, & &1) |> maybe_remove_currency_provider()
groups = Enum.group_by(providers, & &1) |> maybe_remove_currency_provider(config)
config = Map.put(config, :providers, Map.keys(groups))

duplicates =
groups
|> Enum.filter(fn {_k, v} -> length(v) > 1 end)
|> Enum.map(&elem(&1, 0))

if length(duplicates) > 0 && !config[:surpress_warnings] do
IO.warn(
if length(duplicates) > 0 && !config[:suppress_warnings] do
note(
"Duplicate Cldr backend providers #{inspect(providers)} for " <>
"backend #{inspect(backend)} have been ignored",
[]
config
)
end

Expand All @@ -2476,11 +2486,11 @@ defmodule Cldr.Config do
config
end

defp maybe_remove_currency_provider(providers) do
defp maybe_remove_currency_provider(providers, config) do
if Map.has_key?(providers, Cldr.Number) and Map.has_key?(providers, Cldr.Currency) do
IO.warn(
note(
"The provider Cldr.Currency is redundant when Cldr.Number is configured. Please remove " <>
"Cldr.Currency from your CLDR backend provider configuration.", []
"Cldr.Currency from your CLDR backend provider configuration.", config
)

Map.delete(providers, Cldr.Currency)
Expand All @@ -2489,6 +2499,28 @@ defmodule Cldr.Config do
end
end

defp validate_default_currency_format(%{default_currency_format: format} = options)
when format in [:currency, :accounting, nil] do
options
end

defp validate_default_currency_format(%{default_currency_format: format}) do
raise ArgumentError,
"Invalid :default_currency_format option specified.\n" <>
"Valid options are :currency, :accounting or nil. Found #{inspect format}"
end

@doc false
def note(text, config) do
if !config[:suppress_warnings] do
[IO.ANSI.yellow(), "note: ", IO.ANSI.reset(), text]
|> :erlang.iolist_to_binary()
|> IO.puts
else
:ok
end
end

# Returns the AST of any configured plugins
@doc false
def define_provider_modules(config) do
Expand All @@ -2501,7 +2533,7 @@ defmodule Cldr.Config do
end
end

defp log_provider_warning(module, function, args, %{supress_warnings: false} = config) do
defp log_provider_warning(module, function, args, %{suppress_warnings: false} = config) do
require Logger

cond do
Expand Down Expand Up @@ -2577,7 +2609,7 @@ defmodule Cldr.Config do
|> Keyword.delete(:_default_locale)
|> Enum.map(&elem(&1, 0))

if length(remaining_config) > 0 && !remaining_config[:supress_warnings] do
if length(remaining_config) > 0 && !remaining_config[:suppress_warnings] do
IO.warn(
"Using the global configuration is deprecated. Global configuration " <>
"only supports the #{inspect(@non_deprecated_keys)} keys. The keys " <>
Expand Down
2 changes: 1 addition & 1 deletion lib/cldr/config/dependents.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule Cldr.Config.Dependents do
# For compatibility with the releases that don't configure
# Reconsider this.
def cldr_provider_modules(%Cldr.Config{providers: nil, backend: backend} = config) do
if !config.supress_warnings do
if !config.suppress_warnings do
require Logger

Logger.warning(
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Cldr.Mixfile do
use Mix.Project

@version "2.34.2"
@version "2.35.0"

def project do
[
Expand Down
31 changes: 23 additions & 8 deletions test/config_locales_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,32 @@ defmodule Cldr.Config.Test do
end)
end)

assert Cldr.Config.Test.PosixDefaultLocale.known_locale_names() == [:en, :"en-GB"]
assert Cldr.Config.Test.PosixDefaultLocale.default_locale().cldr_locale_name == :"en-GB"
assert apply(Cldr.Config.Test.PosixDefaultLocale, :known_locale_names, []) ==
[:en, :"en-GB"]

assert apply(Cldr.Config.Test.PosixDefaultLocale, :default_locale, []).cldr_locale_name ==
:"en-GB"
end

test "that redundant Cldr.Currency is warned if Cldr.Number is configured" do
assert capture_io(:stderr, fn ->
capture_io(fn ->
defmodule ConfigRedundant do
use Cldr, default_locale: "en", providers: [Cldr.Number, Cldr.Currency]
end
end)
assert capture_io(fn ->
defmodule ConfigRedundant do
use Cldr,
default_locale: "en",
providers: [Cldr.Number, Cldr.Currency],
suppress_warnings: false
end
end) =~ "The provider Cldr.Currency is redundant"
end

test "that :supress warnings is deprecated" do
assert capture_io(fn ->
defmodule SupressWarnings do
use Cldr,
default_locale: "en",
providers: [Cldr.Number, Cldr.Currency],
supress_warnings: false
end
end) =~ "The option :supress_warnings has been deprecated and replaced with :suppress_warnings"
end
end

0 comments on commit 0d2dca8

Please sign in to comment.