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

How to get list of available accessors? #356

Closed
alec-c4 opened this issue Dec 22, 2019 · 5 comments
Closed

How to get list of available accessors? #356

alec-c4 opened this issue Dec 22, 2019 · 5 comments
Labels

Comments

@alec-c4
Copy link

alec-c4 commented Dec 22, 2019

Hi!
Is there any method to get list of all available translation accessors, something like globalize_attribute_names from globalize gem?

@alec-c4 alec-c4 changed the title How to get list of available accessors How to get list of available accessors? Dec 22, 2019
@alec-c4
Copy link
Author

alec-c4 commented Dec 22, 2019

of course I can use something like

I18n.available_locales.each {|l| "name_#{l.to_s}"}

or

I18n.available_locales.each {|l| ClassName.mobility_attributes.each { |a| puts [a.to_s,l.to_s].join("_")}}

but it look ugly :(

@shioyama
Copy link
Owner

Unfortunately there is no clean way to do this right now. The accessors are defined here:

def #{name}_#{normalized_locale}(options = {})

There's nothing actually tracking their names once the methods have been defined.

@mKontakis
Copy link

That is something that I extensively use, I would love to see that implemented

@shioyama
Copy link
Owner

shioyama commented Dec 12, 2020

Hmm I'm not sure. You can do this with something like:

module I18nLocaleAccessors
  def locale_accessor_names
    mobility_attributes.flat_map do |name|
      Mobility.available_locales.map do |locale|
        "#{name}_#{Mobility.normalize_locale(locale)}"
      end
    end
  end
end

Then just extend I18nLocaleAccessors from your model classes, or to make it global just:

class ApplicationRecord < ActiveRecord::Base
  # ...
  extend I18nLocaleAccessors
end

Since it's quite simple to implement, I'm not really inclined to add it to the plugin unless there are others asking for it.

That said, the way of calculating the names above is a bit inefficient... 🤔

@Splines
Copy link

Splines commented Apr 24, 2024

unless there are others asking for it.

We use it too at MaMpf and came here migrating from globalize & globalize-accessors. Thanks for your suggested solution, shioyama.

If anyone wonders how this change could look like in production-code, here are the changes I just made to our codebase.

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

No branches or pull requests

4 participants