translation.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class Translation < ApplicationModel
  3. before_create :set_initial
  4. after_create :cache_clear
  5. after_update :cache_clear
  6. after_destroy :cache_clear
  7. def self.list(locale)
  8. # check cache
  9. list = cache_get( locale )
  10. if !list
  11. list = []
  12. translations = Translation.where( :locale => locale.downcase )
  13. translations.each { |item|
  14. data = [
  15. item.id,
  16. item.source,
  17. item.target,
  18. ]
  19. list.push data
  20. }
  21. # set cache
  22. cache_set( locale, list )
  23. end
  24. timestamp_map_default = 'yyyy-mm-dd HH:MM'
  25. timestamp_map = {
  26. :de => 'dd.mm.yyyy HH:MM',
  27. }
  28. timestamp = timestamp_map[ locale.to_sym ] || timestamp_map_default
  29. return {
  30. :list => list,
  31. :timestampFormat => timestamp,
  32. }
  33. end
  34. def self.translate(locale, string)
  35. # translate string
  36. records = Translation.where( :locale => locale, :source => string )
  37. records.each {|record|
  38. return record.target if record.source == string
  39. }
  40. # fallback lookup in en
  41. records = Translation.where( :locale => 'en', :source => string )
  42. records.each {|record|
  43. return record.target if record.source == string
  44. }
  45. return string
  46. end
  47. private
  48. def set_initial
  49. self.target_initial = self.target
  50. end
  51. def cache_clear
  52. Cache.delete( 'Translation::' + self.locale.downcase )
  53. end
  54. def self.cache_set(locale, data)
  55. Cache.write( 'Translation::' + locale.downcase, data )
  56. end
  57. def self.cache_get(locale)
  58. Cache.get( 'Translation::' + locale.downcase )
  59. end
  60. end