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:
|
|
|
|
|
|
|
|
Locale.sync
|
|
|
|
|
|
|
|
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-01-19 06:58:58 +00:00
|
|
|
locales = Locale.where(active: true, locale: ['en-us'])
|
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']
|
|
|
|
locales = Locale.where(active: true, locale: ENV['Z_LOCALES'].split(':') )
|
|
|
|
end
|
2015-09-21 12:20:36 +00:00
|
|
|
locales
|
|
|
|
end
|
|
|
|
|
2016-07-25 20:13:38 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
load locales from online
|
|
|
|
|
|
|
|
all:
|
|
|
|
|
|
|
|
Locale.load
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 06:20:52 +00:00
|
|
|
def self.load
|
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,
|
|
|
|
{},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
json: 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
|
|
|
|
2015-04-28 21:08:39 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
2016-06-30 20:04:48 +00:00
|
|
|
result.data.each { |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
|
|
|
|
exists.update(locale.symbolize_keys!)
|
|
|
|
else
|
|
|
|
Locale.create(locale.symbolize_keys!)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2015-04-27 06:20:52 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|