From 4d8011032ef21b8707ff3d3313a3420e1f0bc2f9 Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Wed, 5 Oct 2022 15:22:16 -0400 Subject: [PATCH] DEV: Add a rake task to export/import translation overrides (#18487) Use `bin/rake export:translation_overrides` to export to a file. Then, copy that file to a new site and run `bin/rake import:file["filename"]. --- lib/import_export.rb | 4 ++++ lib/import_export/base_exporter.rb | 6 +++++ lib/import_export/importer.rb | 14 +++++++++++ .../translation_overrides_exporter.rb | 24 +++++++++++++++++++ lib/tasks/export.rake | 8 +++++++ spec/fixtures/json/import-export.json | 3 +++ spec/import_export/importer_spec.rb | 1 + 7 files changed, 60 insertions(+) create mode 100644 lib/import_export/translation_overrides_exporter.rb diff --git a/lib/import_export.rb b/lib/import_export.rb index 6a32d686f13..13d2b5eddf3 100644 --- a/lib/import_export.rb +++ b/lib/import_export.rb @@ -6,6 +6,7 @@ require "import_export/category_structure_exporter" require "import_export/category_exporter" require "import_export/topic_exporter" require "import_export/group_exporter" +require "import_export/translation_overrides_exporter" require "json" module ImportExport @@ -31,4 +32,7 @@ module ImportExport ImportExport::GroupExporter.new(include_users).perform.save_to_file(filename) end + def self.export_translation_overrides(filename = nil) + ImportExport::TranslationOverridesExporter.new.perform.save_to_file(filename) + end end diff --git a/lib/import_export/base_exporter.rb b/lib/import_export/base_exporter.rb index 5a7f4a61956..989b7220e4d 100644 --- a/lib/import_export/base_exporter.rb +++ b/lib/import_export/base_exporter.rb @@ -178,6 +178,12 @@ module ImportExport data end + def export_translation_overrides + @export_data[:translation_overrides] = TranslationOverride.all.select(:locale, :translation_key, :value) + + self + end + def default_filename_prefix raise "Overwrite me!" end diff --git a/lib/import_export/importer.rb b/lib/import_export/importer.rb index a800b45d9b9..c9d03030a15 100644 --- a/lib/import_export/importer.rb +++ b/lib/import_export/importer.rb @@ -10,6 +10,7 @@ module ImportExport @groups = data[:groups] @categories = data[:categories] @topics = data[:topics] + @translation_overrides = data[:translation_overrides] # To support legacy `category_export` script if data[:category].present? @@ -25,6 +26,7 @@ module ImportExport import_groups import_categories import_topics + import_translation_overrides self ensure @@ -167,6 +169,18 @@ module ImportExport self end + def import_translation_overrides + return if @translation_overrides.blank? + + puts "Importing translation overrides..." + + @translation_overrides.each do |tu| + TranslationOverride.upsert!(tu[:locale], tu[:translation_key], tu[:value]) + end + + TranslationOverride.reload_all_overrides! + end + def new_user_id(external_user_id) ucf = UserCustomField.where(name: "import_id", value: "#{external_user_id}#{import_source}").first ucf ? ucf.user_id : Discourse::SYSTEM_USER_ID diff --git a/lib/import_export/translation_overrides_exporter.rb b/lib/import_export/translation_overrides_exporter.rb new file mode 100644 index 00000000000..7094248a99f --- /dev/null +++ b/lib/import_export/translation_overrides_exporter.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module ImportExport + class TranslationOverridesExporter < BaseExporter + + def initialize() + @export_data = { + translation_overrides: [] + } + end + + def perform + puts "Exporting all translation overrides...", "" + export_translation_overrides + + self + end + + def default_filename_prefix + "translation-overrides" + end + + end +end diff --git a/lib/tasks/export.rake b/lib/tasks/export.rake index 1e58a3f9a86..1b4a63ed2fe 100644 --- a/lib/tasks/export.rake +++ b/lib/tasks/export.rake @@ -24,3 +24,11 @@ task 'export:groups', [:include_group_users, :file_name] => [:environment] do |_ ImportExport.export_groups(args[:include_group_users], args[:file_name]) puts "", "Done", "" end + +desc 'Export all translation overrides' +task 'export:translation_overrides' => [:environment] do |_, args| + require "import_export" + + ImportExport.export_translation_overrides + puts "", "Done", "" +end diff --git a/spec/fixtures/json/import-export.json b/spec/fixtures/json/import-export.json index f852a9def17..0d2288cbcdb 100644 --- a/spec/fixtures/json/import-export.json +++ b/spec/fixtures/json/import-export.json @@ -27,5 +27,8 @@ {"id":7,"user_id":-1,"post_number":2,"raw":"Edit the first post in this topic to change the contents of the FAQ/Guidelines page.","created_at":"2017-10-26T17:15:03.822Z","reply_to_post_number":null,"hidden":false,"hidden_reason_id":null,"wiki":false} ] } + ], + "translation_overrides":[ + {"locale":"en","translation_key":"login_required.welcome_message","value":"## [Dude, Welcome to %{title}](#welcome)\nAn account is required. Please create an account or log in to continue again!\n"} ] } diff --git a/spec/import_export/importer_spec.rb b/spec/import_export/importer_spec.rb index b9f6581aadc..39009ddabfb 100644 --- a/spec/import_export/importer_spec.rb +++ b/spec/import_export/importer_spec.rb @@ -102,6 +102,7 @@ RSpec.describe ImportExport::Importer do .and change { Group.count }.by(2) .and change { Topic.count }.by(8) .and change { User.count }.by(2) + .and change { TranslationOverride.count }.by(1) end end end