railsmechanic

railsmechanic

Localization: how to use ex_cldr (correctly) in phoenix templates?

Hi all!

I’m currently working on a phoenix web application which should be available in different languages. Therefore I need to localize e.g. numbers, datetimes etc. I’ve correctly set up ex_cldr, but I wonder how to use it correctly in phoenix templates? The default Cldr.to_string/1 function works for numbers but I need to use different formats for datetimes throughout the application. As the Cldr.to_string/1 function cannot take further parameters (e.g. a keyword list), I think the only option is to use one of the Cldr.Datetime.to_string functions, but the returned data is an ok/error tuple {:ok, "formated_string"} | {:error, ...}. Using the “bang function” is not always possible, because some datetime fields in the schema are allowed to be nil.

So, do I need to write some kind of “wrapper function(s)” on my own? In Rails, when the type of data is not the expected one, either nil or a string with the unformatted data is returned. What is the best practice here?

I’ve written the following wrapper function, but is there a default way to work with ex_cldr in phoenix?

def l(formatter, value, options \\ []) when is_atom(formatter) do
    case apply(formatter, :to_string, [value, options]) do
      {:ok, formatted_value} -> formatted_value
      _formatting_failed -> Keyword.get(options, :default, "")
    end
  end

which is called in a template with e.g.

<%= l(Cldr.DateTime, schema.my_datetime, format: "dd.MM.YYYY - HH:mm") %>
# => 03.10.2022 - 09:32

Many thanks!

First Post!

kip

kip

ex_cldr Core Team

@railsmechanic I’m definitely open to suggestions.

I thought about this a lot when I implemented the Cldr.Chars protocol. There are many common options for formatting numbers, dates, units and so on - but not always the case. Given that the contract for Cldr.to_string/1 is return a string its likely the right approach (things like templates were part of the motivation for it). I’m just not sure what to do if an error is returned in the underlying protocol implementation. Just return ""? Return "" and log an error?

What do you think would work best?

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

We're in Beta

About us Mission Statement