Closed felix91gr closed 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: "Русский"
Omg, thank you!
Question: where do I create that class? The application_helper.rb
should suffice, right?
Edit: apparently, yes!!
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
.
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
):
(I tried one and then the other, that's why I have both files ^^)
Most likely, you need to require that file somewhere to apply the monkey patching. (e.g. in config/application.rb
).
I'm trying this and it's working.
Basically, I'm doing the monkey patching like so:
config/initializers/rails_admin_globalize_fields.rb
)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?
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.
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 :)
@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.
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?