Skip to content

Commit

Permalink
Improve export performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Werner authored and Christoph Werner committed May 12, 2024
1 parent 39bc124 commit feeb679
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
6 changes: 4 additions & 2 deletions app/lib/texterify/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ class Export
def self.create_language_export_data(project, export_config, language, post_processing_rules, **args)
export_data = {}

flavor = project.export_configs.find(export_config.id).flavor

# Load the translations for the given language and export config.
project
.keys
.includes(:translations)
.order_by_name
.each do |key|
key_translation = key.translation_for(language.id, export_config.id)
key_translation = key.translation_for(language.id, flavor)
translation_export_data = nil

if key_translation
Expand All @@ -47,7 +49,7 @@ def self.create_language_export_data(project, export_config, language, post_proc
while parent_language.present?
parent_language.keys.each do |key|
if export_data[key.name].nil? || export_data[key.name][:other].empty?
key_translation = key.translation_for(parent_language.id, export_config.id)
key_translation = key.translation_for(parent_language.id, flavor)
translation_export_data = nil

if key_translation
Expand Down
13 changes: 8 additions & 5 deletions app/models/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,24 @@ def check_placeholders
end

# Returns the translation of the key for the given language and export config.
def translation_for(language_id, export_config_id)
flavor = project.export_configs.find(export_config_id).flavor

def translation_for(language_id, flavor)
key_translation_flavor = nil
if flavor.present?
key_translation_flavor =
self.translations.where(language_id: language_id, flavor_id: flavor.id).order(created_at: :desc).first
self
.translations
.sort_by(&:created_at)
.reverse
.find { |t| t.language_id == language_id && t.flavor_id == flavor.id }
end

# If there is a flavor translation use it.
if key_translation_flavor&.content.present?
key_translation = key_translation_flavor
else
# Otherwise use the default translation of the language.
key_translation = self.translations.where(language_id: language_id, flavor_id: nil).order(created_at: :desc).first
key_translation =
self.translations.sort_by(&:created_at).reverse.find { |t| t.language_id == language_id && t.flavor_id.nil? }
end

key_translation
Expand Down

0 comments on commit feeb679

Please sign in to comment.