translation_spec.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. require 'rails_helper'
  2. RSpec.describe Translation do
  3. before(:all) do
  4. Translation.where(locale: 'de-de').destroy_all
  5. Translation.sync('de-de')
  6. end
  7. context 'default translations' do
  8. it 'en with existing word' do
  9. expect(Translation.translate('en', 'New')).to eq('New')
  10. end
  11. it 'en-us with existing word' do
  12. expect(Translation.translate('en-us', 'New')).to eq('New')
  13. end
  14. it 'en with not existing word' do
  15. expect(Translation.translate('en', 'Some Not Existing Word')).to eq('Some Not Existing Word')
  16. end
  17. it 'de-de with existing word' do
  18. expect(Translation.translate('de-de', 'New')).to eq('Neu')
  19. end
  20. it 'de-de with existing word' do
  21. expect(Translation.translate('de-de', 'Some Not Existing Word')).to eq('Some Not Existing Word')
  22. end
  23. end
  24. context 'remote_translation_need_update? tests' do
  25. it 'translation is still the same' do
  26. translation = Translation.where(locale: 'de-de', format: 'string').last
  27. translations = Translation.where(locale: 'de-de').pluck(:id, :locale, :source, :format, :target, :target_initial).to_a
  28. expect(
  29. Translation.remote_translation_need_update?(
  30. {
  31. 'source' => translation.source,
  32. 'format' => translation.format,
  33. 'locale' => translation.locale,
  34. 'target' => translation.target,
  35. 'target_initial' => translation.target_initial,
  36. }, translations
  37. )
  38. ).to be false
  39. end
  40. it 'translation target has locally changed' do
  41. translation = Translation.where(locale: 'de-de', format: 'string').last
  42. translation.target = 'some new translation'
  43. translation.save!
  44. translations = Translation.where(locale: 'de-de').pluck(:id, :locale, :source, :format, :target, :target_initial).to_a
  45. expect(
  46. Translation.remote_translation_need_update?(
  47. {
  48. 'source' => translation.source,
  49. 'format' => translation.format,
  50. 'locale' => translation.locale,
  51. 'target' => translation.target,
  52. 'target_initial' => translation.target_initial,
  53. }, translations
  54. )
  55. ).to be false
  56. end
  57. it 'translation target has remotely changed' do
  58. translation = Translation.where(locale: 'de-de', format: 'string').last
  59. translations = Translation.where(locale: 'de-de').pluck(:id, :locale, :source, :format, :target, :target_initial).to_a
  60. (result, translation_result) = Translation.remote_translation_need_update?(
  61. {
  62. 'source' => translation.source,
  63. 'format' => translation.format,
  64. 'locale' => translation.locale,
  65. 'target' => 'some new translation by remote',
  66. 'target_initial' => 'some new translation by remote',
  67. }, translations
  68. )
  69. expect(result).to be true
  70. expect(translation_result.attributes).to eq translation.attributes
  71. end
  72. it 'translation target has remotely and locally changed' do
  73. translation = Translation.where(locale: 'de-de', format: 'string').last
  74. translation.target = 'some new translation'
  75. translation.save!
  76. translations = Translation.where(locale: 'de-de').pluck(:id, :locale, :source, :format, :target, :target_initial).to_a
  77. expect(
  78. Translation.remote_translation_need_update?(
  79. {
  80. 'source' => translation.source,
  81. 'format' => translation.format,
  82. 'locale' => translation.locale,
  83. 'target' => 'some new translation by remote',
  84. 'target_initial' => 'some new translation by remote',
  85. }, translations
  86. )
  87. ).to be false
  88. end
  89. end
  90. context 'custom translation tests' do
  91. it 'cycle of change and reload translation' do
  92. locale = 'de-de'
  93. # check for non existing custom changes
  94. list = Translation.lang(locale)
  95. list['list'].each do |item|
  96. translation = Translation.find_by(source: item[1], locale: locale)
  97. expect(translation.class).to be(Translation)
  98. expect(locale).to eq(translation.locale)
  99. expect(translation.target).to eq(translation.target_initial)
  100. end
  101. # add custom changes
  102. translation = Translation.find_by(locale: locale, source: 'open')
  103. expect(translation.class).to be(Translation)
  104. expect(translation.target).to eq('offen')
  105. expect(translation.target_initial).to eq('offen')
  106. translation.target = 'offen2'
  107. translation.save!
  108. list = Translation.lang(locale)
  109. list['list'].each do |item|
  110. translation = Translation.find_by(source: item[1], locale: locale)
  111. expect(translation.class).to be(Translation)
  112. expect(locale).to eq(translation.locale)
  113. if translation.source == 'open'
  114. expect(translation.target).to eq('offen2')
  115. expect(translation.target_initial).to eq('offen')
  116. else
  117. expect(translation.target).to eq(translation.target_initial)
  118. end
  119. end
  120. # check for existing custom changes after new translations are loaded
  121. Translation.load(locale)
  122. list = Translation.lang(locale)
  123. list['list'].each do |item|
  124. translation = Translation.find_by(source: item[1], locale: locale)
  125. expect(translation.class).to be(Translation)
  126. expect(locale).to eq(translation.locale)
  127. if translation.source == 'open'
  128. expect(translation.target).to eq('offen2')
  129. expect(translation.target_initial).to eq('offen')
  130. else
  131. expect(translation.target).to eq(translation.target_initial)
  132. end
  133. end
  134. # reset custom translations and check for non existing custom changes
  135. Translation.reset(locale)
  136. list = Translation.lang(locale)
  137. list['list'].each do |item|
  138. translation = Translation.find_by(source: item[1], locale: locale)
  139. expect(translation.class).to be(Translation)
  140. expect(locale).to eq(translation.locale)
  141. expect(translation.target).to eq(translation.target_initial)
  142. end
  143. end
  144. end
  145. context 'file based import' do
  146. it 'check download of locales' do
  147. version = Version.get
  148. directory = Rails.root.join('config')
  149. file = Rails.root.join(directory, "locales-#{version}.yml")
  150. if File.exist?(file)
  151. File.delete(file)
  152. end
  153. expect(File.exist?(file)).to be false
  154. Locale.fetch
  155. expect(File.exist?(file)).to be true
  156. end
  157. it 'check download of translations' do
  158. version = Version.get
  159. locale = 'de-de'
  160. directory = Rails.root.join('config', 'translations')
  161. if File.directory?(directory)
  162. FileUtils.rm_rf(directory)
  163. end
  164. file = Rails.root.join(directory, "#{locale}-#{version}.yml")
  165. expect(File.exist?(file)).to be false
  166. Translation.fetch(locale)
  167. expect(File.exist?(file)).to be true
  168. end
  169. end
  170. context 'sync duplicate tests' do
  171. it 'check duplication of entries' do
  172. Translation.where(locale: 'de-de').destroy_all
  173. Translation.sync('de-de')
  174. translation_count = Translation.where(locale: 'de-de').count
  175. Translation.sync('de-de')
  176. expect(
  177. Translation.where(locale: 'de-de').count
  178. ).to be translation_count
  179. end
  180. end
  181. end