translation_spec.rb 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Translation do
  4. before(:all) do # rubocop:disable RSpec/BeforeAfterAll
  5. described_class.where(locale: 'de-de').destroy_all
  6. described_class.sync_locale_from_po('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. it 'format string with given arguments' do
  25. expect(described_class.translate('en', 'a %s string', 'given')).to eq('a given string')
  26. end
  27. end
  28. context 'when using find_source' do
  29. it 'de-de with existing title case word' do
  30. expect(described_class.find_source('de-de', 'New')).to have_attributes(source: 'New', target_initial: 'Neu', target: 'Neu')
  31. end
  32. it 'de-de with existing lower case word' do
  33. expect(described_class.find_source('de-de', 'new')).to have_attributes(source: 'new', target_initial: 'neu', target: 'neu')
  34. end
  35. it 'de-de with nonexisting existing source' do
  36. expect(described_class.find_source('de-de', 'nonexisting-string')).to be_nil
  37. end
  38. end
  39. context 'default timestamp translations' do
  40. it 'de-de with array' do
  41. expect(described_class.timestamp('de-de', 'Europe/Berlin', ['some value'])).to eq('["some value"]')
  42. end
  43. it 'not_existing with timestamp as string' do
  44. expect(described_class.timestamp('not_existing', 'Europe/Berlin', '2018-10-10T10:00:00Z0')).to eq('2018-10-10 12:00:00 +0200')
  45. end
  46. it 'not_existing with time object' do
  47. 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')
  48. end
  49. it 'not_existing with invalid timestamp string' do
  50. expect(described_class.timestamp('not_existing', 'Europe/Berlin', 'something')).to eq('something')
  51. end
  52. it 'en-us with invalid time zone' do
  53. 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)
  54. end
  55. it 'en-us with timestamp as string (am)' do
  56. expect(described_class.timestamp('en-us', 'Europe/Berlin', '2018-10-10T01:00:00Z0')).to eq('10/10/2018 3:00 am (Europe/Berlin)')
  57. end
  58. it 'en-us with timestamp as string (pm)' do
  59. expect(described_class.timestamp('en-us', 'Europe/Berlin', '2018-10-10T10:00:00Z0')).to eq('10/10/2018 12:00 pm (Europe/Berlin)')
  60. end
  61. it 'en-us with time object (am)' do
  62. expect(described_class.timestamp('en-us', 'Europe/Berlin', Time.zone.parse('2018-10-10T01:00:00Z0'))).to eq('10/10/2018 3:00 am (Europe/Berlin)')
  63. end
  64. it 'en-us with time object (pm)' do
  65. expect(described_class.timestamp('en-us', 'Europe/Berlin', Time.zone.parse('2018-10-10T10:00:00Z0'))).to eq('10/10/2018 12:00 pm (Europe/Berlin)')
  66. end
  67. it 'de-de with timestamp as string' do
  68. expect(described_class.timestamp('de-de', 'Europe/Berlin', '2018-10-10T10:00:00Z0')).to eq('10.10.2018 12:00 (Europe/Berlin)')
  69. end
  70. it 'de-de with time object' do
  71. 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)')
  72. end
  73. end
  74. context 'default date translations' do
  75. it 'de-de with array' do
  76. expect(described_class.date('de-de', ['some value'])).to eq('["some value"]')
  77. end
  78. it 'not_existing with date as string' do
  79. expect(described_class.date('not_existing', '2018-10-10')).to eq('2018-10-10')
  80. end
  81. it 'not_existing with date object' do
  82. expect(described_class.date('not_existing', Date.parse('2018-10-10'))).to eq('2018-10-10')
  83. end
  84. it 'not_existing with invalid data as string' do
  85. expect(described_class.date('not_existing', 'something')).to eq('something')
  86. end
  87. it 'en-us with date as string' do
  88. expect(described_class.date('en-us', '2018-10-10')).to eq('10/10/2018')
  89. end
  90. it 'en-us with date object' do
  91. expect(described_class.date('en-us', Date.parse('2018-10-10'))).to eq('10/10/2018')
  92. end
  93. it 'de-de with date as string' do
  94. expect(described_class.date('de-de', '2018-10-10')).to eq('10.10.2018')
  95. end
  96. it 'de-de with date object' do
  97. expect(described_class.date('de-de', Date.parse('2018-10-10'))).to eq('10.10.2018')
  98. end
  99. end
  100. context 'custom translation tests' do
  101. it 'cycle of change and reload translation' do
  102. locale = 'de-de'
  103. # check for non existing custom changes
  104. list = described_class.lang(locale)
  105. list['list'].each do |item|
  106. translation = described_class.find_source(locale, item[1])
  107. expect(translation.class).to be(described_class)
  108. expect(locale).to eq(translation.locale)
  109. expect(translation.target).to eq(translation.target_initial)
  110. end
  111. # add custom changes
  112. translation = described_class.find_source(locale, 'open')
  113. expect(translation.class).to be(described_class)
  114. expect(translation.target).to eq('offen')
  115. expect(translation.target_initial).to eq('offen')
  116. translation.target = 'offen2'
  117. translation.save!
  118. list = described_class.lang(locale)
  119. list['list'].each do |item|
  120. translation = described_class.find_source(locale, item[1])
  121. expect(translation.class).to be(described_class)
  122. expect(locale).to eq(translation.locale)
  123. if translation.source == 'open'
  124. expect(translation.target).to eq('offen2')
  125. expect(translation.target_initial).to eq('offen')
  126. else
  127. expect(translation.target).to eq(translation.target_initial)
  128. end
  129. end
  130. # check for existing custom changes after new translations are loaded
  131. described_class.sync_locale_from_po(locale)
  132. list = described_class.lang(locale)
  133. list['list'].each do |item|
  134. translation = described_class.find_source(locale, item[1])
  135. expect(translation.class).to be(described_class)
  136. expect(locale).to eq(translation.locale)
  137. if translation.source == 'open'
  138. expect(translation.target).to eq('offen2')
  139. expect(translation.target_initial).to eq('offen')
  140. else
  141. expect(translation.target).to eq(translation.target_initial)
  142. end
  143. end
  144. # reset custom translations and check for non existing custom changes
  145. described_class.reset(locale)
  146. list = described_class.lang(locale)
  147. list['list'].each do |item|
  148. translation = described_class.find_source(locale, item[1])
  149. expect(translation.class).to be(described_class)
  150. expect(locale).to eq(translation.locale)
  151. expect(translation.target).to eq(translation.target_initial)
  152. end
  153. end
  154. end
  155. describe 'scope -> sources' do
  156. it 'returns an source strings' do
  157. expect(described_class.sources.count).to be_positive
  158. end
  159. end
  160. describe 'scope -> customized' do
  161. context 'when no customized translations exist' do
  162. it 'returns an empty array' do
  163. expect(described_class.customized).to eq([])
  164. end
  165. end
  166. context 'when customized translations exist' do
  167. before do
  168. described_class.find_by(locale: 'de-de', source: 'New').update!(target: 'Neu!')
  169. end
  170. it 'returns the customized translation' do
  171. expect(described_class.customized[0].source).to eq('New')
  172. end
  173. end
  174. context 'when only a new customized translation exists' do
  175. before do
  176. create(:translation, locale: 'de-de', source: 'A example', target: 'Ein Beispiel')
  177. end
  178. it 'returns the customized translation' do
  179. expect(described_class.customized[0].source).to eq('A example')
  180. end
  181. end
  182. end
  183. describe 'scope -> not_customized', :aggregate_failures do
  184. let(:translation_item) { described_class.find_by(locale: 'de-de', source: 'New') }
  185. context 'when customized items exists' do
  186. before do
  187. translation_item.update!(target: 'Neu!')
  188. create(:translation, locale: 'de-de', source: 'A example', target: 'Ein Beispiel')
  189. end
  190. it 'list without customized translations' do
  191. not_customized = described_class.not_customized.where(locale: 'de-de')
  192. expect(not_customized).to be_none { |item| item.source == translation_item.source }
  193. expect(not_customized).to be_none { |item| item.source == 'A example' }
  194. end
  195. end
  196. end
  197. describe '#reset' do
  198. context 'when record is not synced from codebase' do
  199. subject(:translation) { create(:translation, locale: 'de-de', source: 'A example', target: 'Ein Beispiel') }
  200. it 'no changes for record' do
  201. expect { translation.reset }.to not_change { translation.reload }
  202. end
  203. end
  204. context 'when record is synced from codebase' do
  205. subject(:translation) { described_class.find_by(locale: 'de-de', source: 'New') }
  206. context 'when translation was not customized' do
  207. it 'no changes for record' do
  208. expect { translation.reset }.to not_change { translation.reload }
  209. end
  210. end
  211. context 'when translation was customized' do
  212. before do
  213. translation.update!(target: 'Neu!')
  214. end
  215. it 'resets target to initial' do
  216. expect { translation.reset }.to change { translation.reload.target }.to(translation.target_initial)
  217. end
  218. end
  219. end
  220. end
  221. describe 'source string quality' do
  222. it 'strings use the unicode ellipsis sign (…) rather than three dots (...)' do
  223. expect(described_class.sources.where("source LIKE '%...%'").pluck(:source)).to eq([])
  224. end
  225. end
  226. end