translation_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. require 'rails_helper'
  2. RSpec.describe Translation do
  3. context 'default translations' do
  4. Translation.reset('de-de')
  5. Translation.sync('de-de')
  6. it 'en with existing word' do
  7. expect(Translation.translate('en', 'New')).to eq('New')
  8. end
  9. it 'en-us with existing word' do
  10. expect(Translation.translate('en-us', 'New')).to eq('New')
  11. end
  12. it 'en with not existing word' do
  13. expect(Translation.translate('en', 'Some Not Existing Word')).to eq('Some Not Existing Word')
  14. end
  15. it 'de-de with existing word' do
  16. expect(Translation.translate('de-de', 'New')).to eq('Neu')
  17. end
  18. it 'de-de with existing word' do
  19. expect(Translation.translate('de-de', 'Some Not Existing Word')).to eq('Some Not Existing Word')
  20. end
  21. end
  22. context 'custom translation tests' do
  23. Translation.where(locale: 'de-de').destroy_all
  24. Translation.sync('de-de')
  25. locale = 'de-de'
  26. it 'cycle of change and reload translation' do
  27. # check for non existing custom changes
  28. list = Translation.lang(locale)
  29. list['list'].each { |item|
  30. translation = Translation.find_by(source: item[1], locale: locale)
  31. expect(translation.class).to be(Translation)
  32. expect(locale).to eq(translation.locale)
  33. expect(translation.target).to eq(translation.target_initial)
  34. }
  35. # add custom changes
  36. translation = Translation.find_by(locale: locale, source: 'open')
  37. expect(translation.class).to be(Translation)
  38. expect(translation.target).to eq('offen')
  39. expect(translation.target_initial).to eq('offen')
  40. translation.target = 'offen2'
  41. translation.save!
  42. list = Translation.lang(locale)
  43. list['list'].each { |item|
  44. translation = Translation.find_by(source: item[1], locale: locale)
  45. expect(translation.class).to be(Translation)
  46. expect(locale).to eq(translation.locale)
  47. if translation.source == 'open'
  48. expect(translation.target).to eq('offen2')
  49. expect(translation.target_initial).to eq('offen')
  50. else
  51. expect(translation.target).to eq(translation.target_initial)
  52. end
  53. }
  54. # check for existing custom changes after new translations are loaded
  55. Translation.load(locale)
  56. list = Translation.lang(locale)
  57. list['list'].each { |item|
  58. translation = Translation.find_by(source: item[1], locale: locale)
  59. expect(translation.class).to be(Translation)
  60. expect(locale).to eq(translation.locale)
  61. if translation.source == 'open'
  62. expect(translation.target).to eq('offen2')
  63. expect(translation.target_initial).to eq('offen')
  64. else
  65. expect(translation.target).to eq(translation.target_initial)
  66. end
  67. }
  68. # reset custom translations and check for non existing custom changes
  69. Translation.reset(locale)
  70. list = Translation.lang(locale)
  71. list['list'].each { |item|
  72. translation = Translation.find_by(source: item[1], locale: locale)
  73. expect(translation.class).to be(Translation)
  74. expect(locale).to eq(translation.locale)
  75. expect(translation.target).to eq(translation.target_initial)
  76. }
  77. end
  78. end
  79. context 'file based import' do
  80. it 'check download of locales' do
  81. version = Version.get
  82. directory = Rails.root.join('config')
  83. file = Rails.root.join("#{directory}/locales-#{version}.yml")
  84. if File.exist?(file)
  85. File.delete(file)
  86. end
  87. expect(File.exist?(file)).to be false
  88. Locale.fetch
  89. expect(File.exist?(file)).to be true
  90. end
  91. it 'check download of translations' do
  92. locale = 'de-de'
  93. directory = Rails.root.join('config/translations')
  94. if File.directory?(directory)
  95. FileUtils.rm_rf(directory)
  96. end
  97. file = Rails.root.join("#{directory}/#{locale}-#{version}.yml")
  98. expect(File.exist?(file)).to be false
  99. Translation.fetch(locale)
  100. expect(File.exist?(file)).to be true
  101. end
  102. end
  103. end