Skip to content

Commit

Permalink
Make translation model configurable (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
23tux committed Sep 15, 2023
1 parent 00b36bc commit c710743
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
21 changes: 15 additions & 6 deletions lib/i18n/backend/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ def initialize
reload!
end

module TranslationModel
private

def translation_model
I18n::Backend::ActiveRecord.config.translation_model
end
end

module Implementation
include Base
include Flatten
include TranslationModel

def available_locales
Translation.available_locales
translation_model.available_locales
rescue ::ActiveRecord::StatementInvalid
[]
end
Expand All @@ -39,15 +48,15 @@ def store_translations(locale, data, options = {})
escape = options.fetch(:escape, true)

flatten_translations(locale, data, escape, false).each do |key, value|
translation = Translation.locale(locale).lookup(expand_keys(key))
translation = translation_model.locale(locale).lookup(expand_keys(key))

if self.class.config.cleanup_with_destroy
translation.destroy_all
else
translation.delete_all
end

Translation.create(locale: locale.to_s, key: key.to_s, value: value)
translation_model.create(locale: locale.to_s, key: key.to_s, value: value)
end

reload! if self.class.config.cache_translations
Expand All @@ -64,7 +73,7 @@ def initialized?
end

def init_translations
@translations = Translation.to_h
@translations = translation_model.to_h
end

def translations(do_init: false)
Expand All @@ -86,9 +95,9 @@ def lookup(locale, key, scope = [], options = {})
end

result = if key == ''
Translation.locale(locale).all
translation_model.locale(locale).all
else
Translation.locale(locale).lookup(key)
translation_model.locale(locale).lookup(key)
end

if result.empty?
Expand Down
3 changes: 2 additions & 1 deletion lib/i18n/backend/active_record/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ module I18n
module Backend
class ActiveRecord
class Configuration
attr_accessor :cleanup_with_destroy, :cache_translations
attr_accessor :cleanup_with_destroy, :cache_translations, :translation_model

def initialize
@cleanup_with_destroy = false
@cache_translations = false
@translation_model = I18n::Backend::ActiveRecord::Translation
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/i18n/backend/active_record/missing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,22 @@ module Backend
class ActiveRecord
module Missing
include Flatten
include TranslationModel

def store_default_translations(locale, key, options = {})
count, scope, _, separator = options.values_at(:count, :scope, :default, :separator)
separator ||= I18n.default_separator
key = normalize_flat_keys(locale, key, scope, separator)

return if ActiveRecord::Translation.locale(locale).lookup(key).exists?
return if translation_model.locale(locale).lookup(key).exists?

interpolations = options.keys - I18n::RESERVED_KEYS
keys = count ? I18n.t('i18n.plural.keys', locale: locale).map { |k| [key, k].join(FLATTEN_SEPARATOR) } : [key]
keys.each { |k| store_default_translation(locale, k, interpolations) }
end

def store_default_translation(locale, key, interpolations)
translation = ActiveRecord::Translation.new locale: locale.to_s, key: key
translation = translation_model.new locale: locale.to_s, key: key
translation.interpolations = interpolations
translation.save
end
Expand Down
4 changes: 3 additions & 1 deletion lib/i18n/backend/active_record/store_procs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module I18n
module Backend
class ActiveRecord
module StoreProcs
extend TranslationModel

def value=(val)
case val
when Proc
Expand All @@ -33,7 +35,7 @@ def value=(val)
end
end

Translation.send(:include, self) if method(:to_s).respond_to?(:to_ruby)
translation_model.send(:include, self) if method(:to_s).respond_to?(:to_ruby)
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions test/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,23 @@ def setup
I18n.t('foo')
end
end

class WithCustomTranslationModel < I18nBackendActiveRecordTest
class CustomTranslation < I18n::Backend::ActiveRecord::Translation
def value=(val)
super("custom #{val}")
end
end

def setup
super

I18n::Backend::ActiveRecord.config.translation_model = CustomTranslation
end

test 'use a custom model' do
store_translations(:en, foo: 'foo')
assert_equal I18n.t(:foo), 'custom foo'
end
end
end

0 comments on commit c710743

Please sign in to comment.