Add support for plurlization to locales

This commit is contained in:
Omar Roth
2019-04-28 14:50:17 -05:00
parent 03891b66b6
commit 6cdb6ec711
3 changed files with 79 additions and 18 deletions

View File

@@ -7,8 +7,24 @@ def translate(locale : Hash(String, JSON::Any) | Nil, translation : String, text
# puts "Could not find translation for #{translation.dump}"
# end
if locale && locale[translation]? && !locale[translation].as_s.empty?
translation = locale[translation].as_s
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
end
if text