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?

Last Post!

kip

kip

ex_cldr Core Team

I will confirm that the Cldr.Chars protocol implementations all return a "" if the argument or result is nil to conform with String.Chars upon which it is based.

Apart from that, I agree with @LostKobrakai that errors should not be swallowed and therefore I don’t think a change to the current APIs is warranted.

I do care about making apps localisable as easier as possible - its definitely a goal of the libraries - so I appreciate understanding your experience and requirements and I’ll definitely think more on how this might be improved.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
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 31494 112
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement