2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
class Translation < ApplicationModel
|
2012-05-18 14:24:00 +00:00
|
|
|
before_create :set_initial
|
2013-08-06 14:17:30 +00:00
|
|
|
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 11:57:36 +00:00
|
|
|
raise "Can't load translations from #{url}: #{result.error}" if !result.success?
|
2015-04-27 06:20:52 +00:00
|
|
|
result.data.each {|translation|
|
|
|
|
#puts translation.inspect
|
2015-04-27 11:47:48 +00:00
|
|
|
|
|
|
|
# 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
|
2015-04-27 06:20:52 +00:00
|
|
|
|
|
|
|
# verify if update is needed
|
2015-04-27 11:47:48 +00:00
|
|
|
translaten.update_attributes(translation.symbolize_keys!)
|
|
|
|
translaten.save
|
2015-04-27 06:20:52 +00:00
|
|
|
else
|
|
|
|
Translation.create(translation.symbolize_keys!)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
push translations to online
|
|
|
|
|
|
|
|
Translation.push(locale)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.push(locale)
|
|
|
|
|
2015-04-27 11:47:48 +00:00
|
|
|
# only push changed translations
|
2015-04-27 06:20:52 +00:00
|
|
|
translations = Translation.where(:locale => locale)
|
|
|
|
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?
|
|
|
|
#return translations_to_push
|
2015-04-27 11:47:48 +00:00
|
|
|
url = 'https://i18n.zammad.com/api/v1/thanks_for_your_support'
|
2015-04-27 06:20:52 +00:00
|
|
|
|
|
|
|
result = UserAgent.post(
|
|
|
|
url,
|
|
|
|
{
|
|
|
|
:locale => locale,
|
|
|
|
:translations => translations_to_push,
|
|
|
|
:fqdn => Setting.get('fqdn'),
|
|
|
|
:translator_key => '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:json => true,
|
|
|
|
}
|
|
|
|
)
|
2015-04-27 11:57:36 +00:00
|
|
|
raise "Can't push translations to #{url}: #{result.error}" if !result.success?
|
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
|
|
|
|
|
2015-04-12 08:41:59 +00:00
|
|
|
def self.list(locale, admin = false)
|
2013-08-06 14:17:30 +00:00
|
|
|
|
|
|
|
# check cache
|
2015-04-12 08:41:59 +00:00
|
|
|
if !admin
|
|
|
|
list = cache_get( locale )
|
|
|
|
end
|
2013-08-06 14:17:30 +00:00
|
|
|
if !list
|
|
|
|
list = []
|
2015-04-12 08:41:59 +00:00
|
|
|
translations = Translation.where( :locale => locale.downcase ).order( :source )
|
2013-08-06 14:17:30 +00:00
|
|
|
translations.each { |item|
|
2015-04-12 08:41:59 +00:00
|
|
|
if admin
|
|
|
|
data = [
|
|
|
|
item.id,
|
|
|
|
item.source,
|
|
|
|
item.target,
|
|
|
|
item.target_initial,
|
2015-04-27 11:47:48 +00:00
|
|
|
item.format,
|
2015-04-12 08:41:59 +00:00
|
|
|
]
|
|
|
|
list.push data
|
|
|
|
else
|
|
|
|
data = [
|
|
|
|
item.id,
|
|
|
|
item.source,
|
|
|
|
item.target,
|
2015-04-27 11:47:48 +00:00
|
|
|
item.format,
|
2015-04-12 08:41:59 +00:00
|
|
|
]
|
|
|
|
list.push data
|
|
|
|
end
|
2013-08-06 14:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# set cache
|
2015-04-12 08:41:59 +00:00
|
|
|
if !admin
|
|
|
|
cache_set( locale, list )
|
|
|
|
end
|
2013-08-06 14:17:30 +00:00
|
|
|
end
|
2013-08-06 08:18:50 +00:00
|
|
|
|
|
|
|
return {
|
2015-04-27 11:47:48 +00:00
|
|
|
:list => list,
|
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
|
|
|
|
|
2013-01-04 13:14:20 +00:00
|
|
|
def self.translate(locale, string)
|
|
|
|
|
|
|
|
# translate string
|
2013-01-04 14:28:55 +00:00
|
|
|
records = Translation.where( :locale => locale, :source => string )
|
|
|
|
records.each {|record|
|
|
|
|
return record.target if record.source == string
|
|
|
|
}
|
2013-01-04 13:14:20 +00:00
|
|
|
|
|
|
|
# fallback lookup in en
|
2013-01-04 14:28:55 +00:00
|
|
|
records = Translation.where( :locale => 'en', :source => string )
|
|
|
|
records.each {|record|
|
|
|
|
return record.target if record.source == string
|
|
|
|
}
|
2013-01-04 13:14:20 +00:00
|
|
|
|
|
|
|
return string
|
|
|
|
end
|
|
|
|
|
2012-05-18 14:24:00 +00:00
|
|
|
private
|
2013-06-12 15:59:58 +00:00
|
|
|
def set_initial
|
2015-04-27 06:20:52 +00:00
|
|
|
if !target_initial
|
|
|
|
self.target_initial = self.target
|
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2013-08-06 14:17:30 +00:00
|
|
|
def cache_clear
|
|
|
|
Cache.delete( 'Translation::' + self.locale.downcase )
|
|
|
|
end
|
|
|
|
def self.cache_set(locale, data)
|
|
|
|
Cache.write( 'Translation::' + locale.downcase, data )
|
|
|
|
end
|
|
|
|
def self.cache_get(locale)
|
|
|
|
Cache.get( 'Translation::' + locale.downcase )
|
|
|
|
end
|
2012-05-18 14:24:00 +00:00
|
|
|
end
|