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

Phrases contain Hashs #155

Open
gmonein opened this issue Mar 11, 2020 · 1 comment
Open

Phrases contain Hashs #155

gmonein opened this issue Mar 11, 2020 · 1 comment

Comments

@gmonein
Copy link

gmonein commented Mar 11, 2020

Hello,

rake tolk:import seen to not properly flatten yml
So some hash are stored as translations

Here is a screen:
image

Raising me errors like

ActionView::Template::Error: translation data {:uno=>"fase", :altro=>"fasi"} can not be used with :count => 1. key 'one' is missing.

Because my users edited hash keys

It seen like the problem come from the flat_hash gem
When i do

Tolk::Locale.flat_hash(parsed_yaml).any? { |_,v| v.is_a? Hash }
=> true

however I fixed it by adding this

Tolk::Locale.class_eval do
  def self.flat_hash(hash)
    res = {}
    stack = [OpenStruct.new(current_path: [], value: hash)]
    while (current = stack.pop)
      if current.value.is_a? Hash
        current.value.each { |k,v| stack << OpenStruct.new(current_path: current.current_path + [k], value: v) }
      else
        res[current.current_path.join('.')] = current.value
      end
    end
    res
  end
end

Into app/config/initializers/tolk.rb

@gmonein
Copy link
Author

gmonein commented Mar 12, 2020

It seen like if my users now forgot a key, neither one or other, I18n is raising errors instead of fallbacking 🎉

Here is another issue about this svenfuchs/rails-i18n#893

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

No branches or pull requests

1 participant