translation_spec.rb 10 KB

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