translation_spec.rb 10 KB

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