scarfacedeb / rails_admin_globalize_field

Tabbed interface and custom field type for globalize translations for Rails_admin
MIT License
50 stars 60 forks source link

How to show the name of the language in a tab? #33

Closed felix91gr closed 4 years ago

felix91gr commented 4 years ago

Sorry for bothering you again, but how would I change the names of the locales shown in the tabs from:

en, sq, sr

To their names in the current locale? Something like this:

English, Albanian, Serbian (when in English)

Engleski, Albanski, Srpski (when in Serbian)

and so on?

scarfacedeb commented 4 years ago

@felix91gr I just released new version 1.1.0 with Tab#label method that you could override.

One of the ways to get what you want:

class RailsAdminGlobalizeField::Tab
  def label
    I18n.translate("locale_name", locale: locale)
  end
end
en:
  locale_name: "English"
de:
  locale_name: "German"
ru:
  locale_name: "Русский"
felix91gr commented 4 years ago

Omg, thank you!

Question: where do I create that class? The application_helper.rb should suffice, right?

Edit: apparently, yes!!

scarfacedeb commented 4 years ago

Well, it'll work, but it's not the right place for that. ApplicationHelper is better suited for view-specific global helper functions that you're going to use in views.

I'd put it in lib/rails_admin_globalize_field/tab.rb or lib/gem_ext/rails_admin_globalize_field.rb.

felix91gr commented 4 years ago

Oh, I see, that makes sense.

I must be doing something wrong though, because I tried that and now the tabs show their old names (sq, sr, en):

image

(I tried one and then the other, that's why I have both files ^^)

scarfacedeb commented 4 years ago

Most likely, you need to require that file somewhere to apply the monkey patching. (e.g. in config/application.rb).

felix91gr commented 4 years ago

I'm trying this and it's working.

Basically, I'm doing the monkey patching like so:

  1. Create a new initializer with an apt name (config/initializers/rails_admin_globalize_fields.rb)
  2. In that initializer, override the class method
  3. Restart the app

This is the code of the initializer:

RailsAdminGlobalizeField::Tab.class_eval do
    def label
        I18n.translate("locale_name.#{locale}")
    end
end

Am I doing it right?

scarfacedeb commented 4 years ago

That'll work too. Putting it in the initializer is a good move too.

Due to the open nature of ruby classes, you don't actually need to use class_eval to override the method. Instead of it you could just reopen existing class and modify it in place. That's what I did in the first comment. But your solution is fine too. It comes down to personal taste.

scarfacedeb commented 4 years ago

And to be clear, 99% of time it's a bad idea to modify existing classes from the code that you don't own. But in this case I give you the official blessing to mess with it :)

scarfacedeb commented 4 years ago

@felix91gr I had some time to think and decided to provide an option to use i18n for tab labels without monkey patching.

In short, if you add admin.globalize_field.tab_label keys to your config/locales/*.yml files, the gem will use those values by default.