Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translation for schema keys #52

Closed
marcotas opened this issue Feb 13, 2018 · 6 comments · Fixed by #61
Closed

Translation for schema keys #52

marcotas opened this issue Feb 13, 2018 · 6 comments · Fixed by #61
Assignees
Labels
Milestone

Comments

@marcotas
Copy link

Maybe this is a dumb question but I really didn't find it anywhere in the documentation.

How can I translate a Schema field name? Example:

UserValidation = Dry::Validation.Schema do
  configure { config.messages = :i18n }

  required(:age).filled
end

When I want the full messages i got the message translated correctly, but not the field name.
age obrigatório(a). I didn't find the translation key for 'age', which should be 'idade' of the translated name.

Maybe the yml file should have something like this?

pt_BR:
  errors:
    fields:
      age: Idade

Sorry about my english. To be clearer I want to know the translation key to be able to translate the Schema fields. If I have required(:name).filled I want to setup my i18n yml file to have 'name' translated in the full messages validations.

Thanks for everything and congratulations for the excelent lib! 👍

@marcotas marcotas changed the title How to translate the Schema Keys? How to translate the Schema Keys (Fields)? Feb 13, 2018
@cmcolinh
Copy link

I was also wondering whether there was a good way to do this. I have monkey patched myself a solution for now, but would rather have a supported solution if possible.

@glaszig
Copy link

glaszig commented Jan 9, 2019

@cmcolinh would you share that? this is mind-boggling.

@cmcolinh
Copy link

Monkey patch this in (I put in in config/initializers/)

def extract_all(key, h)
  r = []
  h.each do |k, v|
    if v.is_a? Hash
      r += [extract_all("#{key}.#{k}", v)].flatten(1)
    else
      r << [:"#{key}.#{k}", v]
    end
  end
  r
end

module Dry
  module Validation
    class Result
      def validation_errors(locale: :en)
        err = {}
        self.errors(locale: locale).map do |k, v|
          if v.is_a? Hash
            err.merge!(extract_all(k, v).to_h)
          else
            err[k] = v
          end
        end

        err.map do |k, v|
          if v[0].is_a?(String)
            [AppDef[k]&.[](:rails) || k, "#{AppDef[k]&.[](locale) || "#{k} "}#{v[0]}"]
          else
            return_value = []

            v.values.each do |a|
              return_value << "#{AppDef[k]&.[](locale)}#{a[0]}"
            end

            [AppDef[k]&.[](:rails), return_value.join("\r\n")]
          end
        end.to_h
      end
    end
  end
end

Where AppDef was written in YAML with an example of

ball:
  es: "pelota"

I see no reason why the i18n file could'nt be used instead.
Then call using

s.call(params).validation_errors(locale: :es)

It's customized for my specific needs of course(to create newline delimited errors that I can raise as is, it's also flattening the error keys as well, which you may not want), but you can probably change this to fit your needs as well.

@glaszig
Copy link

glaszig commented Jan 10, 2019 via email

@Mistgun
Copy link
Contributor

Mistgun commented Jan 11, 2019

I'm also interested in this. Any inbuild solution to that?

@glaszig
Copy link

glaszig commented Jan 11, 2019 via email

@flash-gordon flash-gordon changed the title How to translate the Schema Keys (Fields)? Translations for schema keys Feb 7, 2019
@flash-gordon flash-gordon changed the title Translations for schema keys Translation for schema keys Feb 7, 2019
@solnic solnic transferred this issue from dry-rb/dry-validation Feb 8, 2019
@solnic solnic added the bug label Feb 16, 2019
@solnic solnic added this to the 1.0.0 milestone Feb 16, 2019
@solnic solnic self-assigned this Feb 17, 2019
solnic added a commit that referenced this issue Feb 18, 2019
  * Use `rule_lookup_path` and move rule name translations to the root.
    Otherwise it would be in conflict with predicate names
  * Fix handling of `locale` in YAML and I18n when fetching rule name

Closes #52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants