[ADD] Create a task to generate model attribute reference for translation

Thanks to Romuald Conty (@neomilium)'s help, this is possible.
parent 80c6b07e
# frozen_string_literal: true
def add_i18n_tasks_use(key)
@content += "# i18n-tasks-use t('#{key}')\n"
end
def add_i18n_tasks_use_for_activerecord_model(model)
add_i18n_tasks_use("activerecord.models.#{model}")
end
def add_i18n_tasks_use_for_activerecord_attribute(model, attribute)
add_i18n_tasks_use("activerecord.attributes.#{model}.#{attribute}")
end
I18N_FILE = File.join('app', 'i18n', 'elabs', 'model_attributes.i18n')
FileUtils.rm(I18N_FILE) if File.exist? I18N_FILE
EXCLUDED_MODELS = [
'Elabs::AlbumsTag',
'Elabs::ApplicationContentRecord',
'Elabs::ApplicationRecord',
'Elabs::ProjectsTag',
'Elabs::AlbumsUpload',
'Elabs::ArticlesTag',
'Elabs::NotesTag',
'Elabs::ProjectsAlbum',
'Elabs::ProjectsArticle',
'Elabs::ProjectsNote',
'Elabs::ProjectsTag',
'Elabs::ProjectsUpload',
'Elabs::UploadsTag'
].freeze
@content = ''
Dir.glob(File.join('app', 'models', 'elabs', '*.rb')).each do |f|
class_name = "Elabs::#{File.basename(f, '.rb').camelize}"
next if EXCLUDED_MODELS.include? class_name
model = class_name.split('::').join('.').underscore
puts "Checking for model #{class_name} (#{model})"
add_i18n_tasks_use_for_activerecord_model(model)
# Standard attributes
class_obj = class_name.constantize
class_obj.attribute_types.each do |attribute, _type|
next if attribute == 'id'
add_i18n_tasks_use_for_activerecord_attribute(model, attribute.sub(/_id$/, ''))
end
end
comment = "# Generated using \"rake app:i18n:add-models-attributes\" - Do not modify manually\n"
File.open(I18N_FILE, 'w') do |file|
file.write "#{comment}\n#{@content}\n#{comment}\n"
end
namespace :i18n do
desc 'Generate ActiveRecord models and attributes list to help i18n-task'
task 'add-models-attributes': :environment do
system 'rails runner lib/runners/generate_i18n_model_attributes.rb'
end
desc 'Add missing translations'
task 'add-missing': :environment do
system 'i18n-tasks add-missing -v "TRANSLATE_ME %{human_key}"' # rubocop:disable Style/FormatStringToken
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment