We initially thought that we will generate a separate translation file for each company, if they decide to change the terminology. This separate file will have all the text replaced with the text that the company admin has specified. However this approach had two issues.
* In order to load the translation file the server has to be restarted
* As some point we might add few more entries in our original translation file which may contain the text "course", "live event" etc. In such cases the company specific translation file has to be regenerated.
Because of these two reasons this approach was not viable.
Right now the approach that we are thinking to follow is as below:
Change the existing translations which contain text "course", "in person event", "live event", "In person event" to take an argument. The value to this argument will be passed when calling that particular translation in the view/controller/model. The value will be picked up from the database. A company admin will have a screen where in he can associate some text to each of the "course", "in person event", "live event", "In person event". If the text is not associated then the default value defined in our en.yml file will be picked up. The Screen to associate the text will have following fields
Enter the text to be replaced in UI
Course: [text box]
Live Event: [text box]
In Person Event: [text box]
Certification: [text box]
Select Language:[Drop down] (This will be a drop down where the user can select the language. Currently it will contain only "English".
[Save] [Cancel]
When a user accesses the sub-site for the first time we will cache the customized translation in the session to ensure that we do not hit the db to fetch the customized text for every request.
The table to store the customized text will look as below
__translations__
| id | key | value | lang | company_id |
| 1 | certification | Quiz | en | 1 |
| 2 | live_event | Virtual Event | en | 1 |
====== Code changes ======
**Currently we do following**
In en.yml
course: Course
course_success_message: Course was successfully saved
When using the translation in view or controller we call it using
= :course_success_message.l
**To allow customization of strings we will have to change it to**
en.yml
course: {{course_text}}
course_success_message: {{course_text}} was successfully saved
When using the translation in view or controller we call it using
= :course_success_message.l(:course_text => value_from_db)
In base_controller.rb
TRANSLATABLE_TEXTS = ['course', 'live_event', 'certification' ,'in_person_event']
TRANSLATABLE_TEXT_DEFAULTS = {'course' => :course.l, 'live_event' => :live_event.l, 'certification' => :certification.l}
def current_account_translations
if session[:translations].nil? # fetch from DB and store this at the 1st request and then cache it in session.
session[:translations] = current_account.translations.merge(TRANSLATABLE_TEXT_DEFAULTS) # use default value for the keys for which translation is not defined by the company.
end
session[:translations]
end
In company.rb
def translations
#return key value pair indicating the text that needs to be replaced for the company
# E.g {'certification' => 'Quiz', 'live_event' => 'Virtual Event'}
end