Class: RailsI18nOnair::Translation

Inherits:
ApplicationRecord show all
Defined in:
app/models/rails_i18n_onair/translation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_or_update_by_language(language, translation_data) ⇒ Object



85
86
87
88
89
90
# File 'app/models/rails_i18n_onair/translation.rb', line 85

def self.create_or_update_by_language(language, translation_data)
  translation_record = find_or_initialize_by(language: language)
  translation_record.translation = translation_data
  translation_record.save!
  translation_record
end

.import_from_yaml(language, yaml_file_path) ⇒ Object

Convert YAML file to JSON and store in database



42
43
44
45
46
47
# File 'app/models/rails_i18n_onair/translation.rb', line 42

def self.import_from_yaml(language, yaml_file_path)
  yaml_content = YAML.load_file(yaml_file_path)
  translation_data = yaml_content

  create_or_update_by_language(language, translation_data)
end

.load_locale(locale) ⇒ Object

Load a single locale efficiently



12
13
14
# File 'app/models/rails_i18n_onair/translation.rb', line 12

def load_locale(locale)
  find_by(language: locale.to_s)
end

.load_locales(locales) ⇒ Object

Load multiple locales in one query



17
18
19
20
21
# File 'app/models/rails_i18n_onair/translation.rb', line 17

def load_locales(locales)
  where(language: locales.map(&:to_s))
    .pluck(:language, :translation)
    .to_h
end

.locale_exists?(locale) ⇒ Boolean

Check if locale exists without loading data

Returns:

  • (Boolean)


24
25
26
# File 'app/models/rails_i18n_onair/translation.rb', line 24

def locale_exists?(locale)
  exists?(language: locale.to_s)
end

.lookup_key(locale, key_path) ⇒ Object

Get a specific translation key. Loads the locale row (unique index on :language) and walks the JSONB document in Ruby.



30
31
32
33
34
35
36
37
38
# File 'app/models/rails_i18n_onair/translation.rb', line 30

def lookup_key(locale, key_path)
  translation_record = find_by(language: locale.to_s)
  return nil unless translation_record

  keys = key_path.split('.')
  keys.reduce(translation_record.translation) { |hash, key|
    hash&.dig(key) || hash&.dig(key.to_sym)
  }
end

Instance Method Details

#export_to_yamlObject

Export translation to YAML format



50
51
52
# File 'app/models/rails_i18n_onair/translation.rb', line 50

def export_to_yaml
  { language => translation }.to_yaml
end

#get_translation(key_path) ⇒ Object

Get a translation value by key path (e.g., "user.name")



55
56
57
58
# File 'app/models/rails_i18n_onair/translation.rb', line 55

def get_translation(key_path)
  keys = key_path.split(".")
  keys.reduce(translation) { |hash, key| hash&.dig(key) || hash&.dig(key.to_sym) }
end

#merge_translations(new_translations) ⇒ Object

Merge new translations into existing ones



78
79
80
81
# File 'app/models/rails_i18n_onair/translation.rb', line 78

def merge_translations(new_translations)
  self.translation = translation.deep_merge(new_translations)
  save
end

#set_translation(key_path, value) ⇒ Object

Set a translation value by key path. Returns false when an intermediate node is not a Hash (the path passes through a leaf value) instead of raising.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/rails_i18n_onair/translation.rb', line 63

def set_translation(key_path, value)
  keys = key_path.split(".")
  last_key = keys.pop
  hash = keys.reduce(translation) do |h, key|
    return false unless h.is_a?(Hash)

    h[key] ||= {}
  end
  return false unless hash.is_a?(Hash)

  hash[last_key] = value
  save
end