Class: RailsI18nOnair::SyncService

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_i18n_onair/sync_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_locale, destination_locale) ⇒ SyncService

Returns a new instance of SyncService.



7
8
9
10
11
# File 'lib/rails_i18n_onair/sync_service.rb', line 7

def initialize(source_locale, destination_locale)
  @source_locale = source_locale.to_s
  @destination_locale = destination_locale.to_s
  @missing_keys = []
end

Instance Attribute Details

#destination_localeObject (readonly)

Returns the value of attribute destination_locale.



5
6
7
# File 'lib/rails_i18n_onair/sync_service.rb', line 5

def destination_locale
  @destination_locale
end

#missing_keysObject (readonly)

Returns the value of attribute missing_keys.



5
6
7
# File 'lib/rails_i18n_onair/sync_service.rb', line 5

def missing_keys
  @missing_keys
end

#source_localeObject (readonly)

Returns the value of attribute source_locale.



5
6
7
# File 'lib/rails_i18n_onair/sync_service.rb', line 5

def source_locale
  @source_locale
end

Instance Method Details

#compareObject

Compare source and destination, return list of missing keys with their source values



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rails_i18n_onair/sync_service.rb', line 14

def compare
  source_data = load_locale_data(source_locale)
  dest_data = load_locale_data(destination_locale)

  # Strip the top-level locale key (e.g. { "en" => { ... } } → { ... })
  source_tree = source_data[source_locale] || {}
  dest_tree = dest_data[destination_locale] || {}

  @missing_keys = []
  deep_compare(source_tree, dest_tree, "")
  @missing_keys
end

#sync!Object

Copy missing keys from source to destination. Comparison uses the merged view of all destination files (so keys living in devise..yml are not duplicated), but new keys are written only into the base .yml file.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rails_i18n_onair/sync_service.rb', line 31

def sync!
  missing = compare
  return { synced: 0, missing_keys: [] } if missing.empty?

  dest_data = load_writable_locale_data(destination_locale)
  dest_tree = dest_data[destination_locale] || {}

  missing.each do |entry|
    set_nested_value(dest_tree, entry[:key], entry[:value])
  end

  save_locale_data(destination_locale, { destination_locale => dest_tree })

  { synced: missing.count, missing_keys: missing }
end