2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2015-04-27 06:20:52 +00:00
|
|
|
|
|
|
|
class Locale < ApplicationModel
|
|
|
|
|
2016-07-25 20:13:38 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get locals to sync
|
|
|
|
|
|
|
|
all:
|
|
|
|
|
2016-12-03 19:37:37 +00:00
|
|
|
Locale.to_sync
|
2016-07-25 20:13:38 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
['en-us', 'de-de', ...]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-09-21 12:20:36 +00:00
|
|
|
def self.to_sync
|
|
|
|
locales = Locale.where(active: true)
|
|
|
|
if Rails.env.test?
|
2016-12-06 11:11:37 +00:00
|
|
|
locales = Locale.where(active: true, locale: ['en-us', 'de-de'])
|
2015-09-21 12:20:36 +00:00
|
|
|
end
|
2015-11-19 08:06:12 +00:00
|
|
|
|
|
|
|
# read used locales based on env, e. g. export Z_LOCALES='en-us:de-de'
|
|
|
|
if ENV['Z_LOCALES']
|
2017-11-15 14:06:53 +00:00
|
|
|
locales = Locale.where(active: true, locale: ENV['Z_LOCALES'].split(':'))
|
2015-11-19 08:06:12 +00:00
|
|
|
end
|
2015-09-21 12:20:36 +00:00
|
|
|
locales
|
|
|
|
end
|
|
|
|
|
2016-07-25 20:13:38 +00:00
|
|
|
=begin
|
|
|
|
|
2016-12-03 19:37:37 +00:00
|
|
|
sync locales from local if exists, otherwise from online
|
|
|
|
|
|
|
|
all:
|
|
|
|
|
|
|
|
Locale.sync
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.sync
|
|
|
|
return true if load_from_file
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-12-03 19:37:37 +00:00
|
|
|
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
|
2016-12-03 19:37:37 +00:00
|
|
|
data = fetch
|
|
|
|
to_database(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
load locales from local
|
|
|
|
|
|
|
|
all:
|
|
|
|
|
|
|
|
Locale.load_from_file
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.load_from_file
|
2017-02-10 14:06:15 +00:00
|
|
|
version = Version.get
|
2017-11-23 08:09:44 +00:00
|
|
|
file = Rails.root.join('config', "locales-#{version}.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
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
fetch locales from remote and store them in local file system
|
|
|
|
|
|
|
|
all:
|
|
|
|
|
|
|
|
Locale.fetch
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.fetch
|
2017-02-10 14:06:15 +00:00
|
|
|
version = Version.get
|
2015-04-27 11:47:48 +00:00
|
|
|
url = 'https://i18n.zammad.com/api/v1/locales'
|
2015-04-27 06:20:52 +00:00
|
|
|
|
|
|
|
result = UserAgent.get(
|
|
|
|
url,
|
2017-02-02 14:37:49 +00:00
|
|
|
{
|
2017-02-10 14:06:15 +00:00
|
|
|
version: version,
|
2017-02-02 14:37:49 +00:00
|
|
|
},
|
2015-04-27 06:20:52 +00:00
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
json: true,
|
2017-09-23 12:31:16 +00:00
|
|
|
open_timeout: 8,
|
|
|
|
read_timeout: 24,
|
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")
|
2016-12-03 19:37:37 +00:00
|
|
|
File.open(file, 'w') do |out|
|
|
|
|
YAML.dump(result.data, out)
|
|
|
|
end
|
|
|
|
result.data
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|