synchronizes_from_po_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. # Copyright (C) 2012-2024 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. context 'when getting the sr-latn-rs file list' do
  36. it 'uses the sr-cyrl-rs framework content instead' do
  37. expect(described_class.po_files_for_locale('sr-latn-rs').first).to eq('i18n/zammad.sr-cyrl-rs.po')
  38. end
  39. end
  40. end
  41. context 'when getting po strings for a locale' do
  42. context 'when getting strings for a nonexistent locale' do
  43. it 'logs an error' do
  44. allow(Rails.logger).to receive(:error)
  45. described_class.strings_for_locale('nonexisting-locale')
  46. expect(Rails.logger).to have_received(:error).with("No translation found for locale 'nonexisting-locale'.")
  47. end
  48. it 'returns an empty array' do
  49. expect(described_class.strings_for_locale('nonexisting-locale')).to eq({})
  50. end
  51. end
  52. context 'when getting the en-us strings' do
  53. it 'contains the translation for "yes"' do
  54. expect(described_class.strings_for_locale('en-us')['yes']).to have_attributes(translation: 'yes', translation_file: 'i18n/zammad.pot')
  55. end
  56. it 'contains the translation for "FORMAT_DATE"' do
  57. expect(described_class.strings_for_locale('en-us')['FORMAT_DATE']).to have_attributes(translation: 'mm/dd/yyyy', translation_file: 'i18n/zammad.pot')
  58. end
  59. it 'contains the translation for "FORMAT_DATE_TIME"' do
  60. 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')
  61. end
  62. end
  63. context 'when getting the de-de strings' do
  64. it 'contains the translation for "yes"' do
  65. expect(described_class.strings_for_locale('de-de')['yes']).to have_attributes(translation: 'ja', translation_file: 'i18n/zammad.de-de.po')
  66. end
  67. it 'contains the translation for "FORMAT_DATE"' do
  68. expect(described_class.strings_for_locale('de-de')['FORMAT_DATE']).to have_attributes(translation: 'dd.mm.yyyy', translation_file: 'i18n/zammad.de-de.po')
  69. end
  70. it 'contains the translation for "FORMAT_DATE_TIME"' do
  71. 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')
  72. end
  73. end
  74. context 'when getting the sr-latn-rs strings' do
  75. it 'contains transliterated sr-cyrl-rs translation for "yes"' do
  76. expect(described_class.strings_for_locale('sr-latn-rs')['yes']).to have_attributes(translation: 'da', translation_file: 'i18n/zammad.sr-cyrl-rs.po')
  77. end
  78. end
  79. end
  80. context 'when unescaping po strings' do
  81. it 'does the right thing' do
  82. expect(Translation::TranslationEntry.unescape_po('My complex \\n \\" string \\\\ with quotes')).to eq("My complex \n \" string \\ with quotes")
  83. end
  84. end
  85. context 'when synchronizing strings for a locale' do
  86. let(:translation_after_sync) do
  87. test_translation = described_class.find_source('de-de', translation_before_sync[:source]) || described_class.new
  88. test_translation.update(translation_before_sync.merge(locale: 'de-de', created_by_id: 1, updated_by_id: 1))
  89. described_class.sync_locale_from_po('de-de')
  90. described_class.find_by(id: test_translation.id)
  91. end
  92. context 'when unknown user strings are present' do
  93. let(:translation_before_sync) { { source: 'unknown string', target: 'unknown translation', is_synchronized_from_codebase: false, synchronized_from_translation_file: nil } }
  94. it 'leaves them alone' do
  95. expect(translation_after_sync).to have_attributes(translation_before_sync)
  96. end
  97. end
  98. context 'when unknown, but user modified synchronized strings are present' do
  99. let(:translation_before_sync) { { source: 'unknown string', target_initial: 'unknown translation', target: 'user modified', is_synchronized_from_codebase: true, synchronized_from_translation_file: nil } }
  100. it 'leaves them alone' do
  101. expect(translation_after_sync).to have_attributes(translation_before_sync)
  102. end
  103. end
  104. context 'when unknown & unchanged synchronized strings are present' do
  105. let(:translation_before_sync) { { source: 'unknown string', target: 'unknown translation', is_synchronized_from_codebase: true } }
  106. it 'deletes them' do
  107. expect(translation_after_sync).to be_nil
  108. end
  109. end
  110. context 'when existing synchronized strings need an update' do
  111. context 'when unmodified' do
  112. let(:translation_before_sync) { { source: 'yes', target_initial: 'unknown translation', target: 'unknown translation', is_synchronized_from_codebase: true } }
  113. it 'updates both target and target_initial' do
  114. expect(translation_after_sync).to have_attributes(target_initial: 'ja', target: 'ja')
  115. end
  116. end
  117. context 'when modified' do
  118. let(:translation_before_sync) { { source: 'yes', target_initial: 'unknown translation', target: 'user changed', is_synchronized_from_codebase: true } }
  119. it 'updates only target_initial' do
  120. expect(translation_after_sync).to have_attributes(target_initial: 'ja', target: 'user changed')
  121. end
  122. end
  123. context 'when source language file changes' do
  124. let(:translation_before_sync) { { source: 'yes', synchronized_from_translation_file: 'i18n/my-fabulous-addon.de-de.po', is_synchronized_from_codebase: true } }
  125. it 'updates :synchronized_from_translation_file' do
  126. expect(translation_after_sync).to have_attributes(synchronized_from_translation_file: 'i18n/zammad.de-de.po')
  127. end
  128. end
  129. end
  130. context 'when existing unsynchronized strings start to be synchronized' do
  131. context 'when unmodified' do
  132. let(:translation_before_sync) { { source: 'yes', target_initial: 'user changed', target: 'user changed', is_synchronized_from_codebase: false } }
  133. it 'updates both target and target_initial' do
  134. expect(translation_after_sync).to have_attributes(target_initial: 'ja', target: 'ja', is_synchronized_from_codebase: true)
  135. end
  136. end
  137. context 'when modified' do
  138. let(:translation_before_sync) { { source: 'yes', target_initial: 'user changed', target: 'different', is_synchronized_from_codebase: false } }
  139. it 'updates only target_initial' do
  140. expect(translation_after_sync).to have_attributes(target_initial: 'ja', target: 'different', is_synchronized_from_codebase: true)
  141. end
  142. end
  143. end
  144. context 'when new strings are added via sync' do
  145. before do
  146. described_class.find_source('de-de', 'yes').destroy!
  147. described_class.sync_locale_from_po('de-de')
  148. end
  149. it 'adds them' do
  150. 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')
  151. end
  152. end
  153. end
  154. context 'when synchronizing strings for CI locales' do
  155. # Tests are slow, so use before :all to save time.
  156. before :all do # rubocop:disable RSpec/BeforeAfterAll
  157. # Simulate additional entries
  158. Rails.root.join('i18n/testaddon.de-de.po').write(<<~CUSTOM_PO)
  159. #: app/graphql/custom.rb
  160. msgid "custom-string-translated"
  161. msgstr "custom-string-übersetzt"
  162. #: app/views/mailer/ticket_create/zh-tw.html.erb
  163. #: app/views/messaging/ticket_create/en.md.erb
  164. msgid "custom-string-to-skip"
  165. msgstr "custom-string-zu-überspringen"
  166. msgid "custom-string-too-long"
  167. msgstr "custom-string-too-long #{'a' * 3001}"
  168. msgid "custom-string-untranslated"
  169. msgstr ""
  170. #, fuzzy
  171. msgid "custom-string-fuzzy"
  172. msgstr "custom-string-fuzzy"
  173. CUSTOM_PO
  174. described_class.delete_all
  175. described_class.sync
  176. end
  177. after :all do # rubocop:disable RSpec/BeforeAfterAll
  178. FileUtils.remove(Rails.root.join('i18n/testaddon.de-de.po'))
  179. end
  180. it 'adds many of them' do
  181. expect(described_class.where(locale: 'de-de').count).to be > 500
  182. end
  183. it 'adds correct data' do
  184. 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')
  185. end
  186. it 'adds the en-us FORMAT_DATE entry' do
  187. 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')
  188. end
  189. it 'adds the en-us FORMAT_DATETIME entry' do
  190. 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')
  191. end
  192. it 'adds the custom translated entry with content' do
  193. expect(described_class.find_source('de-de', 'custom-string-translated')).to have_attributes(target: 'custom-string-übersetzt')
  194. end
  195. it 'adds the custom untranslated entry without content' do
  196. expect(described_class.find_source('de-de', 'custom-string-untranslated')).to have_attributes(target: '')
  197. end
  198. it 'adds the custom fuzzy entry without content' do
  199. expect(described_class.find_source('de-de', 'custom-string-fuzzy')).to have_attributes(target: '')
  200. end
  201. it 'ignores strings that are too long' do
  202. expect(described_class.find_source('de-de', 'custom-string-too-long')).to be_nil
  203. end
  204. it 'skips strings that need to be skipped' do
  205. expect(described_class.find_source('de-de', 'custom-string-to-skip')).to be_nil
  206. end
  207. end
  208. # Make sure that translation imports work really for some major locales.
  209. context 'when synchronizing strings for some major locales' do
  210. before do
  211. # Only 'en-us' and 'de-de' are returned in test env - override.
  212. allow(Locale).to receive(:to_sync).and_return(Locale.where(locale: %w[en-us de-de fr-fr ru zh-cn]))
  213. described_class.sync
  214. end
  215. it 'imports without error and finds Chinese entries' do
  216. expect(described_class.where(locale: 'zh-cn').count).to be > 500
  217. end
  218. end
  219. end