trabajo-afectivo/app/models/locale.rb

145 lines
2.5 KiB
Ruby
Raw Permalink 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
get locals to sync
all:
Locale.to_sync
2016-07-25 20:13:38 +00:00
returns
['en-us', 'de-de', ...]
=end
def self.to_sync
locales = Locale.where(active: true)
if Rails.env.test?
locales = Locale.where(active: true, locale: %w[en-us de-de])
end
# read used locales based on env, e. g. export Z_LOCALES='en-us:de-de'
if ENV['Z_LOCALES']
locales = Locale.where(active: true, locale: ENV['Z_LOCALES'].split(':'))
end
locales
end
2016-07-25 20:13:38 +00:00
=begin
sync locales from local if exists, otherwise from online
all:
Locale.sync
=end
def self.sync
return true if load_from_file
load
end
=begin
2016-07-25 20:13:38 +00:00
load locales from online
all:
Locale.load
=end
2015-04-27 06:20:52 +00:00
def self.load
data = fetch
to_database(data)
end
=begin
load locales from local
all:
Locale.load_from_file
=end
def self.load_from_file
version = Version.get
2017-11-23 08:09:44 +00:00
file = Rails.root.join('config', "locales-#{version}.yml")
return false if !File.exist?(file)
data = YAML.load_file(file)
to_database(data)
true
end
=begin
fetch locales from remote and store them in local file system
all:
Locale.fetch
=end
def self.fetch
version = Version.get
url = 'https://i18n.zammad.com/api/v1/locales'
2015-04-27 06:20:52 +00:00
result = UserAgent.get(
url,
{
version: version,
},
2015-04-27 06:20:52 +00:00
{
json: true,
open_timeout: 8,
read_timeout: 24,
verify_ssl: true,
2015-04-27 06:20:52 +00:00
}
)
2015-04-27 11:57:36 +00:00
2016-03-01 14:26:46 +00:00
raise "Can't load locales from #{url}" if !result
raise "Can't load locales from #{url}: #{result.error}" if !result.success?
2015-04-27 11:57:36 +00:00
2017-11-23 08:09:44 +00:00
file = Rails.root.join('config', "locales-#{version}.yml")
File.open(file, 'w') do |out|
YAML.dump(result.data, out)
end
result.data
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