synchronizes_from_po_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Translation, 'synchronizes_from_po' do
  4. context 'when getting the list of files for a locale' do
  5. context 'when a locale is nonexistent' do
  6. it 'logs an error' do
  7. allow(Rails.logger).to receive(:error)
  8. described_class.po_files_for_locale('nonexisting-locale')
  9. expect(Rails.logger).to have_received(:error).with("No translation found for locale 'nonexisting-locale'.")
  10. end
  11. it 'returns an empty array' do
  12. expect(described_class.po_files_for_locale('nonexisting-locale')).to eq([])
  13. end
  14. end
  15. context 'when the locale is en-us' do
  16. it 'returns the translation source catalog' do
  17. expect(described_class.po_files_for_locale('en-us')).to eq(['i18n/zammad.pot'])
  18. end
  19. end
  20. context 'when getting the de-de file list' do
  21. before do
  22. # Simulate an addon translation file being present by duplicating zammad's de-de translation.
  23. FileUtils.copy(Rails.root.join('i18n/zammad.de-de.po'), Rails.root.join('i18n/testaddon.de-de.po'))
  24. end
  25. after do
  26. FileUtils.remove(Rails.root.join('i18n/testaddon.de-de.po'))
  27. end
  28. it 'has the framework content as first entry' do
  29. expect(described_class.po_files_for_locale('de-de').first).to eq('i18n/zammad.de-de.po')
  30. end
  31. it 'also has another addon translation file' do
  32. expect(described_class.po_files_for_locale('de-de')).to include('i18n/testaddon.de-de.po')
  33. end
  34. end
  35. end
  36. context 'when getting po strings for a locale' do
  37. context 'when getting strings for a nonexistent locale' do
  38. it 'logs an error' do
  39. allow(Rails.logger).to receive(:error)
  40. described_class.strings_for_locale('nonexisting-locale')
  41. expect(Rails.logger).to have_received(:error).with("No translation found for locale 'nonexisting-locale'.")
  42. end
  43. it 'returns an empty array' do
  44. expect(described_class.strings_for_locale('nonexisting-locale')).to eq({})
  45. end
  46. end
  47. context 'when getting the en-us strings' do
  48. it 'contains the translation for "yes"' do
  49. expect(described_class.strings_for_locale('en-us')['yes']).to have_attributes(translation: 'yes', translation_file: 'i18n/zammad.pot')
  50. end
  51. it 'contains the translation for "FORMAT_DATE"' do
  52. expect(described_class.strings_for_locale('en-us')['FORMAT_DATE']).to have_attributes(translation: 'mm/dd/yyyy', translation_file: 'i18n/zammad.pot')
  53. end
  54. it 'contains the translation for "FORMAT_DATE_TIME"' do
  55. expect(described_class.strings_for_locale('en-us')['FORMAT_DATETIME']).to have_attributes(translation: 'mm/dd/yyyy l:MM P', translation_file: 'i18n/zammad.pot')
  56. end
  57. end
  58. context 'when getting the de-de strings' do
  59. it 'contains the translation for "yes"' do
  60. expect(described_class.strings_for_locale('de-de')['yes']).to have_attributes(translation: 'ja', translation_file: 'i18n/zammad.de-de.po')
  61. end
  62. it 'contains the translation for "FORMAT_DATE"' do
  63. expect(described_class.strings_for_locale('de-de')['FORMAT_DATE']).to have_attributes(translation: 'dd.mm.yyyy', translation_file: 'i18n/zammad.de-de.po')
  64. end
  65. it 'contains the translation for "FORMAT_DATE_TIME"' do
  66. expect(described_class.strings_for_locale('de-de')['FORMAT_DATETIME']).to have_attributes(translation: 'dd.mm.yyyy HH:MM', translation_file: 'i18n/zammad.de-de.po')
  67. end
  68. end
  69. end
  70. context 'when unescaping po strings' do
  71. it 'does the right thing' do
  72. expect(described_class.unescape_po('My complex \\n \\" string \\\\ with quotes')).to eq("My complex \n \" string \\ with quotes")
  73. end
  74. end
  75. context 'when synchronizing strings for a locale' do
  76. let(:translation_after_sync) do
  77. test_translation = described_class.find_source('de-de', translation_before_sync[:source]) || described_class.new
  78. test_translation.update(translation_before_sync.merge(locale: 'de-de', created_by_id: 1, updated_by_id: 1))
  79. described_class.sync_locale_from_po('de-de')
  80. described_class.find_by(id: test_translation.id)
  81. end
  82. context 'when unknown user strings are present' do
  83. let(:translation_before_sync) { { source: 'unknown string', target: 'unknown translation', is_synchronized_from_codebase: false, synchronized_from_translation_file: nil } }
  84. it 'leaves them alone' do
  85. expect(translation_after_sync).to have_attributes(translation_before_sync)
  86. end
  87. end
  88. context 'when unknown, but user modified synchronized strings are present' do
  89. let(:translation_before_sync) { { source: 'unknown string', target_initial: 'unknown translation', target: 'user modified', is_synchronized_from_codebase: true, synchronized_from_translation_file: nil } }
  90. it 'leaves them alone' do
  91. expect(translation_after_sync).to have_attributes(translation_before_sync)
  92. end
  93. end
  94. context 'when unknown & unchanged synchronized strings are present' do
  95. let(:translation_before_sync) { { source: 'unknown string', target: 'unknown translation', is_synchronized_from_codebase: true } }
  96. it 'deletes them' do
  97. expect(translation_after_sync).to be_nil
  98. end
  99. end
  100. context 'when existing synchronized strings need an update' do
  101. context 'when unmodified' do
  102. let(:translation_before_sync) { { source: 'yes', target_initial: 'unknown translation', target: 'unknown translation', is_synchronized_from_codebase: true } }
  103. it 'updates both target and target_initial' do
  104. expect(translation_after_sync).to have_attributes(target_initial: 'ja', target: 'ja')
  105. end
  106. end
  107. context 'when modified' do
  108. let(:translation_before_sync) { { source: 'yes', target_initial: 'unknown translation', target: 'user changed', is_synchronized_from_codebase: true } }
  109. it 'updates only target_initial' do
  110. expect(translation_after_sync).to have_attributes(target_initial: 'ja', target: 'user changed')
  111. end
  112. end
  113. context 'when source language file changes' do
  114. let(:translation_before_sync) { { source: 'yes', synchronized_from_translation_file: 'i18n/my-fabulous-addon.de-de.po', is_synchronized_from_codebase: true } }
  115. it 'updates :synchronized_from_translation_file' do
  116. expect(translation_after_sync).to have_attributes(synchronized_from_translation_file: 'i18n/zammad.de-de.po')
  117. end
  118. end
  119. end
  120. context 'when existing unsynchronized strings start to be synchronized' do
  121. context 'when unmodified' do
  122. let(:translation_before_sync) { { source: 'yes', target_initial: 'user changed', target: 'user changed', is_synchronized_from_codebase: false } }
  123. it 'updates both target and target_initial' do
  124. expect(translation_after_sync).to have_attributes(target_initial: 'ja', target: 'ja', is_synchronized_from_codebase: true)
  125. end
  126. end
  127. context 'when modified' do
  128. let(:translation_before_sync) { { source: 'yes', target_initial: 'user changed', target: 'different', is_synchronized_from_codebase: false } }
  129. it 'updates only target_initial' do
  130. expect(translation_after_sync).to have_attributes(target_initial: 'ja', target: 'different', is_synchronized_from_codebase: true)
  131. end
  132. end
  133. end
  134. context 'when new strings are added via sync' do
  135. before do
  136. described_class.find_source('de-de', 'yes').destroy!
  137. described_class.sync_locale_from_po('de-de')
  138. end
  139. it 'adds them' do
  140. expect(described_class.find_source('de-de', 'yes')).to have_attributes(source: 'yes', target_initial: 'ja', target: 'ja', is_synchronized_from_codebase: true, synchronized_from_translation_file: 'i18n/zammad.de-de.po')
  141. end
  142. end
  143. end
  144. context 'when synchronizing strings for CI locales' do
  145. # Tests are slow, so use before :all to save time.
  146. before :all do # rubocop:disable RSpec/BeforeAfterAll
  147. # Simulate additional entries
  148. File.write(Rails.root.join('i18n/testaddon.de-de.po'), <<~CUSTOM_PO)
  149. msgid "custom-string-translated"
  150. msgstr "custom-string-übersetzt"
  151. msgid "custom-string-too-long"
  152. msgstr "custom-string-too-long #{'a' * 3001}"
  153. msgid "custom-string-untranslated"
  154. msgstr ""
  155. #, fuzzy
  156. msgid "custom-string-fuzzy"
  157. msgstr "custom-string-fuzzy"
  158. CUSTOM_PO
  159. described_class.all.delete_all
  160. described_class.sync
  161. end
  162. after :all do # rubocop:disable RSpec/BeforeAfterAll
  163. FileUtils.remove(Rails.root.join('i18n/testaddon.de-de.po'))
  164. end
  165. it 'adds many of them' do
  166. expect(described_class.where(locale: 'de-de').count).to be > 500
  167. end
  168. it 'adds correct data' do
  169. expect(described_class.where(locale: 'de-de').first).to have_attributes(is_synchronized_from_codebase: true, synchronized_from_translation_file: 'i18n/zammad.de-de.po')
  170. end
  171. it 'adds the en-us FORMAT_DATE entry' do
  172. expect(described_class.find_source('en-us', 'FORMAT_DATE')).to have_attributes(is_synchronized_from_codebase: true, synchronized_from_translation_file: 'i18n/zammad.pot', target: 'mm/dd/yyyy')
  173. end
  174. it 'adds the en-us FORMAT_DATETIME entry' do
  175. expect(described_class.find_source('en-us', 'FORMAT_DATETIME')).to have_attributes(is_synchronized_from_codebase: true, synchronized_from_translation_file: 'i18n/zammad.pot', target: 'mm/dd/yyyy l:MM P')
  176. end
  177. it 'adds the custom translated entry with content' do
  178. expect(described_class.find_source('de-de', 'custom-string-translated')).to have_attributes(target: 'custom-string-übersetzt')
  179. end
  180. it 'adds the custom untranslated entry without content' do
  181. expect(described_class.find_source('de-de', 'custom-string-untranslated')).to have_attributes(target: '')
  182. end
  183. it 'adds the custom fuzzy entry without content' do
  184. expect(described_class.find_source('de-de', 'custom-string-fuzzy')).to have_attributes(target: '')
  185. end
  186. it 'ignores strings that are too long' do
  187. expect(described_class.find_source('de-de', 'custom-string-too-long')).to be_nil
  188. end
  189. end
  190. # Make sure that translation imports work really for some major locales.
  191. context 'when synchronizing strings for some major locales' do
  192. before do
  193. # Only 'en-us' and 'de-de' are returned in test env - override.
  194. allow(Locale).to receive(:to_sync).and_return(Locale.where(locale: %w[en-us de-de fr-fr ru zh-cn]))
  195. described_class.sync
  196. end
  197. it 'imports without error and finds Chinese entries' do
  198. expect(described_class.where(locale: 'zh-cn').count).to be > 500
  199. end
  200. end
  201. end