In the last post, we saw how we can translate a variety of dynamic contents that we may have in our Rails applications using globalize gem. There is another complementary gem to globalize that can be used to do these translations inside Rails Admin, which make it a lot easier and simpler to insert these translations inside RailsAdmin’s user friendly interface.
It is called rails_admin_globalize_field and is very easy to install and configure.
You should initially have globalize gem already installed and configured; rails_admin_globalize_field uses it to its job.
Once we are done with the installation and configuration of globalize, we can install this new gem:
gem 'rails_admin_globalize_field'
After that, you can include translation models at your rails_admin.rb config file with the following:
config.included_models << ['Model','Model::Translation']
You should also add the following at the model that you are trying to do the translation for:
<span style="font-weight: 400;"> class Model < ActiveRecord::Base</span>
<span style="font-weight: 400;"> translates :title, :desc</span>
<span style="font-weight: 400;"> accepts_nested_attributes_for :translations, allow_destroy: true</span>
<span style="font-weight: 400;"> end</span>
Add configuration to your translated model and associated translation model. :locale field is always required.
<span style="font-weight: 400;">config.model 'Post' do</span>
<span style="font-weight: 400;"> configure :translations, :globalize_tabs</span>
<span style="font-weight: 400;"> edit do</span>
<span style="font-weight: 400;"> field :translations do</span>
<span style="font-weight: 400;"> label "Translations"</span>
<span style="font-weight: 400;"> end</span>
<span style="font-weight: 400;"> end</span>
<span style="font-weight: 400;">end</span>
<span style="font-weight: 400;"> config.model 'Model::Translation' do</span>
<span style="font-weight: 400;"> visible false</span>
<span style="font-weight: 400;"> configure :locale, :hidden do</span>
<span style="font-weight: 400;"> help ''</span>
<span style="font-weight: 400;"> end</span>
<span style="font-weight: 400;"> include_fields :locale, :title, :desc</span>
<span style="font-weight: 400;"> end</span>
Do not forget to include the translations field in your Edit page, because you may not be able to see it being displayed in your RailsAdmin.
You can see a preview of how it looks like in the image displayed above.
This is an open source project, that you can learn and benefit from, and also contribute to make it better. At the time of this writing, this gem has only 29 stars in its Github page, but I believe it deserves a lot more, because of its applicability.