Localization of attribute names #1773
-
Not too long ago, we added the I think this can be implemented easily by changing the following method to look in the i18n backend first: https://github.com/luckyframework/avram/blob/main/src/lucky/ext/avram/errors.cr#L17-L19 To: module Avram
class InvalidOperationError < AvramError
# ...
def renderable_details : String
"#{localized_invalid_attribute_name} #{validation_messages.first}"
end
private def localized_invalid_attribute_name
Avram.settings.i18n_backend.get?("attribute_#{invalid_attribute_name}") ||
invalid_attribute_name
end
# ....
end
end Note that There can be a naming convention for attributes translations, something like: {
# ...
validate_required: "is vereist",
validate_uniqueness_of: "bestaat al",
attribute_email: "e-mail",
attribute_password: "wachtwoord",
} We could even take it further by allowing operation-specific attribute translations. The only change needed would be storing the https://github.com/luckyframework/avram/blob/main/src/avram/errors.cr#L62 module Avram
class InvalidOperationError < AvramError
getter errors : Hash(Symbol, Array(String))
private getter operation : Avram::Operation
def initialize(@operation)
#...
end
end
end Then we could: module Avram
class InvalidOperationError < AvramError
# ...
def renderable_details : String
"#{localized_invalid_attribute_name} #{validation_messages.first}"
end
private def localized_invalid_attribute_name
Avram.settings.i18n_backend.get?("attribute_#{operation.class.name.underscore}_#{name}") ||
Avram.settings.i18n_backend.get?("attribute_#{name}") ||
invalid_attribute_name
end
# ...
end
end That would be the cherry on the cake. 🙂 What do you guys think? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
oh interesting. I guess I didn't consider the actual attribute names, but that makes sense. Het is een goed idee! |
Beta Was this translation helpful? Give feedback.
-
I'm testing this in a few projects to iron out any possible issues. Once I feel comfortable enough with it, I'll create a PR. |
Beta Was this translation helpful? Give feedback.
oh interesting. I guess I didn't consider the actual attribute names, but that makes sense. Het is een goed idee!