Problems installing ex_cldr_date_and _times package in a phoenix api

Hi everyone,

I have the following config in config.exs

# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
#
# This configuration file is loaded before any dependency and
# is restricted to this project.

# General application configuration
use Mix.Config

config :wolf_cms,
  ecto_repos: [WolfCms.Repo]

# Configures the endpoint
config :wolf_cms, WolfCmsWeb.Endpoint,
  url: [host: "localhost"],
  secret_key_base: "gsKzQFCNGujOPkFu1NH3FbAuPLnjEnmIw5vkZrEy+tS5cwsCW05lEBpeXq7q4d2y",
  render_errors: [view: WolfCmsWeb.ErrorView, accepts: ~w(json)],
  pubsub: [name: WolfCms.PubSub, adapter: Phoenix.PubSub.PG2]

# Configures Elixir's Logger
config :logger, :console,
  format: "$time $metadata[$level] $message\n",
  metadata: [:request_id]

# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

config :ex_cldr,
  default_locale: "en",
  json_library: Jason

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"

I have the following problem after installing:

{:ex_cldr, "~> 2.0"},
{:ex_cldr_dates_times, "~> 2.0"}

The only way I get a result

WolfCms.Cldr.DateTime.to_string DateTime.utc_now, WolfCms.Cldr, format: :short
** (UndefinedFunctionError) function WolfCms.Cldr.DateTime.to_string/3 is undefined (module WolfCms.Cldr.DateTime is not available)
    WolfCms.Cldr.DateTime.to_string(~U[2020-01-25 14:31:16.268454Z], WolfCms.Cldr, [format: :short]

If I use the new method for version 2 for DateTime.to_string/3

iex(1)> Cldr.DateTime.to_string DateTime.utc_now, WolfCms.Cldr, format: :short        
** (UndefinedFunctionError) function WolfCms.Cldr.default_locale/0 is undefined (module WolfCms.Cldr is not available)
    WolfCms.Cldr.default_locale()
    (ex_cldr) lib/cldr.ex:157: Cldr.get_locale/1
    (ex_cldr_dates_times) lib/cldr/datetime.ex:121: Cldr.DateTime.default_options/1
    (ex_cldr_dates_times) lib/cldr/datetime.ex:102: Cldr.DateTime.to_string/3
iex(1)> 

Can someone explain why this is not working properly?

Thanks

I know all your secrets now :slight_smile:

You can generate a new one with

$ mix phx.gen.secret

but it should be kept private.

1 Like

I know but that doesn’t help me now with the problem at hand. But thanks for reminding me.

I have not used ex_cldr yet, but maybe @kip knows.

Wait a minute isn’t @kip the author of the library?

Yes, he is

1 Like

OK, now I know what I need to ask.

How can the ex_cldr module and submodules, be used to support a multilingual CMS?

What is the best configurations options?

Thanks in advance

The hint is in the message:

WolfCms.Cldr.DateTime.to_string DateTime.utc_now, WolfCms.Cldr, format: :short
** (UndefinedFunctionError) function WolfCms.Cldr.DateTime.to_string/3 is undefined (module WolfCms.Cldr.DateTime is not available)
    WolfCms.Cldr.DateTime.to_string(~U[2020-01-25 14:31:16.268454Z], WolfCms.Cldr, [format: :short]

You need to define the backend module WolfCms.Cldr as noted in the readme. A simple module would be:

defmodule WolfCms.Cldr do
  use Cldr,
    default_locale: "en",
    locales: ["fr", "en"],
    providers: [Cldr.Number, Cldr.DateTime]
end

Then you should be good to go.

2 Likes

Thanks for the response @kip, but unfortunately it doesn’t work
.

Error trace:

iex(1)> Cldr.DateTime.to_string DateTime.utc_now, WolfCms.Cldr, format: :short
** (UndefinedFunctionError) function WolfCms.Cldr.Calendar.day_periods/2 is undefined (module WolfCms.Cldr.Calendar is not available)
    WolfCms.Cldr.Calendar.day_periods(%Cldr.LanguageTag{canonical_locale_name: "en-Latn-US", cldr_locale_name: "en", extensions: %{}, gettext_locale_name: nil, language: "en", language_subtags: [], language_variant: nil, locale: %{}, private_use: [], rbnf_locale_name: "en", requested_locale_name: "en", script: "Latn", territory: "US", transform: %{}}, :gregorian)
    (ex_cldr_calendars) lib/cldr/calendar.ex:1824: Cldr.Calendar.localize/6
    (wolf_cms) lib/cldr/backend/formatter.ex:1: WolfCms.Cldr.DateTime.Formatter.format/4
    (ex_cldr_dates_times) lib/cldr/time.ex:108: Cldr.Time.to_string/3 
    (ex_cldr_dates_times) lib/cldr/format/datetime_formatter.ex:196: Cldr.DateTime.Formatter.time/5
    (wolf_cms) lib/cldr/backend/formatter.ex:1: WolfCms.Cldr.DateTime.Formatter.format/4
    (ex_cldr_dates_times) lib/cldr/datetime.ex:108: Cldr.DateTime.to_string/3
iex(1)> 

Also here is the backend.ex file located in /lib/wolfcms/

defmodule WolfCms.Cldr do
  use Cldr,
    default_locale: "en",
    locales: ["fr", "en"],
    providers: [Cldr.Number, Cldr.DateTime]
end

Also I will most probably not use this module because it is to complicated for me. I hope you don’t mind when i am saying this:
But it needs a more clear documentation.

Thanks for the help.

Whoops, Add Cldr.Calendar to the providers list.

2 Likes

The readme tries to make it easy - I18N is a big topic. The following is right after some examples - here in case anyway else gets stuck. PR’s for documentation are always welcome - I care a lot about the docs and I hope it shows.

Configuration and Migration from Version 1

ex_cldr_dates_times uses the configuration set for the dependency ex_cldr . See the documentation for ex_cldr.

A backend module is required that is used to host the functions that manage CLDR data. An example to get started is:

  1. Create a backend module (see ex_cldr for details of the available options). Note the requirement to configure the appropriate Cldr provider backends.
defmodule MyApp.Cldr do 
  use Cldr, locales: ["en", "fr", "ja"], 
  providers: [Cldr.Number, Cldr.Calendar, Cldr.DateTime] 
end
3 Likes

Maybe put this info in the installation section of the docs for better readability @kip?

Good suggestion. Done and republished the docs to hex.

2 Likes