trabajo-afectivo/app/models/translation.rb

222 lines
4.7 KiB
Ruby
Raw Normal View History

2014-02-03 19:23:00 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Translation < ApplicationModel
2012-05-18 14:24:00 +00:00
before_create :set_initial
after_create :cache_clear
after_update :cache_clear
after_destroy :cache_clear
2012-05-18 14:24:00 +00:00
2015-04-27 06:20:52 +00:00
=begin
load translations from online
Translation.load
=end
def self.load
2015-04-27 11:57:36 +00:00
url = 'https://i18n.zammad.com/api/v1/translations'
2015-04-27 06:20:52 +00:00
if !UserInfo.current_user_id
UserInfo.current_user_id = 1
end
result = UserAgent.get(
url,
{},
{
json: true,
2015-04-27 06:20:52 +00:00
}
)
fail "Can't load translations from #{url}: #{result.error}" if !result.success?
2015-04-28 21:08:39 +00:00
ActiveRecord::Base.transaction do
result.data.each {|translation|
# handle case insensitive sql
exists = Translation.where(locale: translation['locale'], format: translation['format'], source: translation['source'])
translaten = nil
exists.each {|item|
if item.source == translation['source']
translaten = item
end
}
if translaten
# verify if update is needed
translaten.update_attributes(translation.symbolize_keys!)
translaten.save
else
Translation.create(translation.symbolize_keys!)
end
}
2015-04-28 21:08:39 +00:00
end
2015-04-27 06:20:52 +00:00
true
end
=begin
push translations to online
Translation.push(locale)
=end
def self.push(locale)
# only push changed translations
translations = Translation.where(locale: locale)
2015-04-27 06:20:52 +00:00
translations_to_push = []
translations.each {|translation|
if translation.target != translation.target_initial
translations_to_push.push translation
end
}
return true if translations_to_push.empty?
2015-06-28 00:16:47 +00:00
url = 'https://i18n.zammad.com/api/v1/thanks_for_your_support'
2015-04-27 06:20:52 +00:00
2015-06-28 00:16:47 +00:00
translator_key = Setting.get('translator_key')
2015-04-27 06:20:52 +00:00
result = UserAgent.post(
url,
{
locale: locale,
translations: translations_to_push,
fqdn: Setting.get('fqdn'),
2015-06-28 00:16:47 +00:00
translator_key: translator_key,
2015-04-27 06:20:52 +00:00
},
{
json: true,
2015-04-27 06:20:52 +00:00
}
)
fail "Can't push translations to #{url}: #{result.error}" if !result.success?
2015-06-28 00:16:47 +00:00
# set new translator_key if given
if result.data['translator_key']
translator_key = Setting.set('translator_key', result.data['translator_key'])
end
true
end
=begin
reset translations to origin
Translation.reset(locale)
=end
def self.reset(locale)
# only push changed translations
translations = Translation.where(locale: locale)
translations.each {|translation|
if !translation.target_initial || translation.target_initial.empty?
translation.destroy
elsif translation.target != translation.target_initial
translation.target = translation.target_initial
translation.save
end
}
2015-04-27 06:20:52 +00:00
true
end
=begin
get list of translations
2015-04-27 13:06:37 +00:00
list = Translation.list('de-de')
2015-04-27 06:20:52 +00:00
=end
def self.list(locale, admin = false)
2015-06-28 00:16:47 +00:00
# use cache if not admin page is requested
if !admin
2015-06-28 00:16:47 +00:00
data = cache_get(locale)
end
2015-06-28 00:16:47 +00:00
if !data
# show total translations as reference count
data = {
'total' => Translation.where(locale: 'de-de').count,
}
list = []
2015-06-28 00:16:47 +00:00
translations = Translation.where(locale: locale.downcase).order(:source)
translations.each { |item|
if admin
2015-06-28 00:16:47 +00:00
translation_item = [
item.id,
item.source,
item.target,
item.target_initial,
item.format,
]
2015-06-28 00:16:47 +00:00
list.push translation_item
else
2015-06-28 00:16:47 +00:00
translation_item = [
item.id,
item.source,
item.target,
item.format,
]
2015-06-28 00:16:47 +00:00
list.push translation_item
end
2015-06-28 00:16:47 +00:00
data['list'] = list
}
# set cache
if !admin
2015-06-28 00:16:47 +00:00
cache_set(locale, data)
end
end
2013-08-06 08:18:50 +00:00
2015-06-28 00:16:47 +00:00
data
2013-08-06 08:18:50 +00:00
end
2015-04-27 06:20:52 +00:00
=begin
translate strings in ruby context, e. g. for notifications
2015-04-27 13:06:37 +00:00
translated = Translation.translate('de-de', 'New')
2015-04-27 06:20:52 +00:00
=end
def self.translate(locale, string)
# translate string
records = Translation.where( locale: locale, source: string )
records.each {|record|
return record.target if record.source == string
}
# fallback lookup in en
records = Translation.where( locale: 'en', source: string )
records.each {|record|
return record.target if record.source == string
}
string
end
2012-05-18 14:24:00 +00:00
private
def set_initial
return if target_initial
self.target_initial = target
end
def cache_clear
2015-06-28 00:16:47 +00:00
Cache.delete( 'TranslationMap::' + locale.downcase )
end
def self.cache_set(locale, data)
2015-06-28 00:16:47 +00:00
Cache.write( 'TranslationMap::' + locale.downcase, data )
end
def self.cache_get(locale)
2015-06-28 00:16:47 +00:00
Cache.get( 'TranslationMap::' + locale.downcase )
end
2012-05-18 14:24:00 +00:00
end