Add ability to translate server rules (#34494)
This commit is contained in:
parent
977164decc
commit
8c51a8ba94
17 changed files with 149 additions and 20 deletions
|
@ -19,6 +19,9 @@ class Rule < ApplicationRecord
|
|||
|
||||
self.discard_column = :deleted_at
|
||||
|
||||
has_many :translations, inverse_of: :rule, class_name: 'RuleTranslation', dependent: :destroy
|
||||
accepts_nested_attributes_for :translations, reject_if: :all_blank, allow_destroy: true
|
||||
|
||||
validates :text, presence: true, length: { maximum: TEXT_SIZE_LIMIT }
|
||||
|
||||
scope :ordered, -> { kept.order(priority: :asc, id: :asc) }
|
||||
|
@ -36,4 +39,9 @@ class Rule < ApplicationRecord
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def translation_for(locale)
|
||||
@cached_translations ||= {}
|
||||
@cached_translations[locale] ||= translations.find_by(language: locale) || RuleTranslation.new(language: locale, text: text, hint: hint)
|
||||
end
|
||||
end
|
||||
|
|
20
app/models/rule_translation.rb
Normal file
20
app/models/rule_translation.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: rule_translations
|
||||
#
|
||||
# id :bigint(8) not null, primary key
|
||||
# hint :text default(""), not null
|
||||
# language :string not null
|
||||
# text :text default(""), not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# rule_id :bigint(8) not null
|
||||
#
|
||||
class RuleTranslation < ApplicationRecord
|
||||
belongs_to :rule
|
||||
|
||||
validates :language, presence: true, uniqueness: { scope: :rule_id }
|
||||
validates :text, presence: true, length: { maximum: Rule::TEXT_SIZE_LIMIT }
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue