translation.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. =begin
  8. load translations from online
  9. Translation.load
  10. =end
  11. def self.load
  12. url = 'https://i18n.zammad.com/api/v1/translations'
  13. if !UserInfo.current_user_id
  14. UserInfo.current_user_id = 1
  15. end
  16. result = UserAgent.get(
  17. url,
  18. {},
  19. {
  20. json: true,
  21. }
  22. )
  23. raise "Can't load translations from #{url}: #{result.error}" if !result.success?
  24. ActiveRecord::Base.transaction do
  25. result.data.each {|translation|
  26. #puts translation.inspect
  27. # handle case insensitive sql
  28. exists = Translation.where(locale: translation['locale'], format: translation['format'], source: translation['source'])
  29. translaten = nil
  30. exists.each {|item|
  31. if item.source == translation['source']
  32. translaten = item
  33. end
  34. }
  35. if translaten
  36. # verify if update is needed
  37. translaten.update_attributes(translation.symbolize_keys!)
  38. translaten.save
  39. else
  40. Translation.create(translation.symbolize_keys!)
  41. end
  42. }
  43. end
  44. true
  45. end
  46. =begin
  47. push translations to online
  48. Translation.push(locale)
  49. =end
  50. def self.push(locale)
  51. # only push changed translations
  52. translations = Translation.where(locale: locale)
  53. translations_to_push = []
  54. translations.each {|translation|
  55. if translation.target != translation.target_initial
  56. translations_to_push.push translation
  57. end
  58. }
  59. return true if translations_to_push.empty?
  60. #return translations_to_push
  61. url = 'https://i18n.zammad.com/api/v1/thanks_for_your_support'
  62. result = UserAgent.post(
  63. url,
  64. {
  65. locale: locale,
  66. translations: translations_to_push,
  67. fqdn: Setting.get('fqdn'),
  68. translator_key: '',
  69. },
  70. {
  71. json: true,
  72. }
  73. )
  74. raise "Can't push translations to #{url}: #{result.error}" if !result.success?
  75. true
  76. end
  77. =begin
  78. get list of translations
  79. list = Translation.list('de-de')
  80. =end
  81. def self.list(locale, admin = false)
  82. # check cache
  83. if !admin
  84. list = cache_get( locale )
  85. end
  86. if !list
  87. list = []
  88. translations = Translation.where( locale: locale.downcase ).order( :source )
  89. translations.each { |item|
  90. if admin
  91. data = [
  92. item.id,
  93. item.source,
  94. item.target,
  95. item.target_initial,
  96. item.format,
  97. ]
  98. list.push data
  99. else
  100. data = [
  101. item.id,
  102. item.source,
  103. item.target,
  104. item.format,
  105. ]
  106. list.push data
  107. end
  108. }
  109. # set cache
  110. if !admin
  111. cache_set( locale, list )
  112. end
  113. end
  114. {
  115. list: list,
  116. }
  117. end
  118. =begin
  119. translate strings in ruby context, e. g. for notifications
  120. translated = Translation.translate('de-de', 'New')
  121. =end
  122. def self.translate(locale, string)
  123. # translate string
  124. records = Translation.where( locale: locale, source: string )
  125. records.each {|record|
  126. return record.target if record.source == string
  127. }
  128. # fallback lookup in en
  129. records = Translation.where( locale: 'en', source: string )
  130. records.each {|record|
  131. return record.target if record.source == string
  132. }
  133. string
  134. end
  135. private
  136. def set_initial
  137. return if target_initial
  138. self.target_initial = self.target
  139. end
  140. def cache_clear
  141. Cache.delete( 'Translation::' + self.locale.downcase )
  142. end
  143. def self.cache_set(locale, data)
  144. Cache.write( 'Translation::' + locale.downcase, data )
  145. end
  146. def self.cache_get(locale)
  147. Cache.get( 'Translation::' + locale.downcase )
  148. end
  149. end