trabajo-afectivo/app/models/locale.rb

60 lines
1.4 KiB
Ruby
Raw Normal View History

# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
2015-04-27 06:20:52 +00:00
class Locale < ApplicationModel
2019-06-04 03:40:48 +00:00
has_many :knowledge_base_locales, inverse_of: :system_locale, dependent: :restrict_with_error,
class_name: 'KnowledgeBase::Locale', foreign_key: :system_locale_id
2016-07-25 20:13:38 +00:00
=begin
returns the records of all locales that are to be synchronized
2016-07-25 20:13:38 +00:00
=end
def self.to_sync
# read used locales based on env, e. g. export Z_LOCALES='en-us:de-de'
return Locale.where(active: true, locale: ENV['Z_LOCALES'].split(':')) if ENV['Z_LOCALES']
2016-07-25 20:13:38 +00:00
return Locale.where(active: true, locale: %w[en-us de-de]) if Rails.env.test?
Locale.where(active: true)
end
=begin
sync locales from config/locales.yml
2016-07-25 20:13:38 +00:00
=end
def self.sync
file = Rails.root.join('config/locales.yml')
return false if !File.exist?(file)
data = YAML.load_file(file)
to_database(data)
true
end
# Default system locale
#
# @example
# Locale.default
def self.default
Setting.get('locale_default') || 'en-us'
end
private_class_method def self.to_database(data)
2015-04-28 21:08:39 +00:00
ActiveRecord::Base.transaction do
data.each do |locale|
exists = Locale.find_by(locale: locale['locale'])
2015-04-28 21:08:39 +00:00
if exists
exists.update!(locale.symbolize_keys!)
2015-04-28 21:08:39 +00:00
else
Locale.create!(locale.symbolize_keys!)
2015-04-28 21:08:39 +00:00
end
end
2015-04-28 21:08:39 +00:00
end
2015-04-27 06:20:52 +00:00
end
end