Browse Source

Maintenance: Add support for on-the-fly transliteration of Serbian Latin to Serbian Cyrillic translations.

Dusan Vuckovic 2 years ago
parent
commit
8432c889af

+ 1 - 0
Gemfile

@@ -162,6 +162,7 @@ gem 'viewpoint', require: false
 gem 'openssl'
 
 # Translation sync
+gem 'byk', require: false
 gem 'PoParser', require: false
 
 # Gems used only for develop/test and not required

+ 2 - 0
Gemfile.lock

@@ -118,6 +118,7 @@ GEM
     buftok (0.2.0)
     builder (3.2.4)
     byebug (11.1.3)
+    byk (1.1.0)
     capybara (3.37.1)
       addressable
       matrix
@@ -615,6 +616,7 @@ DEPENDENCIES
   brakeman
   browser
   byebug
+  byk
   capybara
   chunky_png
   clearbit

+ 19 - 6
app/models/translation/synchronizes_from_po.rb

@@ -5,7 +5,8 @@ module Translation::SynchronizesFromPo
 
   # Represents an entry to import to the Translation table.
   class TranslationEntry
-    attr_reader :source, :translation, :translation_file, :skip_sync
+    attr_reader :source, :translation_file, :skip_sync
+    attr_accessor :translation
 
     def self.create(locale, file, entry) # rubocop:disable Metrics/AbcSize
       source = unescape_po(entry.msgid.to_s)
@@ -114,13 +115,20 @@ module Translation::SynchronizesFromPo
       @cached_strings_for_locale[locale] ||= strings_for_locale(locale).freeze
     end
 
-    def strings_for_locale(locale)
+    def strings_for_locale(locale) # rubocop:disable Metrics/AbcSize
       result = {}
       po_files_for_locale(locale).each do |file|
         require 'poparser' # Only load when it is actually used
         PoParser.parse_file(Rails.root.join(file)).entries.each do |entry|
 
           TranslationEntry.create(locale, file, entry).tap do |translation_entry|
+
+            # In case of Serbian Latin locale, transliterate the translation string to latin alphabet on-the-fly.
+            if locale == 'sr-latn-rs'
+              require 'byk/safe' # Only load when it is actually used
+              translation_entry.translation = Byk.to_latin(translation_entry.translation)
+            end
+
             result[translation_entry.source] = translation_entry
           end
         end
@@ -134,14 +142,19 @@ module Translation::SynchronizesFromPo
     def po_files_for_locale(locale)
       return ['i18n/zammad.pot'] if locale.eql? 'en-us'
 
-      files = Dir.glob "i18n/*.#{locale}.po", base: Rails.root
-      if files.exclude?("i18n/zammad.#{locale}.po")
-        Rails.logger.error "No translation found for locale '#{locale}'."
+      locale_name = locale
+
+      # In case of Serbian Latin locale, return Serbian Cyrillic po files instead.
+      locale_name = 'sr-cyrl-rs' if locale_name == 'sr-latn-rs'
+
+      files = Dir.glob "i18n/*.#{locale_name}.po", base: Rails.root
+      if files.exclude?("i18n/zammad.#{locale_name}.po")
+        Rails.logger.error "No translation found for locale '#{locale_name}'."
         return []
       end
 
       [
-        files.delete("i18n/zammad.#{locale}.po"),
+        files.delete("i18n/zammad.#{locale_name}.po"),
         *files.sort
       ]
     end

+ 13 - 0
spec/models/translation/synchronizes_from_po_spec.rb

@@ -41,7 +41,13 @@ RSpec.describe Translation, 'synchronizes_from_po' do
       it 'also has another addon translation file' do
         expect(described_class.po_files_for_locale('de-de')).to include('i18n/testaddon.de-de.po')
       end
+    end
 
+    context 'when getting the sr-latn-rs file list' do
+      it 'uses the sr-cyrl-rs framework content instead' do
+        pending 'waiting on merge of sr-cyrl-rs translations'
+        expect(described_class.po_files_for_locale('sr-latn-rs').first).to eq('i18n/zammad.sr-cyrl-rs.po')
+      end
     end
   end
 
@@ -85,6 +91,13 @@ RSpec.describe Translation, 'synchronizes_from_po' do
         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')
       end
     end
+
+    context 'when getting the sr-latn-rs strings' do
+      it 'contains transliterated sr-cyrl-rs translation for "yes"' do
+        pending 'waiting on merge of sr-cyrl-rs translations'
+        expect(described_class.strings_for_locale('sr-latn-rs')['yes']).to have_attributes(translation: 'da', translation_file: 'i18n/zammad.sr-cyrl-rs.po')
+      end
+    end
   end
 
   context 'when unescaping po strings' do