translation_spec.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Translation do
  4. before(:all) do
  5. described_class.where(locale: 'de-de').destroy_all
  6. described_class.sync('de-de')
  7. end
  8. context 'default string translations' do
  9. it 'en with existing word' do
  10. expect(described_class.translate('en', 'New')).to eq('New')
  11. end
  12. it 'en-us with existing word' do
  13. expect(described_class.translate('en-us', 'New')).to eq('New')
  14. end
  15. it 'en with not existing word' do
  16. expect(described_class.translate('en', 'Some Not Existing Word')).to eq('Some Not Existing Word')
  17. end
  18. it 'de-de with existing word' do
  19. expect(described_class.translate('de-de', 'New')).to eq('Neu')
  20. end
  21. it 'de-de with not existing word' do
  22. expect(described_class.translate('de-de', 'Some Not Existing Word')).to eq('Some Not Existing Word')
  23. end
  24. end
  25. context 'default timestamp translations' do
  26. it 'de-de with array' do
  27. expect(described_class.timestamp('de-de', 'Europe/Berlin', ['some value'])).to eq('["some value"]')
  28. end
  29. it 'not_existing with timestamp as string' do
  30. expect(described_class.timestamp('not_existing', 'Europe/Berlin', '2018-10-10T10:00:00Z0')).to eq('2018-10-10 12:00:00 +0200')
  31. end
  32. it 'not_existing with time object' do
  33. expect(described_class.timestamp('not_existing', 'Europe/Berlin', Time.zone.parse('2018-10-10T10:00:00Z0'))).to eq('2018-10-10 12:00:00 +0200')
  34. end
  35. it 'not_existing with invalid timestamp string' do
  36. expect(described_class.timestamp('not_existing', 'Europe/Berlin', 'something')).to eq('something')
  37. end
  38. it 'en-us with invalid time zone' do
  39. expect(described_class.timestamp('en-us', 'Invalid/TimeZone', '2018-10-10T10:00:00Z0')).to eq(Time.zone.parse('2018-10-10T10:00:00Z0').to_s)
  40. end
  41. it 'en-us with timestamp as string' do
  42. expect(described_class.timestamp('en-us', 'Europe/Berlin', '2018-10-10T10:00:00Z0')).to eq('10/10/2018 12:00 (Europe/Berlin)')
  43. end
  44. it 'en-us with time object' do
  45. expect(described_class.timestamp('en-us', 'Europe/Berlin', Time.zone.parse('2018-10-10T10:00:00Z0'))).to eq('10/10/2018 12:00 (Europe/Berlin)')
  46. end
  47. it 'de-de with timestamp as string' do
  48. expect(described_class.timestamp('de-de', 'Europe/Berlin', '2018-10-10T10:00:00Z0')).to eq('10.10.2018 12:00 (Europe/Berlin)')
  49. end
  50. it 'de-de with time object' do
  51. expect(described_class.timestamp('de-de', 'Europe/Berlin', Time.zone.parse('2018-10-10T10:00:00Z0'))).to eq('10.10.2018 12:00 (Europe/Berlin)')
  52. end
  53. end
  54. context 'default date translations' do
  55. it 'de-de with array' do
  56. expect(described_class.date('de-de', ['some value'])).to eq('["some value"]')
  57. end
  58. it 'not_existing with date as string' do
  59. expect(described_class.date('not_existing', '2018-10-10')).to eq('2018-10-10')
  60. end
  61. it 'not_existing with date object' do
  62. expect(described_class.date('not_existing', Date.parse('2018-10-10'))).to eq('2018-10-10')
  63. end
  64. it 'not_existing with invalid data as string' do
  65. expect(described_class.date('not_existing', 'something')).to eq('something')
  66. end
  67. it 'en-us with date as string' do
  68. expect(described_class.date('en-us', '2018-10-10')).to eq('10/10/2018')
  69. end
  70. it 'en-us with date object' do
  71. expect(described_class.date('en-us', Date.parse('2018-10-10'))).to eq('10/10/2018')
  72. end
  73. it 'de-de with date as string' do
  74. expect(described_class.date('de-de', '2018-10-10')).to eq('10.10.2018')
  75. end
  76. it 'de-de with date object' do
  77. expect(described_class.date('de-de', Date.parse('2018-10-10'))).to eq('10.10.2018')
  78. end
  79. end
  80. context 'remote_translation_need_update? tests' do
  81. it 'translation is still the same' do
  82. translation = described_class.where(locale: 'de-de', format: 'string').last
  83. translations = described_class.where(locale: 'de-de').pluck(:id, :locale, :source, :format, :target, :target_initial).to_a
  84. expect(
  85. described_class.remote_translation_need_update?(
  86. {
  87. 'source' => translation.source,
  88. 'format' => translation.format,
  89. 'locale' => translation.locale,
  90. 'target' => translation.target,
  91. 'target_initial' => translation.target_initial,
  92. }, translations
  93. )
  94. ).to be false
  95. end
  96. it 'translation target has locally changed' do
  97. translation = described_class.where(locale: 'de-de', format: 'string').last
  98. translation.target = 'some new translation'
  99. translation.save!
  100. translations = described_class.where(locale: 'de-de').pluck(:id, :locale, :source, :format, :target, :target_initial).to_a
  101. expect(
  102. described_class.remote_translation_need_update?(
  103. {
  104. 'source' => translation.source,
  105. 'format' => translation.format,
  106. 'locale' => translation.locale,
  107. 'target' => translation.target,
  108. 'target_initial' => translation.target_initial,
  109. }, translations
  110. )
  111. ).to be false
  112. end
  113. it 'translation target has remotely changed' do
  114. translation = described_class.where(locale: 'de-de', format: 'string').last
  115. translations = described_class.where(locale: 'de-de').pluck(:id, :locale, :source, :format, :target, :target_initial).to_a
  116. (result, translation_result) = described_class.remote_translation_need_update?(
  117. {
  118. 'source' => translation.source,
  119. 'format' => translation.format,
  120. 'locale' => translation.locale,
  121. 'target' => 'some new translation by remote',
  122. 'target_initial' => 'some new translation by remote',
  123. }, translations
  124. )
  125. expect(result).to be true
  126. expect(translation_result.attributes).to eq translation.attributes
  127. end
  128. it 'translation target has remotely and locally changed' do
  129. translation = described_class.where(locale: 'de-de', format: 'string').last
  130. translation.target = 'some new translation'
  131. translation.save!
  132. translations = described_class.where(locale: 'de-de').pluck(:id, :locale, :source, :format, :target, :target_initial).to_a
  133. expect(
  134. described_class.remote_translation_need_update?(
  135. {
  136. 'source' => translation.source,
  137. 'format' => translation.format,
  138. 'locale' => translation.locale,
  139. 'target' => 'some new translation by remote',
  140. 'target_initial' => 'some new translation by remote',
  141. }, translations
  142. )
  143. ).to be false
  144. end
  145. end
  146. context 'custom translation tests' do
  147. it 'cycle of change and reload translation' do
  148. locale = 'de-de'
  149. # check for non existing custom changes
  150. list = described_class.lang(locale)
  151. list['list'].each do |item|
  152. translation = described_class.find_by(source: item[1], locale: locale)
  153. expect(translation.class).to be(described_class)
  154. expect(locale).to eq(translation.locale)
  155. expect(translation.target).to eq(translation.target_initial)
  156. end
  157. # add custom changes
  158. translation = described_class.find_by(locale: locale, source: 'open')
  159. expect(translation.class).to be(described_class)
  160. expect(translation.target).to eq('offen')
  161. expect(translation.target_initial).to eq('offen')
  162. translation.target = 'offen2'
  163. translation.save!
  164. list = described_class.lang(locale)
  165. list['list'].each do |item|
  166. translation = described_class.find_by(source: item[1], locale: locale)
  167. expect(translation.class).to be(described_class)
  168. expect(locale).to eq(translation.locale)
  169. if translation.source == 'open'
  170. expect(translation.target).to eq('offen2')
  171. expect(translation.target_initial).to eq('offen')
  172. else
  173. expect(translation.target).to eq(translation.target_initial)
  174. end
  175. end
  176. # check for existing custom changes after new translations are loaded
  177. described_class.load(locale)
  178. list = described_class.lang(locale)
  179. list['list'].each do |item|
  180. translation = described_class.find_by(source: item[1], locale: locale)
  181. expect(translation.class).to be(described_class)
  182. expect(locale).to eq(translation.locale)
  183. if translation.source == 'open'
  184. expect(translation.target).to eq('offen2')
  185. expect(translation.target_initial).to eq('offen')
  186. else
  187. expect(translation.target).to eq(translation.target_initial)
  188. end
  189. end
  190. # reset custom translations and check for non existing custom changes
  191. described_class.reset(locale)
  192. list = described_class.lang(locale)
  193. list['list'].each do |item|
  194. translation = described_class.find_by(source: item[1], locale: locale)
  195. expect(translation.class).to be(described_class)
  196. expect(locale).to eq(translation.locale)
  197. expect(translation.target).to eq(translation.target_initial)
  198. end
  199. end
  200. end
  201. context 'file based import' do
  202. it 'check download of locales' do
  203. version = Version.get
  204. directory = Rails.root.join('config')
  205. file = Rails.root.join(directory, "locales-#{version}.yml")
  206. if File.exist?(file)
  207. File.delete(file)
  208. end
  209. expect(File.exist?(file)).to be false
  210. Locale.fetch
  211. expect(File.exist?(file)).to be true
  212. end
  213. it 'check download of translations' do
  214. version = Version.get
  215. locale = 'de-de'
  216. directory = Rails.root.join('config/translations')
  217. if File.directory?(directory)
  218. FileUtils.rm_rf(directory)
  219. end
  220. file = Rails.root.join(directory, "#{locale}-#{version}.yml")
  221. expect(File.exist?(file)).to be false
  222. described_class.fetch(locale)
  223. expect(File.exist?(file)).to be true
  224. end
  225. end
  226. context 'sync duplicate tests' do
  227. it 'check duplication of entries' do
  228. described_class.where(locale: 'de-de').destroy_all
  229. described_class.sync('de-de')
  230. translation_count = described_class.where(locale: 'de-de').count
  231. described_class.sync('de-de')
  232. expect(
  233. described_class.where(locale: 'de-de').count
  234. ).to be translation_count
  235. end
  236. end
  237. end