i18n: pass only the ISO code string to 'translate()'

Don't use the whole Hash everywhere.
Also fall back nicely to english string if no translation exists.
This commit is contained in:
Samantaz Fox
2021-11-08 23:52:55 +01:00
parent 301444563b
commit 139786b9ef
23 changed files with 133 additions and 126 deletions

View File

@@ -22,7 +22,7 @@ def github_details(summary : String, content : String)
return HTML.escape(details)
end
def error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)
def error_template_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception)
if exception.is_a?(InfoException)
return error_template_helper(env, locale, status_code, exception.message || "")
end
@@ -46,7 +46,7 @@ def error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSO
return templated "error"
end
def error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)
def error_template_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String)
env.response.content_type = "text/html"
env.response.status_code = status_code
error_message = translate(locale, message)
@@ -58,7 +58,7 @@ macro error_atom(*args)
error_atom_helper(env, locale, {{*args}})
end
def error_atom_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)
def error_atom_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception)
if exception.is_a?(InfoException)
return error_atom_helper(env, locale, status_code, exception.message || "")
end
@@ -67,7 +67,7 @@ def error_atom_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::A
return "<error>#{exception.inspect_with_backtrace}</error>"
end
def error_atom_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)
def error_atom_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String)
env.response.content_type = "application/atom+xml"
env.response.status_code = status_code
return "<error>#{message}</error>"
@@ -77,7 +77,7 @@ macro error_json(*args)
error_json_helper(env, locale, {{*args}})
end
def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception, additional_fields : Hash(String, Object) | Nil)
def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception, additional_fields : Hash(String, Object) | Nil)
if exception.is_a?(InfoException)
return error_json_helper(env, locale, status_code, exception.message || "", additional_fields)
end
@@ -90,11 +90,11 @@ def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::A
return error_message.to_json
end
def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)
def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception)
return error_json_helper(env, locale, status_code, exception, nil)
end
def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String, additional_fields : Hash(String, Object) | Nil)
def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String, additional_fields : Hash(String, Object) | Nil)
env.response.content_type = "application/json"
env.response.status_code = status_code
error_message = {"error" => message}
@@ -104,11 +104,11 @@ def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::A
return error_message.to_json
end
def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)
def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String)
error_json_helper(env, locale, status_code, message, nil)
end
def error_redirect_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil)
def error_redirect_helper(env : HTTP::Server::Context, locale : String?)
request_path = env.request.path
if request_path.starts_with?("/search") || request_path.starts_with?("/watch") ||

View File

@@ -190,7 +190,7 @@ def create_notification_stream(env, topics, connection_channel)
connection = Channel(PQ::Notification).new(8)
connection_channel.send({true, connection})
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
locale = env.get("preferences").as(Preferences).locale
since = env.params.query["since"]?.try &.to_i?
id = 0

View File

@@ -64,31 +64,36 @@ def load_all_locales
return locales
end
def translate(locale : Hash(String, JSON::Any) | Nil, translation : String, text : String | Nil = nil)
# if locale && !locale[translation]?
# puts "Could not find translation for #{translation.dump}"
# end
def translate(locale : String?, key : String, text : String | Nil = nil) : String
# Raise an eception if "key" doesn't exist in en-US locale
raise "Invalid translation key \"#{key}\"" unless LOCALES["en-US"].has_key?(key)
if locale && locale[translation]?
case locale[translation]
when .as_h?
match_length = 0
# Default to english, whenever the locale doesn't exist,
# or the key requested has not been translated
if locale && LOCALES.has_key?(locale) && LOCALES[locale].has_key?(key)
raw_data = LOCALES[locale][key]
else
raw_data = LOCALES["en-US"][key]
end
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
case raw_data
when .as_h?
# Init
translation = ""
match_length = 0
raw_data.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
when .as_s?
if !locale[translation].as_s.empty?
translation = locale[translation].as_s
end
else
raise "Invalid translation #{translation}"
end
when .as_s?
translation = raw_data.as_s
else
raise "Invalid translation \"#{raw_data}\""
end
if text
@@ -98,7 +103,7 @@ def translate(locale : Hash(String, JSON::Any) | Nil, translation : String, text
return translation
end
def translate_bool(locale : Hash(String, JSON::Any) | Nil, translation : Bool)
def translate_bool(locale : String?, translation : Bool)
case translation
when true
return translate(locale, "Yes")

View File

@@ -64,7 +64,7 @@ struct SearchVideo
end
end
def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)
def to_json(locale : String?, json : JSON::Builder)
json.object do
json.field "type", "video"
json.field "title", self.title
@@ -96,7 +96,7 @@ struct SearchVideo
end
# TODO: remove the locale and follow the crystal convention
def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)
def to_json(locale : String?, _json : Nil)
JSON.build do |json|
to_json(locale, json)
end
@@ -130,7 +130,7 @@ struct SearchPlaylist
property videos : Array(SearchPlaylistVideo)
property thumbnail : String?
def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)
def to_json(locale : String?, json : JSON::Builder)
json.object do
json.field "type", "playlist"
json.field "title", self.title
@@ -161,7 +161,7 @@ struct SearchPlaylist
end
# TODO: remove the locale and follow the crystal convention
def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)
def to_json(locale : String?, _json : Nil)
JSON.build do |json|
to_json(locale, json)
end
@@ -183,7 +183,7 @@ struct SearchChannel
property description_html : String
property auto_generated : Bool
def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)
def to_json(locale : String?, json : JSON::Builder)
json.object do
json.field "type", "channel"
json.field "author", self.author
@@ -214,7 +214,7 @@ struct SearchChannel
end
# TODO: remove the locale and follow the crystal convention
def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)
def to_json(locale : String?, _json : Nil)
JSON.build do |json|
to_json(locale, json)
end
@@ -234,7 +234,7 @@ class Category
property description_html : String
property badges : Array(Tuple(String, String))?
def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)
def to_json(locale : String?, json : JSON::Builder)
json.object do
json.field "type", "category"
json.field "title", self.title
@@ -249,7 +249,7 @@ class Category
end
# TODO: remove the locale and follow the crystal convention
def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)
def to_json(locale : String?, _json : Nil)
JSON.build do |json|
to_json(locale, json)
end