2018-12-20 21:32:09 +00:00
|
|
|
def load_locale(name)
|
|
|
|
return JSON.parse(File.read("locales/#{name}.json")).as_h
|
|
|
|
end
|
|
|
|
|
|
|
|
def translate(locale : Hash(String, JSON::Any) | Nil, translation : String, text : String | Nil = nil)
|
2018-12-20 23:41:42 +00:00
|
|
|
# if locale && !locale[translation]?
|
|
|
|
# puts "Could not find translation for #{translation.dump}"
|
2018-12-20 21:32:09 +00:00
|
|
|
# end
|
|
|
|
|
2019-04-28 19:50:17 +00:00
|
|
|
if locale && locale[translation]?
|
|
|
|
case locale[translation]
|
|
|
|
when .as_h?
|
|
|
|
match_length = 0
|
|
|
|
|
|
|
|
locale[translation].as_h.each do |key, value|
|
|
|
|
if md = text.try &.match(/#{key}/)
|
|
|
|
if md[0].size >= match_length
|
|
|
|
translation = value.as_s
|
|
|
|
match_length = md[0].size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when .as_s?
|
|
|
|
if !locale[translation].as_s.empty?
|
|
|
|
translation = locale[translation].as_s
|
|
|
|
end
|
|
|
|
end
|
2018-12-20 21:32:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if text
|
|
|
|
translation = translation.gsub("`x`", text)
|
|
|
|
end
|
|
|
|
|
|
|
|
return translation
|
|
|
|
end
|
2019-04-28 19:56:06 +00:00
|
|
|
|
|
|
|
def translate_bool(locale : Hash(String, JSON::Any) | Nil, translation : Bool)
|
|
|
|
case translation
|
|
|
|
when true
|
|
|
|
return translate(locale, "Yes")
|
|
|
|
when false
|
|
|
|
return translate(locale, "No")
|
|
|
|
end
|
|
|
|
end
|