3 -- Compability translator code to support MT 0.4, which doesn't support
4 -- translations for mods.
5 -- This adds two dummy or wrapper functions:
6 -- * minetest.translate ← calendar._translate
7 -- * minetest.get_translator ← calendar._get_translator
9 if not minetest
.translate
then
10 -- No translation system available, use dummy functions
11 function calendar
._translate(textdomain
, str
, ...)
12 local arg
= {n
=select('#', ...), ...}
13 return str
:gsub("@(.)", function(matched
)
14 local c
= string.byte(matched
)
15 if string.byte("1") <= c
and c
<= string.byte("9") then
16 return arg
[c
- string.byte("0")]
23 function calendar
._get_translator(textdomain
)
24 return function(str
, ...) return calendar
._translate(textdomain
or "", str
, ...) end
27 -- Translation system available, just user wrapper functions
28 calendar
._translate
= minetest
.translate
29 calendar
._get_translator
= minetest
.get_translator
32 local S
= calendar
._get_translator("calendar")
34 dofile(minetest
.get_modpath("calendar").."/gameconfig.lua")
36 local function update_helper_vars()
37 -- Number of months in a year
38 calendar
.MONTHS
= #calendar
.month_names
39 -- Number of days in a week
40 calendar
.WEEK_DAYS
= #calendar
.weekday_names
41 -- Number of days in a year
42 calendar
.YEAR_DAYS
= calendar
.MONTHS
* calendar
.MONTH_DAYS
48 calendar
.config
= function(config
)
49 if config
.MONTH_DAYS
then
50 calendar
.MONTH_DAYS
= config
.MONTH_DAYS
52 if config
.month_names
then
53 calendar
.month_names
= config
.month_names
55 if config
.month_names_short
then
56 calendar
.month_names_short
= config
.month_names_short
58 if config
.weekday_names
then
59 calendar
.weekday_names
= config
.weekday_names
61 if config
.weekday_names_short
then
62 calendar
.weekday_names_short
= config
.weekday_names_short
64 if config
.FIRST_WEEK_DAY
then
65 calendar
.FIRST_WEEK_DAY
= config
.FIRST_WEEK_DAY
70 calendar
.register_holiday
= function(def
)
71 table.insert(holidays
, def
)
74 calendar
.get_holidays
= function(total_days
)
75 if not total_days
then
76 total_days
= minetest
.get_day_count()
78 local days
, months
, years
= calendar
.get_date(total_days
)
79 local found_holidays
= {}
81 local holiday
= holidays
[h
]
82 if holiday
.type == "monthday" then
83 if holiday
.days
== days
and holiday
.months
== months
and (holiday
.years
== nil or holiday
.years
== years
) then
84 table.insert(found_holidays
, holiday
)
86 elseif holiday
.type == "custom" then
87 if holiday
.is_holiday(total_days
) == true then
88 table.insert(found_holidays
, holiday
)
91 minetest
.log("error", "[calender] Invalid holiday type: "..tostring(holiday
.type))
97 calendar
.get_weekday
= function(total_days
)
98 if not total_days
then
99 total_days
= minetest
.get_day_count()
101 return (total_days
+ calendar
.FIRST_WEEK_DAY
) % calendar
.WEEK_DAYS
104 calendar
.get_date
= function(total_days
, ordinal
)
105 if not total_days
then
106 total_days
= minetest
.get_day_count()
109 local y
= math
.floor(total_days
/ calendar
.YEAR_DAYS
) -- elapsed years in epoch
110 local m
= math
.floor(total_days
% calendar
.YEAR_DAYS
/ calendar
.MONTH_DAYS
) -- elapsed months in year
111 local d
= math
.floor(total_days
% calendar
.YEAR_DAYS
% calendar
.MONTH_DAYS
) -- elapsed days in month
112 local j
= math
.floor(total_days
% calendar
.YEAR_DAYS
) -- elapsed days in year
114 if ordinal
== nil then
126 calendar
.get_total_days_from_date
= function(days
, months
, years
, is_ordinal
)
132 return years
* calendar
.YEAR_DAYS
+ months
* calendar
.MONTH_DAYS
+ days
135 calendar
.get_date_string
= function(format, total_days
)
136 if not total_days
then
137 total_days
= minetest
.get_day_count()
140 if format == nil then
141 format = S("%D days, %M months, %Y years")
144 local y
, m
, d
, j
= calendar
.get_date(total_days
)
145 local w
= calendar
.get_weekday(total_days
)
148 format = string.gsub( format, "%%Y", y
) -- elapsed years in epoch
149 format = string.gsub( format, "%%M", m
) -- elapsed months in year
150 format = string.gsub( format, "%%D", d
) -- elapsed days in month
151 format = string.gsub( format, "%%J", j
) -- elapsed days in year
154 format = string.gsub( format, "%%y", y
+ 1 ) -- current year of epoch
155 format = string.gsub( format, "%%m", m
+ 1 ) -- current month of year
156 format = string.gsub( format, "%%d", d
+ 1 ) -- current day of month
157 format = string.gsub( format, "%%j", j
+ 1 ) -- current day of year
159 format = string.gsub( format, "%%b", S(calendar
.month_names
[ m
+ 1 ]) ) -- current month long name
160 format = string.gsub( format, "%%h", S(calendar
.month_names_short
[ m
+ 1 ]) ) -- current month short name
162 format = string.gsub( format, "%%W", S(calendar
.weekday_names
[ w
+ 1 ]) ) -- current weekday long name
163 format = string.gsub( format, "%%w", S(calendar
.weekday_names_short
[ w
+ 1 ]) ) -- current weekday short name
165 format = string.gsub( format, "%%z", "%%" )
170 dofile(minetest
.get_modpath("calendar").."/gui.lua")