translation.rb 5.0 KB

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