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-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,
|
|
|
|
]
|
|
|
|
list.push data
|
|
|
|
else
|
|
|
|
data = [
|
|
|
|
item.id,
|
|
|
|
item.source,
|
|
|
|
item.target,
|
|
|
|
]
|
|
|
|
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
|
|
|
|
|
|
|
timestamp_map_default = 'yyyy-mm-dd HH:MM'
|
|
|
|
timestamp_map = {
|
|
|
|
:de => 'dd.mm.yyyy HH:MM',
|
|
|
|
}
|
|
|
|
timestamp = timestamp_map[ locale.to_sym ] || timestamp_map_default
|
2014-09-10 12:19:44 +00:00
|
|
|
|
|
|
|
date_map_default = 'yyyy-mm-dd'
|
|
|
|
date_map = {
|
|
|
|
:de => 'dd.mm.yyyy',
|
|
|
|
}
|
|
|
|
date = date_map[ locale.to_sym ] || date_map_default
|
|
|
|
|
2013-08-06 08:18:50 +00:00
|
|
|
return {
|
|
|
|
:list => list,
|
|
|
|
:timestampFormat => timestamp,
|
2014-09-10 12:19:44 +00:00
|
|
|
:dateFormat => date,
|
2013-08-06 08:18:50 +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
|
|
|
|
self.target_initial = self.target
|
|
|
|
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
|