Created
January 27, 2017 13:15
-
-
Save fidelisrafael/e5fce97235d860501b6351394d82c845 to your computer and use it in GitHub Desktop.
Sequel ORM validation error messages Internalization(I18n)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"en": | |
"sequel": | |
"validation_errors": | |
"exact_length": "is not %{exact} characters" | |
"format": "is invalid" | |
"includes": "is not in range or set: %{set}" | |
"integer": "is not a number" | |
"length_range": "is too short or too long. Range: %{range}" | |
"max_length": "is longer than %{max} characters" | |
"max_length_nil": "is not present" | |
"min_length": "is shorter than %{min} characters" | |
"not_null": "is not present" | |
"numeric": "is not a number" | |
"operator": "is not %{operator} %{rhs}" | |
"type": is not a valid %{type}" | |
"or_join_clause": " or " | |
"presence": "is not present" | |
"unique": 'is already taken' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sequel' | |
Sequel::Model.plugin(:validation_helpers) # load namespace | |
module Sequel | |
module Plugins | |
module ValidationHelpers | |
module I18nHelpers | |
def default_options_errors_namespace | |
'sequel.validation_errors' | |
end | |
def translate_error_default_option(key, data = {}) | |
::I18n.t("#{default_options_errors_namespace}.#{key}", data) | |
end | |
alias :i18n_error :translate_error_default_option | |
end | |
extend I18nHelpers | |
# Default validation options used by Sequel. Can be modified to change the error | |
# messages for all models (e.g. for internationalization), or to set certain | |
# default options for validations (e.g. :allow_nil=>true for all validates_format). | |
DEFAULT_OPTIONS = { | |
:exact_length=> { | |
:message=> lambda {|exact| i18n_error(:exact_length, exact: exact) } | |
}, | |
:format => { | |
:message=> lambda {|with| i18n_error(:format) } | |
}, | |
:includes => { | |
:message=> lambda {|set| i18n_error(:includes, set: set.inspect) } | |
}, | |
:integer => { | |
:message=> lambda { i18n_error(:integer) } | |
}, | |
:length_range => { | |
:message=> lambda {|range| i18n_error(:length_range, range: range) } | |
}, | |
:max_length => { | |
:message=> lambda {|max| i18n_error(:max_length, max: max) }, | |
:nil_message=> lambda { i18n_error(:max_length_nil) } | |
}, | |
:min_length => { | |
:message=> lambda {|min| i18n_error(:min_length) } | |
}, | |
:not_null => { | |
:message=> lambda { i18n_error(:min_length) } | |
}, | |
:numeric => { | |
:message => lambda { i18n_error(:numeric) } | |
}, | |
:operator => { | |
:message => lambda {|operator, rhs| | |
i18n_error(:numeric, operator: operator, rhs: rhs) | |
} | |
}, | |
:type => { | |
:message => lambda {|klass| | |
type = klass.is_a?(Array) ? | |
klass.join(i18n_error(:or_join_clause)) : | |
klass.to_s | |
i18n_error(:type, type: type.downcase) | |
}}, | |
:presence => { | |
:message => lambda { i18n_error(:presence) } | |
}, | |
:unique => { | |
:message => lambda { i18n_error(:unique) } | |
} | |
} | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"pt-BR": | |
"sequel": | |
"validation_errors": | |
"exact_length": "Não possui exatamente %{exact} caracteres" | |
"format": "O formato é inválido" | |
"includes": "Não está incluido na lista: %{set}" | |
"integer": "Não é número inteiro válido" | |
"length_range": "Muito curto ou muito longo. Range: %{range}" | |
"max_length": "Possui mais que %{max} caracteres" | |
"max_length_nil": "Está vazio" | |
"min_length": "Possui menos que %{min} caracteres" | |
"not_null": "Está vazio" | |
"numeric": "Não é um numero válido" | |
"operator": "Não é um operador do tipo %{operator} %{rhs}" | |
"type": "Não é do tipo: %{type}" | |
"or_join_clause": " ou " | |
"presence": "Possui um valor vazio" | |
"unique": "Já está sendo utilizado" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment