Skip to content

Commit

Permalink
Post rebase fixes and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
timfjord committed Oct 26, 2021
1 parent 1a5e6b4 commit 1f875b0
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 141 deletions.
3 changes: 1 addition & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ GEM
i18n (1.8.10)
concurrent-ruby (~> 1.0)
minitest (5.14.4)
mocha (1.2.1)
metaclass (~> 0.0.1)
mocha (1.13.0)
mysql2 (0.5.3)
pg (1.2.3)
rake (13.0.1)
Expand Down
162 changes: 81 additions & 81 deletions lib/i18n/backend/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class ActiveRecord
autoload :Translation, 'i18n/backend/active_record/translation'
autoload :Configuration, 'i18n/backend/active_record/configuration'

include Base
include Flatten

class << self
def configure
yield(config) if block_given?
Expand All @@ -19,114 +22,111 @@ def config
end
end

module Implementation
include Base, Flatten
def initialize(*args)
super

def available_locales
begin
Translation.available_locales
rescue ::ActiveRecord::StatementInvalid
[]
end
end
reload!
end

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))
def available_locales
Translation.available_locales
rescue ::ActiveRecord::StatementInvalid
[]
end

if ActiveRecord.config.cleanup_with_destroy
translation.destroy_all
else
translation.delete_all
end
def store_translations(locale, data, options = {})
escape = options.fetch(:escape, true)

Translation.create(:locale => locale.to_s, :key => key.to_s, :value => value)
flatten_translations(locale, data, escape, false).each do |key, value|
translation = Translation.locale(locale).lookup(expand_keys(key))

if ActiveRecord.config.cleanup_with_destroy
translation.destroy_all
else
translation.delete_all
end

reload! if ActiveRecord.config.cache_translations
Translation.create(locale: locale.to_s, key: key.to_s, value: value)
end

def reload!
@translations = nil
reload! if ActiveRecord.config.cache_translations
end

self
end
def reload!
@translations = nil

def initialized?
!@translations.nil?
end
self
end

def init_translations
@translations = Translation.to_hash
end
def initialized?
!@translations.nil?
end

def translations(do_init: false)
init_translations if do_init || !initialized?
@translations ||= {}
end
def init_translations
@translations = Translation.to_hash
end

protected
def translations(do_init: false)
init_translations if do_init || !initialized?
@translations ||= {}
end

def lookup(locale, key, scope = [], options = {})
key = normalize_flat_keys(locale, key, scope, options[:separator])
if key.first == '.'
key = key[1..-1]
end
if key.last == '.'
key = key[0..-2]
end
protected

if ActiveRecord.config.cache_translations
init_translations if @translations.nil? || @translations.empty?
def lookup(locale, key, scope = [], options = {})
key = normalize_flat_keys(locale, key, scope, options[:separator])
key = key[1..-1] if key.first == '.'
key = key[0..-2] if key.last == '.'

keys = ([locale] + key.split(I18n::Backend::Flatten::FLATTEN_SEPARATOR)).map(&:to_sym)
if ActiveRecord.config.cache_translations
init_translations if @translations.nil? || @translations.empty?

return translations.dig(*keys)
end
keys = ([locale] + key.split(I18n::Backend::Flatten::FLATTEN_SEPARATOR)).map(&:to_sym)

result = if key == ''
Translation.locale(locale).all
else
Translation.locale(locale).lookup(key)
end
return translations.dig(*keys)
end

if result.empty?
nil
elsif result.first.key == key
result.first.value
else
result = result.inject({}) do |hash, translation|
hash.deep_merge build_translation_hash_by_key(key, translation)
end
result.deep_symbolize_keys
end
result = if key == ''
Translation.locale(locale).all
else
Translation.locale(locale).lookup(key)
end

def build_translation_hash_by_key(lookup_key, translation)
hash = {}
if lookup_key == ''
chop_range = 0..-1
else
chop_range = (lookup_key.size + FLATTEN_SEPARATOR.size)..-1
if result.empty?
nil
elsif result.first.key == key
result.first.value
else
result = result.inject({}) do |hash, translation|
hash.deep_merge build_translation_hash_by_key(key, translation)
end
translation_nested_keys = translation.key.slice(chop_range).split(FLATTEN_SEPARATOR)
translation_nested_keys.each.with_index.inject(hash) do |iterator, (key, index)|
iterator[key] = translation_nested_keys[index + 1] ? {} : translation.value
iterator[key]
end
hash
result.deep_symbolize_keys
end
end

# For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
def expand_keys(key)
key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, key|
keys << [keys.last, key].compact.join(FLATTEN_SEPARATOR)
end
def build_translation_hash_by_key(lookup_key, translation)
hash = {}

chop_range = if lookup_key == ''
0..-1
else
(lookup_key.size + FLATTEN_SEPARATOR.size)..-1
end
translation_nested_keys = translation.key.slice(chop_range).split(FLATTEN_SEPARATOR)
translation_nested_keys.each.with_index.inject(hash) do |iterator, (key, index)|
iterator[key] = translation_nested_keys[index + 1] ? {} : translation.value
iterator[key]
end

hash
end

include Implementation
# For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
def expand_keys(key)
key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, k|
keys << [keys.last, k].compact.join(FLATTEN_SEPARATOR)
end
end
end
end
end
3 changes: 1 addition & 2 deletions lib/i18n/backend/active_record/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ module I18n
module Backend
class ActiveRecord
class Configuration
attr_accessor :cleanup_with_destroy
attr_accessor :cache_translations
attr_accessor :cleanup_with_destroy, :cache_translations

def initialize
@cleanup_with_destroy = false
Expand Down
15 changes: 8 additions & 7 deletions lib/i18n/backend/active_record/missing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,24 @@ def store_default_translations(locale, key, options = {})
separator ||= I18n.default_separator
key = normalize_flat_keys(locale, key, scope, separator)

unless ActiveRecord::Translation.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 { |key| store_default_translation(locale, key, interpolations) }
end
return if ActiveRecord::Translation.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 = ActiveRecord::Translation.new locale: locale.to_s, key: key
translation.interpolations = interpolations
translation.save
end

def translate(locale, key, options = {})
result = catch(:exception) { super }

if result.is_a?(I18n::MissingTranslation)
self.store_default_translations(locale, key, options)
store_default_translations(locale, key, options)
throw(:exception, result)
else
result
Expand Down
6 changes: 3 additions & 3 deletions lib/i18n/backend/active_record/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ class Translation < ::ActiveRecord::Base

class << self
def locale(locale)
where(:locale => locale.to_s)
where(locale: locale.to_s)
end

def lookup(keys, *separator)
column_name = connection.quote_column_name('key')
keys = Array(keys).map! { |key| key.to_s }
keys = Array(keys).map!(&:to_s)

unless separator.empty?
warn "[DEPRECATION] Giving a separator to Translation.lookup is deprecated. " <<
Expand Down Expand Up @@ -91,7 +91,7 @@ def to_hash
end

def interpolates?(key)
self.interpolations.include?(key) if self.interpolations
interpolations&.include?(key)
end

def value
Expand Down
Loading

0 comments on commit 1f875b0

Please sign in to comment.