diff --git a/app/lib/texterify/export.rb b/app/lib/texterify/export.rb index 39e49d72..8f90d8a3 100644 --- a/app/lib/texterify/export.rb +++ b/app/lib/texterify/export.rb @@ -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 @@ -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 diff --git a/app/models/key.rb b/app/models/key.rb index d5fb2475..8e92bfe0 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -153,13 +153,15 @@ 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. @@ -167,7 +169,8 @@ def translation_for(language_id, export_config_id) 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