From 2d6149234de748d91d44dffa224865572a921412 Mon Sep 17 00:00:00 2001 From: Mantas Masalskis Date: Wed, 16 Mar 2022 18:35:50 +0200 Subject: [PATCH] Fixes #4016 - Translations are not available in API after syncing --- app/models/translation.rb | 41 +++++++++++-------- .../translation/synchronizes_from_po.rb | 2 +- .../translation_synchronizes_from_po_spec.rb | 8 ++++ 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/app/models/translation.rb b/app/models/translation.rb index 909592e6e..b28f7a136 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -4,9 +4,6 @@ class Translation < ApplicationModel include Translation::SynchronizesFromPo before_create :set_initial - after_create :cache_clear - after_update :cache_clear - after_destroy :cache_clear =begin @@ -44,7 +41,7 @@ get list of translations # use cache if not admin page is requested if !admin - data = cache_get(locale) + data = lang_cache_get(locale) return data if data end @@ -91,7 +88,7 @@ get list of translations # set cache if !admin - cache_set(locale, data) + lang_cache_set(locale, data) end data @@ -220,6 +217,23 @@ or [true, nil] end + def self.import(locale, translations) + bulk_import translations + lang_cache_clear(locale) + end + + def self.lang_cache_clear(locale) + Cache.delete lang_cache_key(locale) + end + + def self.lang_cache_set(locale, data) + Cache.write lang_cache_key(locale), data + end + + def self.lang_cache_get(locale) + Cache.read lang_cache_key(locale) + end + private def set_initial @@ -230,18 +244,13 @@ or true end - def cache_clear - Cache.delete("TranslationMapOnlyContent::#{locale.downcase}") - true + def cache_delete + super + self.class.lang_cache_clear(locale) # delete whole lang cache as well end - def self.cache_set(locale, data) - Cache.write("TranslationMapOnlyContent::#{locale.downcase}", data) + def self.lang_cache_key(locale) + "TranslationMapOnlyContent::#{locale.downcase}" end - private_class_method :cache_set - - def self.cache_get(locale) - Cache.read("TranslationMapOnlyContent::#{locale.downcase}") - end - private_class_method :cache_get + private_class_method :lang_cache_key end diff --git a/app/models/translation/synchronizes_from_po.rb b/app/models/translation/synchronizes_from_po.rb index 46ca39c23..6f53c7792 100644 --- a/app/models/translation/synchronizes_from_po.rb +++ b/app/models/translation/synchronizes_from_po.rb @@ -59,7 +59,7 @@ module Translation::SynchronizesFromPo updated_translation_ids.add t.id end - Translation.import importable_translations + Translation.import locale, importable_translations # Remove any unmodified & synchronized strings that are not present in the data any more. previous_unmodified_translations.reject { |t| updated_translation_ids.member? t.id }.each(&:destroy!) true diff --git a/spec/models/translation/translation_synchronizes_from_po_spec.rb b/spec/models/translation/translation_synchronizes_from_po_spec.rb index 2eeb28abf..fbf07700f 100644 --- a/spec/models/translation/translation_synchronizes_from_po_spec.rb +++ b/spec/models/translation/translation_synchronizes_from_po_spec.rb @@ -179,6 +179,14 @@ RSpec.describe Translation do 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') end end + + it 'clear cache after sync' do + allow(Cache).to receive(:delete) + + described_class.sync_locale_from_po('de-de') + + expect(Cache).to have_received(:delete).with('TranslationMapOnlyContent::de-de') + end end context 'when synchronizing strings for all locales' do