OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Translate seed data for keys starting with 't_'
Christophe Bliard862a91bcb308
diff --git a/app/seeders/root_seeder.rb b/app/seeders/root_seeder.rbindex 754b1cbb614..3d94e155477 100644--- a/app/seeders/root_seeder.rb+++ b/app/seeders/root_seeder.rb@@ -28,28 +28,20 @@# Seeds the minimum data required to run OpenProject (BasicDataSeeder, AdminUserSeeder)# as well as optional demo data (DemoDataSeeder) to give a user some orientation.-class RootSeeder < Seederdef initialize(seed_development_data: Rails.env.development?)super()- require 'basic_data_seeder'- require 'demo_data_seeder'- require 'development_data_seeder'@seed_development_data = seed_development_data- rails_engines.each { |engine| load_engine_seeders! engine }+ load_available_seedersend# Returns the demo data in the default language.def seed_dataraise 'cannot generate demo seed data without setting locale first' unless @locale_set- @seed_data ||=- OpenProject::Configuration['edition']- .then { |edition| edition == 'bim' ? 'modules/bim/app/seeders/bim.yml' : 'app/seeders/standard.yml' }- .then { |path| YAML.load_file(Rails.root.join(path)) }- .then { |yaml_content| SeedData.new(yaml_content) }+ @seed_data ||= SeedDataLoader.get_dataenddef seed_data!@@ -76,15 +68,24 @@ class RootSeeder < Seeder@seed_development_dataend- def rails_engines- ::Rails::Engine.subclasses.map(&:instance)+ private++ # Load all seeders so that they can be discovered when doing+ # `Seeder.subclasses`.+ def load_available_seeders+ load_engine_seeders(Rails)+ rails_engines.each { |engine| load_engine_seeders engine }end- def load_engine_seeders!(engine)- Dir[engine.root.join('app/seeders/**/*.rb')]+ def load_engine_seeders(engine)+ engine.root.glob('app/seeders/**/*.rb').each { |file| require file }end+ def rails_engines+ ::Rails::Engine.subclasses.map(&:instance)+ end+### Clears some schema caches and column information.def reset_active_record!@@ -123,8 +124,6 @@ class RootSeeder < SeederDelayed::Worker.delay_jobs = previous_delay_jobsend- private-def seed_basic_dataprint_status "*** Seeding basic data for #{OpenProject::Configuration['edition']} edition"::Standard::BasicDataSeeder.new(seed_data).seed!diff --git a/app/seeders/seed_data.rb b/app/seeders/seed_data.rbindex 3dd0cf987ff..082679fb692 100644--- a/app/seeders/seed_data.rb+++ b/app/seeders/seed_data.rb@@ -30,7 +30,7 @@class SeedDatadef initialize(data)- @data = data.deep_stringify_keys+ @data = dataenddef lookup(path)diff --git a/app/seeders/seed_data_loader.rb b/app/seeders/seed_data_loader.rbnew file mode 100644index 00000000000..7dd9662adfe--- /dev/null+++ b/app/seeders/seed_data_loader.rb@@ -0,0 +1,102 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++class SeedDataLoader+ class << self+ def get_data(edition: nil)+ edition ||= OpenProject::Configuration['edition']+ loader = new(edition:)+ loader.seed_data+ end+ end++ attr_reader :edition, :locale++ def initialize(edition: 'standard', locale: I18n.locale)+ @edition = edition+ @locale = locale+ end++ def seed_data+ @seed_data ||= SeedData.new(translate(raw_seed_data))+ end++ def translate(hash, i18n_key = "seeds.#{edition}")+ translate_translatable_keys(hash, i18n_key)+ translate_nested_enumerations(hash, i18n_key)+ hash+ end++ private++ def translate_translatable_keys(hash, i18n_key)+ translatable_keys(hash).each do |key|+ value = hash.delete("t_#{key}")+ hash[key] = translate_value(value, "#{i18n_key}.#{key}")+ end+ end++ def translatable_keys(hash)+ hash.keys+ .filter { _1.start_with?('t_') }+ .map { _1.gsub(/^t_/, '') }+ end++ def translate_value(value, i18n_key)+ case value+ when String+ I18n.t(i18n_key, locale:, default: value)+ when Array+ value.map.with_index { |v, i| translate_value(v, "#{i18n_key}.#{i}") }+ end+ end++ def translate_nested_enumerations(hash, i18n_key)+ hash.each do |key, value|+ case value+ when Hash+ translate(value, "#{i18n_key}.#{key}")+ when Array+ value+ .filter { |v| v.is_a?(Hash) }+ .each_with_index { |h, i| translate(h, "#{i18n_key}.#{key}.#{i}") }+ end+ end+ end++ def raw_seed_data+ YAML.load_file(seed_file_path).deep_stringify_keys!+ end++ def seed_file_path+ path = edition == 'bim' ? 'modules/bim/app/seeders/bim.yml' : 'app/seeders/standard.yml'+ Rails.root.join(path)+ end+enddiff --git a/spec/seeders/seed_data_loader_spec.rb b/spec/seeders/seed_data_loader_spec.rbnew file mode 100644index 00000000000..fd814f08781--- /dev/null+++ b/spec/seeders/seed_data_loader_spec.rb@@ -0,0 +1,168 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'++RSpec.describe SeedDataLoader do+ let(:title_en) { 'Welcome to OpenProject' }+ let(:text_en) { 'Learn how to plan projects efficiently.' }+ let(:title_fr) { 'Bienvenue sur OpenProject' }+ let(:text_fr) { 'Apprenez à planifier des projets efficacement.' }++ def mock_translations(locale, translations_map)+ translations_map.each do |key, translation|+ allow(I18n).to receive(:t).with(key, hash_including(locale: locale.to_s)).and_return(translation)+ allow(I18n).to receive(:t).with(key, hash_including(locale: locale.to_sym)).and_return(translation)+ end+ end++ before do+ allow(I18n).to receive(:t).and_call_original+ end++ describe '#translate' do+ it 'translates keys with a "t_" prefix' do+ loader = described_class.new(locale: 'fr')+ mock_translations(+ 'fr',+ 'seeds.standard.welcome.title' => title_fr,+ 'seeds.standard.welcome.text' => text_fr+ )+ hash = {+ 'welcome' => {+ 't_title' => title_en,+ 't_text' => text_en,+ 'icon' => ':smile:'+ }+ }++ translated = loader.translate(hash)+ expect(translated.dig('welcome', 'title')).to eq(title_fr)+ expect(translated.dig('welcome', 'text')).to eq(text_fr)+ expect(translated.dig('welcome', 'icon')).to eq(':smile:')+ end++ it 'translates nothing if prefix "t_" is absent' do+ loader = described_class.new(locale: 'fr')++ hash = {+ 'welcome' => {+ 'title' => title_en,+ 'text' => text_en+ }+ }++ translated = loader.translate(hash)+ expect(I18n).not_to have_received(:t)+ expect(translated.dig('welcome', 'title')).to eq(title_en)+ expect(translated.dig('welcome', 'text')).to eq(text_en)+ end++ it 'uses the original string if no translation exists' do+ loader = described_class.new(locale: 'fr')+ hash = {+ 'welcome' => {+ 't_title' => title_en,+ 't_text' => text_en+ }+ }++ translated = loader.translate(hash)++ expect(I18n).to have_received(:t).at_least(:twice)+ expect(translated.dig('welcome', 'title')).to eq(title_en)+ expect(translated.dig('welcome', 'text')).to eq(text_en)+ end++ it 'removes the prefixed keys from the returned hash' do+ loader = described_class.new(locale: 'fr')+ hash = {+ 'welcome' => {+ 't_title' => title_en,+ 't_text' => text_en+ }+ }++ translated = loader.translate(hash)+ expect(translated['welcome']).to eq(+ 'title' => title_en,+ 'text' => text_en+ )+ end++ context 'when the value to translate is an array' do+ it 'translates each values using indices' do+ loader = described_class.new(locale: 'de')+ mock_translations(+ 'de',+ 'seeds.standard.categories.0' => 'Erste Kategorie',+ 'seeds.standard.categories.1' => 'Zweite Kategorie'+ )+ hash = {+ 't_categories' => [+ 'First category',+ 'Second category',+ 'Missing translations are kept as-is'+ ]+ }++ translated = loader.translate(hash)+ expect(translated['categories'])+ .to eq(['Erste Kategorie', 'Zweite Kategorie', 'Missing translations are kept as-is'])+ end+ end++ context 'when hash contains array of hashes' do+ it 'translates keys in the nested values if they have translatable keys' do+ loader = described_class.new(locale: 'fr')+ mock_translations(+ 'fr',+ 'seeds.standard.queries.0.name' => 'Plan projet',+ 'seeds.standard.queries.1.name' => 'Tâches'+ )++ translated = loader.translate(+ 'queries' => [+ { 't_name' => 'Project plan', 'open' => true },+ { 't_name' => 'Tasks', 'open' => true },+ { 't_name' => 'Missing translations are kept as-is', 'open' => true }+ ]+ )+ expect(translated['queries']).to eq(+ [+ { 'name' => 'Plan projet', 'open' => true },+ { 'name' => 'Tâches', 'open' => true },+ { 'name' => 'Missing translations are kept as-is', 'open' => true }+ ]+ )+ end+ end+ end+end