translation_synchronizes_from_po_spec.rb 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 'throws' do
  7. expect { described_class.po_files_for_locale('nonexisting-locale') }.to raise_error "No translation found for locale 'nonexisting-locale'."
  8. end
  9. end
  10. context 'when the locale is en-us' do
  11. it 'returns the translation source catalog' do
  12. expect(described_class.po_files_for_locale('en-us')).to eq(['i18n/zammad.pot'])
  13. end
  14. end
  15. context 'when getting the de-de file list' do
  16. before do
  17. # Simulate an addon translation file being present by duplicating zammad's de-de translation.
  18. FileUtils.copy(Rails.root.join('i18n/zammad.de-de.po'), Rails.root.join('i18n/testaddon.de-de.po'))
  19. end
  20. after do
  21. FileUtils.remove(Rails.root.join('i18n/testaddon.de-de.po'))
  22. end
  23. it 'has the framework content as first entry' do
  24. expect(described_class.po_files_for_locale('de-de').first).to eq('i18n/zammad.de-de.po')
  25. end
  26. it 'also has another addon translation file' do
  27. expect(described_class.po_files_for_locale('de-de')).to include('i18n/testaddon.de-de.po')
  28. end
  29. end
  30. end
  31. context 'when getting po strings for a locale' do
  32. context 'when getting strings for a nonexistent locale' do
  33. it 'throws' do
  34. expect { described_class.strings_for_locale('nonexisting-locale') }.to raise_error "No translation found for locale 'nonexisting-locale'."
  35. end
  36. end
  37. context 'when getting the en-us strings' do
  38. it 'contains the translation for "yes"' do
  39. expect(described_class.strings_for_locale('en-us')['yes']).to have_attributes(translation: 'yes', translation_file: 'i18n/zammad.pot')
  40. end
  41. it 'contains the translation for "FORMAT_DATE"' do
  42. expect(described_class.strings_for_locale('en-us')['FORMAT_DATE']).to have_attributes(translation: 'mm/dd/yyyy', translation_file: 'i18n/zammad.pot')
  43. end
  44. it 'contains the translation for "FORMAT_DATE_TIME"' do
  45. 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')
  46. end
  47. end
  48. context 'when getting the de-de strings' do
  49. it 'contains the translation for "yes"' do
  50. expect(described_class.strings_for_locale('de-de')['yes']).to have_attributes(translation: 'ja', translation_file: 'i18n/zammad.de-de.po')
  51. end
  52. it 'contains the translation for "FORMAT_DATE"' do
  53. expect(described_class.strings_for_locale('de-de')['FORMAT_DATE']).to have_attributes(translation: 'dd.mm.yyyy', translation_file: 'i18n/zammad.de-de.po')
  54. end
  55. it 'contains the translation for "FORMAT_DATE_TIME"' do
  56. 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')
  57. end
  58. end
  59. end
  60. context 'when unescaping po strings' do
  61. it 'does the right thing' do
  62. expect(described_class.unescape_po('My complex \\n \\" string \\\\ with quotes')).to eq("My complex \n \" string \\ with quotes")
  63. end
  64. end
  65. context 'when synchronizing strings for a locale' do
  66. let(:translation_after_sync) do
  67. test_translation = described_class.find_source('de-de', translation_before_sync[:source]) || described_class.new
  68. test_translation.update(translation_before_sync.merge(locale: 'de-de', created_by_id: 1, updated_by_id: 1))
  69. described_class.sync_locale_from_po('de-de')
  70. described_class.find_by(id: test_translation.id)
  71. end
  72. context 'when unknown user strings are present' do
  73. let(:translation_before_sync) { { source: 'unknown string', target: 'unknown translation', is_synchronized_from_codebase: false, synchronized_from_translation_file: nil } }
  74. it 'leaves them alone' do
  75. expect(translation_after_sync).to have_attributes(translation_before_sync)
  76. end
  77. end
  78. context 'when unknown, but user modified synchronized strings are present' do
  79. let(:translation_before_sync) { { source: 'unknown string', target_initial: 'unknown translation', target: 'user modified', is_synchronized_from_codebase: true, synchronized_from_translation_file: nil } }
  80. it 'leaves them alone' do
  81. expect(translation_after_sync).to have_attributes(translation_before_sync)
  82. end
  83. end
  84. context 'when unknown & unchanged synchronized strings are present' do
  85. let(:translation_before_sync) { { source: 'unknown string', target: 'unknown translation', is_synchronized_from_codebase: true } }
  86. it 'deletes them' do
  87. expect(translation_after_sync).to be_nil
  88. end
  89. end
  90. context 'when existing synchronized strings need an update' do
  91. context 'when unmodified' do
  92. let(:translation_before_sync) { { source: 'yes', target_initial: 'unknown translation', target: 'unknown translation', is_synchronized_from_codebase: true } }
  93. it 'updates both target and target_initial' do
  94. expect(translation_after_sync).to have_attributes(target_initial: 'ja', target: 'ja')
  95. end
  96. end
  97. context 'when modified' do
  98. let(:translation_before_sync) { { source: 'yes', target_initial: 'unknown translation', target: 'user changed', is_synchronized_from_codebase: true } }
  99. it 'updates only target_initial' do
  100. expect(translation_after_sync).to have_attributes(target_initial: 'ja', target: 'user changed')
  101. end
  102. end
  103. context 'when source language file changes' do
  104. let(:translation_before_sync) { { source: 'yes', synchronized_from_translation_file: 'i18n/my-fabulous-addon.de-de.po', is_synchronized_from_codebase: true } }
  105. it 'updates :synchronized_from_translation_file' do
  106. expect(translation_after_sync).to have_attributes(synchronized_from_translation_file: 'i18n/zammad.de-de.po')
  107. end
  108. end
  109. end
  110. context 'when existing unsynchronized strings start to be synchronized' do
  111. context 'when unmodified' do
  112. let(:translation_before_sync) { { source: 'yes', target_initial: 'user changed', target: 'user changed', is_synchronized_from_codebase: false } }
  113. it 'updates both target and target_initial' do
  114. expect(translation_after_sync).to have_attributes(target_initial: 'ja', target: 'ja', is_synchronized_from_codebase: true)
  115. end
  116. end
  117. context 'when modified' do
  118. let(:translation_before_sync) { { source: 'yes', target_initial: 'user changed', target: 'different', is_synchronized_from_codebase: false } }
  119. it 'updates only target_initial' do
  120. expect(translation_after_sync).to have_attributes(target_initial: 'ja', target: 'different', is_synchronized_from_codebase: true)
  121. end
  122. end
  123. end
  124. context 'when new strings are added via sync' do
  125. before do
  126. described_class.find_source('de-de', 'yes').destroy!
  127. described_class.sync_locale_from_po('de-de')
  128. end
  129. it 'adds them' do
  130. 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')
  131. end
  132. end
  133. end
  134. context 'when synchronizing strings for all locales' do
  135. before do
  136. # Simulate additional entries
  137. File.write(Rails.root.join('i18n/testaddon.de-de.po'), <<~CUSTOM_PO)
  138. msgid "custom-string-translated"
  139. msgstr "custom-string-übersetzt"
  140. msgid "custom-string-too-long"
  141. msgstr "custom-string-too-long #{'a' * 3001}"
  142. msgid "custom-string-untranslated"
  143. msgstr ""
  144. #, fuzzy
  145. msgid "custom-string-fuzzy"
  146. msgstr "custom-string-fuzzy"
  147. CUSTOM_PO
  148. described_class.all.delete_all
  149. described_class.sync
  150. end
  151. after do
  152. FileUtils.remove(Rails.root.join('i18n/testaddon.de-de.po'))
  153. end
  154. it 'adds many of them' do
  155. expect(described_class.where(locale: 'de-de').count).to be > 500
  156. end
  157. it 'adds correct data' do
  158. 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')
  159. end
  160. it 'adds the en-us FORMAT_DATE entry' do
  161. 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')
  162. end
  163. it 'adds the en-us FORMAT_DATETIME entry' do
  164. 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')
  165. end
  166. it 'adds the custom translated entry with content' do
  167. expect(described_class.find_source('de-de', 'custom-string-translated')).to have_attributes(target: 'custom-string-übersetzt')
  168. end
  169. it 'adds the custom untranslated entry without content' do
  170. expect(described_class.find_source('de-de', 'custom-string-untranslated')).to have_attributes(target: '')
  171. end
  172. it 'adds the custom fuzzy entry without content' do
  173. expect(described_class.find_source('de-de', 'custom-string-fuzzy')).to have_attributes(target: '')
  174. end
  175. it 'ignores strings that are too long' do
  176. expect(described_class.find_source('de-de', 'custom-string-too-long')).to be_nil
  177. end
  178. end
  179. end