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
|
|
|
|
|
2015-09-21 12:20:36 +00:00
|
|
|
all:
|
|
|
|
|
2015-04-27 06:20:52 +00:00
|
|
|
Translation.load
|
|
|
|
|
2015-09-21 12:20:36 +00:00
|
|
|
dedicated:
|
|
|
|
|
|
|
|
Translation.load(locale) # e. g. en-us or de-de
|
|
|
|
|
2015-04-27 06:20:52 +00:00
|
|
|
=end
|
|
|
|
|
2015-09-21 12:20:36 +00:00
|
|
|
def self.load(dedicated_locale = nil)
|
|
|
|
locales_list = []
|
|
|
|
if !dedicated_locale
|
|
|
|
locales = Locale.to_sync
|
|
|
|
locales.each {|locale|
|
|
|
|
locales_list.push locale.locale
|
|
|
|
}
|
|
|
|
else
|
|
|
|
locales_list = [dedicated_locale]
|
2015-08-15 07:48:08 +00:00
|
|
|
end
|
2015-09-21 12:20:36 +00:00
|
|
|
locales_list.each {|locale|
|
|
|
|
url = "https://i18n.zammad.com/api/v1/translations/#{locale}"
|
2015-08-04 10:41:36 +00:00
|
|
|
if !UserInfo.current_user_id
|
|
|
|
UserInfo.current_user_id = 1
|
|
|
|
end
|
|
|
|
result = UserAgent.get(
|
|
|
|
url,
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
json: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
fail "Can't load translations from #{url}: #{result.error}" if !result.success?
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2015-10-28 12:24:52 +00:00
|
|
|
result.data.each {|translation_raw|
|
2015-08-04 10:41:36 +00:00
|
|
|
|
|
|
|
# handle case insensitive sql
|
2015-10-28 12:24:52 +00:00
|
|
|
exists = Translation.where(locale: translation_raw['locale'], format: translation_raw['format'], source: translation_raw['source'])
|
|
|
|
translation = nil
|
2015-08-04 10:41:36 +00:00
|
|
|
exists.each {|item|
|
2015-10-28 12:24:52 +00:00
|
|
|
if item.source == translation_raw['source']
|
|
|
|
translation = item
|
2015-08-04 10:41:36 +00:00
|
|
|
end
|
|
|
|
}
|
2015-10-28 12:24:52 +00:00
|
|
|
if translation
|
2015-08-04 10:41:36 +00:00
|
|
|
|
|
|
|
# verify if update is needed
|
2015-10-28 12:24:52 +00:00
|
|
|
update_needed = false
|
|
|
|
translation_raw.each {|key, _value|
|
2015-10-28 12:26:41 +00:00
|
|
|
if translation_raw[key] != translation[key]
|
2015-10-28 12:24:52 +00:00
|
|
|
update_needed = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
if update_needed
|
|
|
|
translation.update_attributes(translation_raw.symbolize_keys!)
|
|
|
|
translation.save
|
|
|
|
end
|
2015-08-04 10:41:36 +00:00
|
|
|
else
|
2015-10-28 12:24:52 +00:00
|
|
|
Translation.create(translation_raw.symbolize_keys!)
|
2015-04-28 21:08:39 +00:00
|
|
|
end
|
|
|
|
}
|
2015-08-04 10:41:36 +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)
|
|
|
|
|
2015-04-27 11:47:48 +00:00
|
|
|
# only push changed translations
|
2015-04-27 13:42:53 +00:00
|
|
|
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
|
|
|
|
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
|
|
|
|
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,
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
json: true,
|
2015-04-27 06:20:52 +00:00
|
|
|
}
|
|
|
|
)
|
2015-05-07 11:27:07 +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
|
|
|
|
|
2015-04-12 08:41:59 +00:00
|
|
|
def self.list(locale, admin = false)
|
2013-08-06 14:17:30 +00:00
|
|
|
|
2015-06-28 00:16:47 +00:00
|
|
|
# use cache if not admin page is requested
|
2015-04-12 08:41:59 +00:00
|
|
|
if !admin
|
2015-06-28 00:16:47 +00:00
|
|
|
data = cache_get(locale)
|
2015-04-12 08:41:59 +00:00
|
|
|
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,
|
|
|
|
}
|
2013-08-06 14:17:30 +00:00
|
|
|
list = []
|
2015-06-28 00:16:47 +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
|
2015-06-28 00:16:47 +00:00
|
|
|
translation_item = [
|
2015-04-12 08:41:59 +00:00
|
|
|
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
|
|
|
]
|
2015-06-28 00:16:47 +00:00
|
|
|
list.push translation_item
|
2015-04-12 08:41:59 +00:00
|
|
|
else
|
2015-06-28 00:16:47 +00:00
|
|
|
translation_item = [
|
2015-04-12 08:41:59 +00:00
|
|
|
item.id,
|
|
|
|
item.source,
|
|
|
|
item.target,
|
2015-04-27 11:47:48 +00:00
|
|
|
item.format,
|
2015-04-12 08:41:59 +00:00
|
|
|
]
|
2015-06-28 00:16:47 +00:00
|
|
|
list.push translation_item
|
2015-04-12 08:41:59 +00:00
|
|
|
end
|
2015-06-28 00:16:47 +00:00
|
|
|
data['list'] = list
|
2013-08-06 14:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# set cache
|
2015-04-12 08:41:59 +00:00
|
|
|
if !admin
|
2015-06-28 00:16:47 +00:00
|
|
|
cache_set(locale, data)
|
2015-04-12 08:41:59 +00:00
|
|
|
end
|
2013-08-06 14:17:30 +00:00
|
|
|
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
|
|
|
|
|
2013-01-04 13:14:20 +00:00
|
|
|
def self.translate(locale, string)
|
|
|
|
|
|
|
|
# translate string
|
2015-04-27 13:42:53 +00:00
|
|
|
records = Translation.where( locale: locale, source: string )
|
2013-01-04 14:28:55 +00:00
|
|
|
records.each {|record|
|
|
|
|
return record.target if record.source == string
|
|
|
|
}
|
2013-01-04 13:14:20 +00:00
|
|
|
|
|
|
|
# fallback lookup in en
|
2015-04-27 13:42:53 +00:00
|
|
|
records = Translation.where( locale: 'en', source: string )
|
2013-01-04 14:28:55 +00:00
|
|
|
records.each {|record|
|
|
|
|
return record.target if record.source == string
|
|
|
|
}
|
2013-01-04 13:14:20 +00:00
|
|
|
|
2015-04-30 17:20:27 +00:00
|
|
|
string
|
2013-01-04 13:14:20 +00:00
|
|
|
end
|
|
|
|
|
2012-05-18 14:24:00 +00:00
|
|
|
private
|
2015-05-01 12:31:46 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def set_initial
|
2015-04-30 15:25:04 +00:00
|
|
|
|
|
|
|
return if target_initial
|
2015-05-07 12:10:38 +00:00
|
|
|
self.target_initial = target
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2013-08-06 14:17:30 +00:00
|
|
|
def cache_clear
|
2015-06-28 00:16:47 +00:00
|
|
|
Cache.delete( 'TranslationMap::' + locale.downcase )
|
2013-08-06 14:17:30 +00:00
|
|
|
end
|
2015-09-09 18:16:20 +00:00
|
|
|
|
2013-08-06 14:17:30 +00:00
|
|
|
def self.cache_set(locale, data)
|
2015-06-28 00:16:47 +00:00
|
|
|
Cache.write( 'TranslationMap::' + locale.downcase, data )
|
2013-08-06 14:17:30 +00:00
|
|
|
end
|
2015-09-09 18:16:20 +00:00
|
|
|
|
2013-08-06 14:17:30 +00:00
|
|
|
def self.cache_get(locale)
|
2015-06-28 00:16:47 +00:00
|
|
|
Cache.get( 'TranslationMap::' + locale.downcase )
|
2013-08-06 14:17:30 +00:00
|
|
|
end
|
2012-05-18 14:24:00 +00:00
|
|
|
end
|