locale.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Locale < ApplicationModel
  3. has_many :knowledge_base_locales, inverse_of: :system_locale, dependent: :restrict_with_error,
  4. class_name: 'KnowledgeBase::Locale', foreign_key: :system_locale_id
  5. =begin
  6. returns the records of all locales that are to be synchronized
  7. =end
  8. def self.to_sync
  9. # read used locales based on env, e. g. export Z_LOCALES='en-us:de-de'
  10. return Locale.where(active: true, locale: ENV['Z_LOCALES'].split(':')) if ENV['Z_LOCALES']
  11. return Locale.where(active: true, locale: %w[en-us de-de]) if Rails.env.test?
  12. Locale.where(active: true)
  13. end
  14. =begin
  15. sync locales from config/locales.yml
  16. =end
  17. def self.sync
  18. file = Rails.root.join('config/locales.yml')
  19. return false if !File.exist?(file)
  20. data = YAML.load_file(file)
  21. to_database(data)
  22. true
  23. end
  24. # Default system locale
  25. #
  26. # @example
  27. # Locale.default
  28. def self.default
  29. Setting.get('locale_default') || 'en-us'
  30. end
  31. private_class_method def self.to_database(data)
  32. ActiveRecord::Base.transaction do
  33. data.each do |locale|
  34. exists = Locale.find_by(locale: locale['locale'])
  35. if exists
  36. exists.update!(locale.symbolize_keys!)
  37. else
  38. Locale.create!(locale.symbolize_keys!)
  39. end
  40. end
  41. end
  42. end
  43. end