translation_synchronizes_from_po_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Translation 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. it 'clear cache after sync' do
  144. allow(Cache).to receive(:delete)
  145. described_class.sync_locale_from_po('de-de')
  146. expect(Cache).to have_received(:delete).with('TranslationMapOnlyContent::de-de')
  147. end
  148. end
  149. context 'when synchronizing strings for CI locales' do
  150. # Tests are slow, so use before :all to save time.
  151. before :all do # rubocop:disable RSpec/BeforeAfterAll
  152. # Simulate additional entries
  153. File.write(Rails.root.join('i18n/testaddon.de-de.po'), <<~CUSTOM_PO)
  154. msgid "custom-string-translated"
  155. msgstr "custom-string-übersetzt"
  156. msgid "custom-string-too-long"
  157. msgstr "custom-string-too-long #{'a' * 3001}"
  158. msgid "custom-string-untranslated"
  159. msgstr ""
  160. #, fuzzy
  161. msgid "custom-string-fuzzy"
  162. msgstr "custom-string-fuzzy"
  163. CUSTOM_PO
  164. described_class.all.delete_all
  165. described_class.sync
  166. end
  167. after :all do # rubocop:disable RSpec/BeforeAfterAll
  168. FileUtils.remove(Rails.root.join('i18n/testaddon.de-de.po'))
  169. end
  170. it 'adds many of them' do
  171. expect(described_class.where(locale: 'de-de').count).to be > 500
  172. end
  173. it 'adds correct data' do
  174. 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')
  175. end
  176. it 'adds the en-us FORMAT_DATE entry' do
  177. 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')
  178. end
  179. it 'adds the en-us FORMAT_DATETIME entry' do
  180. 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')
  181. end
  182. it 'adds the custom translated entry with content' do
  183. expect(described_class.find_source('de-de', 'custom-string-translated')).to have_attributes(target: 'custom-string-übersetzt')
  184. end
  185. it 'adds the custom untranslated entry without content' do
  186. expect(described_class.find_source('de-de', 'custom-string-untranslated')).to have_attributes(target: '')
  187. end
  188. it 'adds the custom fuzzy entry without content' do
  189. expect(described_class.find_source('de-de', 'custom-string-fuzzy')).to have_attributes(target: '')
  190. end
  191. it 'ignores strings that are too long' do
  192. expect(described_class.find_source('de-de', 'custom-string-too-long')).to be_nil
  193. end
  194. end
  195. # Make sure that translation imports work really for all locales.
  196. context 'when synchronizing strings for all locales' do
  197. before do
  198. # Only 'en-us' and 'de-de' are returned in test env - override.
  199. allow(Locale).to receive(:to_sync).and_return(Locale.where(active: true))
  200. described_class.sync
  201. end
  202. it 'imports without error and finds Chinese entries' do
  203. expect(described_class.where(locale: 'zh-cn').count).to be > 500
  204. end
  205. end
  206. end