2021-06-01 12:20:20 +00:00
|
|
|
# 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
|
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
returns the records of all locales that are to be synchronized
|
2016-07-25 20:13:38 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-09-21 12:20:36 +00:00
|
|
|
def self.to_sync
|
2015-11-19 08:06:12 +00:00
|
|
|
# read used locales based on env, e. g. export Z_LOCALES='en-us:de-de'
|
2021-11-15 15:58:19 +00:00
|
|
|
return Locale.where(active: true, locale: ENV['Z_LOCALES'].split(':')) if ENV['Z_LOCALES']
|
2016-07-25 20:13:38 +00:00
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
return Locale.where(active: true, locale: %w[en-us de-de]) if Rails.env.test?
|
2016-12-03 19:37:37 +00:00
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
Locale.where(active: true)
|
2016-12-03 19:37:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
sync locales from config/locales.yml
|
2016-07-25 20:13:38 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
def self.sync
|
|
|
|
file = Rails.root.join('config/locales.yml')
|
2016-12-03 19:37:37 +00:00
|
|
|
return false if !File.exist?(file)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-12-03 19:37:37 +00:00
|
|
|
data = YAML.load_file(file)
|
|
|
|
to_database(data)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2020-01-27 09:28:17 +00:00
|
|
|
# Default system locale
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# Locale.default
|
|
|
|
def self.default
|
|
|
|
Setting.get('locale_default') || 'en-us'
|
|
|
|
end
|
|
|
|
|
2016-12-03 19:37:37 +00:00
|
|
|
private_class_method def self.to_database(data)
|
2015-04-28 21:08:39 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
2017-10-01 12:25:52 +00:00
|
|
|
data.each do |locale|
|
2015-05-07 10:15:40 +00:00
|
|
|
exists = Locale.find_by(locale: locale['locale'])
|
2015-04-28 21:08:39 +00:00
|
|
|
if exists
|
2017-09-11 11:16:08 +00:00
|
|
|
exists.update!(locale.symbolize_keys!)
|
2015-04-28 21:08:39 +00:00
|
|
|
else
|
2017-09-11 11:16:08 +00:00
|
|
|
Locale.create!(locale.symbolize_keys!)
|
2015-04-28 21:08:39 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-04-28 21:08:39 +00:00
|
|
|
end
|
2015-04-27 06:20:52 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|