translation.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. date_map_default = 'yyyy-mm-dd'
  30. date_map = {
  31. :de => 'dd.mm.yyyy',
  32. }
  33. date = date_map[ locale.to_sym ] || date_map_default
  34. return {
  35. :list => list,
  36. :timestampFormat => timestamp,
  37. :dateFormat => date,
  38. }
  39. end
  40. def self.translate(locale, string)
  41. # translate string
  42. records = Translation.where( :locale => locale, :source => string )
  43. records.each {|record|
  44. return record.target if record.source == string
  45. }
  46. # fallback lookup in en
  47. records = Translation.where( :locale => 'en', :source => string )
  48. records.each {|record|
  49. return record.target if record.source == string
  50. }
  51. return string
  52. end
  53. private
  54. def set_initial
  55. self.target_initial = self.target
  56. end
  57. def cache_clear
  58. Cache.delete( 'Translation::' + self.locale.downcase )
  59. end
  60. def self.cache_set(locale, data)
  61. Cache.write( 'Translation::' + locale.downcase, data )
  62. end
  63. def self.cache_get(locale)
  64. Cache.get( 'Translation::' + locale.downcase )
  65. end
  66. end