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"].
This commit is contained in:
Penar Musaraj 2022-10-05 15:22:16 -04:00 committed by GitHub
parent 15f192447b
commit 4d8011032e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 0 deletions

View File

@ -6,6 +6,7 @@ require "import_export/category_structure_exporter"
require "import_export/category_exporter" require "import_export/category_exporter"
require "import_export/topic_exporter" require "import_export/topic_exporter"
require "import_export/group_exporter" require "import_export/group_exporter"
require "import_export/translation_overrides_exporter"
require "json" require "json"
module ImportExport module ImportExport
@ -31,4 +32,7 @@ module ImportExport
ImportExport::GroupExporter.new(include_users).perform.save_to_file(filename) ImportExport::GroupExporter.new(include_users).perform.save_to_file(filename)
end end
def self.export_translation_overrides(filename = nil)
ImportExport::TranslationOverridesExporter.new.perform.save_to_file(filename)
end
end end

View File

@ -178,6 +178,12 @@ module ImportExport
data data
end end
def export_translation_overrides
@export_data[:translation_overrides] = TranslationOverride.all.select(:locale, :translation_key, :value)
self
end
def default_filename_prefix def default_filename_prefix
raise "Overwrite me!" raise "Overwrite me!"
end end

View File

@ -10,6 +10,7 @@ module ImportExport
@groups = data[:groups] @groups = data[:groups]
@categories = data[:categories] @categories = data[:categories]
@topics = data[:topics] @topics = data[:topics]
@translation_overrides = data[:translation_overrides]
# To support legacy `category_export` script # To support legacy `category_export` script
if data[:category].present? if data[:category].present?
@ -25,6 +26,7 @@ module ImportExport
import_groups import_groups
import_categories import_categories
import_topics import_topics
import_translation_overrides
self self
ensure ensure
@ -167,6 +169,18 @@ module ImportExport
self self
end 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) def new_user_id(external_user_id)
ucf = UserCustomField.where(name: "import_id", value: "#{external_user_id}#{import_source}").first ucf = UserCustomField.where(name: "import_id", value: "#{external_user_id}#{import_source}").first
ucf ? ucf.user_id : Discourse::SYSTEM_USER_ID ucf ? ucf.user_id : Discourse::SYSTEM_USER_ID

View File

@ -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

View File

@ -24,3 +24,11 @@ task 'export:groups', [:include_group_users, :file_name] => [:environment] do |_
ImportExport.export_groups(args[:include_group_users], args[:file_name]) ImportExport.export_groups(args[:include_group_users], args[:file_name])
puts "", "Done", "" puts "", "Done", ""
end end
desc 'Export all translation overrides'
task 'export:translation_overrides' => [:environment] do |_, args|
require "import_export"
ImportExport.export_translation_overrides
puts "", "Done", ""
end

View File

@ -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} {"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"}
] ]
} }

View File

@ -102,6 +102,7 @@ RSpec.describe ImportExport::Importer do
.and change { Group.count }.by(2) .and change { Group.count }.by(2)
.and change { Topic.count }.by(8) .and change { Topic.count }.by(8)
.and change { User.count }.by(2) .and change { User.count }.by(2)
.and change { TranslationOverride.count }.by(1)
end end
end end
end end