locale_spec.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Locale, type: :model do
  4. describe '.default' do
  5. context 'with default locale' do
  6. before { Setting.set('locale_default', 'foo') }
  7. it 'returns the system-wide default locale' do
  8. expect(described_class.default).to eq('foo')
  9. end
  10. end
  11. context 'without default locale' do
  12. before { Setting.set('locale_default', nil) }
  13. it 'returns en-us' do
  14. expect(described_class.default).to eq('en-us')
  15. end
  16. end
  17. end
  18. describe '.sync()' do
  19. context 'when importing locales' do
  20. before do
  21. described_class.delete_all
  22. described_class.sync
  23. end
  24. it 'imports many locales locales' do
  25. expect(described_class.count).to be > 40
  26. end
  27. it 'imports locale data correctly' do
  28. expect(described_class.find_by(locale: 'de-de')).to have_attributes(locale: 'de-de', alias: 'de', name: 'Deutsch', dir: 'ltr', active: true)
  29. end
  30. end
  31. end
  32. # Ensure valid date/time format strings. Since not all translations are imported in CI,
  33. # we read the values from the po file directly.
  34. # See also https://github.com/zammad/zammad/issues/5223
  35. describe 'locale validity' do
  36. before do
  37. described_class.sync
  38. end
  39. def string_from_po(locale, msgid)
  40. po = Rails.root.join("i18n/zammad.#{locale.locale}.po").read
  41. po.scan(%r{msgid "#{msgid}"\nmsgstr "([^"]+)"}).first.first
  42. end
  43. matcher :have_valid_date_format_string do
  44. match do
  45. string_from_po(actual, 'FORMAT_DATE').match(%r{^[ymd./ -]+$})
  46. end
  47. failure_message do
  48. "Locale #{actual.locale} has an invalid value for FORMAT_DATE: #{string_from_po(actual, 'FORMAT_DATE')}"
  49. end
  50. end
  51. matcher :have_valid_datetime_format_string do
  52. match do
  53. string_from_po(actual, 'FORMAT_DATETIME').match(%r{^[ymdHMSlP:./ -]+$})
  54. end
  55. failure_message do
  56. "Locale #{actual.locale} has an invalid value for FORMAT_DATETIME: #{string_from_po(actual, 'FORMAT_DATETIME')}"
  57. end
  58. end
  59. it 'has locales with valid format strings', :aggregate_failures do
  60. skip_locales = %w[en-us sr-latn-rs].freeze
  61. described_class.all.each do |locale|
  62. next if skip_locales.include?(locale.locale)
  63. expect(locale).to have_valid_date_format_string
  64. expect(locale).to have_valid_datetime_format_string
  65. end
  66. end
  67. end
  68. end