Procházet zdrojové kódy

Add date localization

theenglishway (time) před 2 roky
rodič
revize
15301ab783
2 změnil soubory, kde provedl 28 přidání a 3 odebrání
  1. 26 1
      lib/views/date.ex
  2. 2 2
      lib/views/work.ex

+ 26 - 1
lib/views/date.ex

@@ -6,7 +6,32 @@ defmodule CvGenView.Date do
 
   def date(assigns) do
     ~H"""
-    <time datetime={@date} {@rest}><%= @date %></time>
+    <time datetime={@date} {@rest}><%= localized(@date) %></time>
     """
   end
+
+  defp localized(nil), do: "Aujourd'hui"
+
+  defp localized(date) when is_binary(date) do
+    case String.split(date, "-") do
+      [year, month] ->
+        {year, month} = {String.to_integer(year), String.to_integer(month)}
+        Date.new!(year, month, 1) |> localized()
+
+      [year] ->
+        year
+    end
+  end
+
+  defp localized(date) when is_struct(date, Date),
+    do:
+      Calendar.strftime(
+        date,
+        "%B %Y",
+        month_names: fn month ->
+          {"Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre",
+           "Octobre", "Novembre", "Décembre"}
+          |> elem(month - 1)
+        end
+      )
 end

+ 2 - 2
lib/views/work.ex

@@ -1,6 +1,6 @@
 defmodule CvGenView.Work do
   use Phoenix.Component
-  import CvGenView.Date, only: [date: 1]
+  alias CvGenView.Date
 
   attr(:name, :string, required: true)
   attr(:start_date, :any, required: true)
@@ -16,7 +16,7 @@ defmodule CvGenView.Work do
         <h1><%= @name %></h1>
         <span class="location"><%= @location %></span>
         <div class="period">
-          <%= @start_date %> - <%= @end_date || "aujourd'hui" %>
+          <Date.date date={@start_date} /> - <Date.date date={@end_date} />
         </div>
       </div>