From 3c50792d94bfeaaa1535876cc521290556547f57 Mon Sep 17 00:00:00 2001 From: Kelvin Stinghen Date: Fri, 7 Jun 2024 11:33:02 -0300 Subject: [PATCH] Adding Romania locale (#52) * Add Romania to the list of locales * Fixing the `mix holidefs.download` task It was actually a problem in the compilation process. As we were raising an exception when there was no file for a given locale, which is the case when we just added a new locale to the list and we're trying to use `mix holidefs.download` to download that file. * Downloading the Romania holiday definitions file * Logging when a definition file is not present * Translating Romania's holidays --- config/test.exs | 1 + lib/holidefs.ex | 1 + lib/holidefs/definition.ex | 44 +++++--- lib/holidefs/definition/store.ex | 2 +- priv/calendars/definitions/ro.yaml | 174 +++++++++++++++++++++++++++++ priv/gettext/en/LC_MESSAGES/ro.po | 42 +++++++ priv/gettext/ro.pot | 36 ++++++ test/holidefs_test.exs | 1 + 8 files changed, 284 insertions(+), 17 deletions(-) create mode 100644 priv/calendars/definitions/ro.yaml create mode 100644 priv/gettext/en/LC_MESSAGES/ro.po create mode 100644 priv/gettext/ro.pot diff --git a/config/test.exs b/config/test.exs index 1509595..a8af250 100644 --- a/config/test.exs +++ b/config/test.exs @@ -36,6 +36,7 @@ config :holidefs, :ph, :pl, :pt, + :ro, :rs, :ru, :se, diff --git a/lib/holidefs.ex b/lib/holidefs.ex index 0f5b35d..97364df 100644 --- a/lib/holidefs.ex +++ b/lib/holidefs.ex @@ -45,6 +45,7 @@ defmodule Holidefs do ph: "Philippines", pl: "Poland", pt: "Portugal", + ro: "Romania", rs: "Serbia", ru: "Russia", se: "Sweden", diff --git a/lib/holidefs/definition.ex b/lib/holidefs/definition.ex index 61319ad..1ffcb73 100644 --- a/lib/holidefs/definition.ex +++ b/lib/holidefs/definition.ex @@ -7,6 +7,8 @@ defmodule Holidefs.Definition do alias Holidefs.Definition alias Holidefs.Definition.Rule + require Logger + defstruct [:code, :name, rules: []] @type t :: %Definition{ @@ -32,28 +34,38 @@ defmodule Holidefs.Definition do @doc """ Loads the definition for a locale code and name. + If the file does not exists, it returns `nil`. + If any definition rule is invalid, a `RuntimeError` will be raised """ @spec load!(atom, String.t()) :: t def load!(code, name) do - {:ok, file_data} = - code - |> file_path() - |> to_charlist() - |> YamlElixir.read_from_file() + case read_file(code) do + {:ok, file_data} -> + rules = + file_data + |> Map.get("months") + |> Enum.flat_map(fn {month, rules} -> + for rule <- rules, do: Rule.build(code, month, rule) + end) - rules = - file_data - |> Map.get("months") - |> Enum.flat_map(fn {month, rules} -> - for rule <- rules, do: Rule.build(code, month, rule) - end) + %Definition{ + code: code, + name: name, + rules: rules + } + + {:error, _} -> + Logger.warn("Definition file for #{code} not found.") + nil + end + end - %Definition{ - code: code, - name: name, - rules: rules - } + defp read_file(code) do + code + |> file_path() + |> to_charlist() + |> YamlElixir.read_from_file() end @doc """ diff --git a/lib/holidefs/definition/store.ex b/lib/holidefs/definition/store.ex index 83d912f..3550503 100644 --- a/lib/holidefs/definition/store.ex +++ b/lib/holidefs/definition/store.ex @@ -5,7 +5,7 @@ defmodule Holidefs.Definition.Store do alias Holidefs.Definition - definitions = for {c, n} <- Holidefs.locales(), do: Definition.load!(c, n) + definitions = Holidefs.locales() |> Enum.map(fn {c, n} -> Definition.load!(c, n) end) |> Enum.reject(&is_nil/1) @doc """ Returns all the loaded definitions with their rules. diff --git a/priv/calendars/definitions/ro.yaml b/priv/calendars/definitions/ro.yaml new file mode 100644 index 0000000..6053a4e --- /dev/null +++ b/priv/calendars/definitions/ro.yaml @@ -0,0 +1,174 @@ +# Romanian holiday definitions for the Ruby Holiday gem. +# +# Created: 2013-07-23 by Mihai Târnovan +# Sources: +# - http://ro.wikipedia.org/wiki/S%C4%83rb%C4%83tori_publice_%C3%AEn_Rom%C3%A2nia +# - http://en.wikipedia.org/wiki/Public_holidays_in_Romania +--- +months: + 0: + - name: Paștele - duminică + regions: [ro] + function: orthodox_easter(year) + - name: Paștele - luni + regions: [ro] + function: orthodox_easter(year) + function_modifier: 1 + - name: Rusaliile - 50 + regions: [ro] + function: orthodox_easter(year) + function_modifier: 49 + - name: Rusaliile - 51 + regions: [ro] + function: orthodox_easter(year) + function_modifier: 50 + 1: + - name: Anul nou + regions: [ro] + mday: 1 + - name: Anul nou + regions: [ro] + mday: 2 + 5: + - name: Ziua muncii + regions: [ro] + mday: 1 + 8: + - name: Adormirea Maicii Domnului + regions: [ro] + mday: 15 + 11: + - name: Sfântul Apostol Andrei + regions: [ro] + mday: 30 + 12: + - name: Ziua Națională + regions: [ro] + mday: 1 + - name: Sărbătoarea Nașterii Domnului + regions: [ro] + mday: 25 + - name: Sărbătoarea Nașterii Domnului + regions: [ro] + mday: 26 + +tests: + - given: + date: '1961-04-09' + regions: ["ro"] + options: ["informal"] + expect: + name: "Paștele - duminică" + - given: + date: '1961-04-10' + regions: ["ro"] + options: ["informal"] + expect: + name: "Paștele - luni" + - given: + date: '2013-05-05' + regions: ["ro"] + options: ["informal"] + expect: + name: "Paștele - duminică" + - given: + date: '2013-05-06' + regions: ["ro"] + options: ["informal"] + expect: + name: "Paștele - luni" + - given: + date: '2027-05-02' + regions: ["ro"] + options: ["informal"] + expect: + name: "Paștele - duminică" + - given: + date: '2027-05-03' + regions: ["ro"] + options: ["informal"] + expect: + name: "Paștele - luni" + - given: + date: '2012-06-04' + regions: ["ro"] + options: ["informal"] + expect: + name: "Rusaliile - 51" + - given: + date: '2013-06-23' + regions: ["ro"] + options: ["informal"] + expect: + name: "Rusaliile - 50" + - given: + date: '2013-06-24' + regions: ["ro"] + options: ["informal"] + expect: + name: "Rusaliile - 51" + - given: + date: '1986-06-22' + regions: ["ro"] + options: ["informal"] + expect: + name: "Rusaliile - 50" + - given: + date: '1986-06-23' + regions: ["ro"] + options: ["informal"] + expect: + name: "Rusaliile - 51" + - given: + date: '2009-01-01' + regions: ["ro"] + options: ["informal"] + expect: + name: "Anul nou" + - given: + date: '2014-01-02' + regions: ["ro"] + options: ["informal"] + expect: + name: "Anul nou" + - given: + date: '2009-05-01' + regions: ["ro"] + options: ["informal"] + expect: + name: "Ziua muncii" + - given: + date: '2012-08-15' + regions: ["ro"] + options: ["informal"] + expect: + name: "Adormirea Maicii Domnului" + - given: + date: '2013-11-30' + regions: ["ro"] + options: ["informal"] + expect: + name: "Sfântul Apostol Andrei" + - given: + date: '2013-12-01' + regions: ["ro"] + options: ["informal"] + expect: + name: "Ziua Națională" + - given: + date: '2013-12-25' + regions: ["ro"] + options: ["informal"] + expect: + name: "Sărbătoarea Nașterii Domnului" + - given: + date: '2013-12-26' + regions: ["ro"] + options: ["informal"] + expect: + name: "Sărbătoarea Nașterii Domnului" + - given: + date: '2013-07-23' + regions: ["ro"] + expect: + holiday: false diff --git a/priv/gettext/en/LC_MESSAGES/ro.po b/priv/gettext/en/LC_MESSAGES/ro.po new file mode 100644 index 0000000..492e791 --- /dev/null +++ b/priv/gettext/en/LC_MESSAGES/ro.po @@ -0,0 +1,42 @@ +## "msgid"s in this file come from POT (.pot) files. +### +### Do not add, change, or remove "msgid"s manually here as +### they're tied to the ones in the corresponding POT file +### (with the same domain). +### +### Use "mix gettext.extract --merge" or "mix gettext.merge" +### to merge POT files into PO files. +msgid "" +msgstr "" +"Language: en\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +msgid "Paștele - duminică" +msgstr "Easter - Sunday" + +msgid "Paștele - luni" +msgstr "Easter - Monday" + +msgid "Rusaliile - 50" +msgstr "Pentecost - 50" + +msgid "Rusaliile - 51" +msgstr "Pentecost - 51" + +msgid "Anul nou" +msgstr "New Year" + +msgid "Ziua muncii" +msgstr "Labor Day" + +msgid "Adormirea Maicii Domnului" +msgstr "Assumption of the Virgin Mary" + +msgid "Sfântul Apostol Andrei" +msgstr "Saint Andrew the Apostle" + +msgid "Ziua Națională" +msgstr "National Day" + +msgid "Sărbătoarea Nașterii Domnului" +msgstr "The Feast of the Nativity" diff --git a/priv/gettext/ro.pot b/priv/gettext/ro.pot new file mode 100644 index 0000000..d1164a9 --- /dev/null +++ b/priv/gettext/ro.pot @@ -0,0 +1,36 @@ +# Generated by `mix holidefs.gettext.extract` +# for the locale RO - Romania +msgid "" +msgstr "" + + +msgid "Paștele - duminică" +msgstr "" + +msgid "Paștele - luni" +msgstr "" + +msgid "Rusaliile - 50" +msgstr "" + +msgid "Rusaliile - 51" +msgstr "" + +msgid "Anul nou" +msgstr "" + +msgid "Ziua muncii" +msgstr "" + +msgid "Adormirea Maicii Domnului" +msgstr "" + +msgid "Sfântul Apostol Andrei" +msgstr "" + +msgid "Ziua Națională" +msgstr "" + +msgid "Sărbătoarea Nașterii Domnului" +msgstr "" + diff --git a/test/holidefs_test.exs b/test/holidefs_test.exs index c6e6afe..10155c7 100644 --- a/test/holidefs_test.exs +++ b/test/holidefs_test.exs @@ -42,6 +42,7 @@ defmodule HolidefsTest do ph: "Philippines", pl: "Poland", pt: "Portugal", + ro: "Romania", rs: "Serbia", ru: "Russia", se: "Sweden",