Skip to content

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

[49581] Translate names from common files when seeding

Christophe Bliard1846d207699b
diff --git a/app/seeders/source/seed_data_loader.rb b/app/seeders/source/seed_data_loader.rb
index 95ed929f15d..c602e97ff53 100644
--- a/app/seeders/source/seed_data_loader.rb
+++ b/app/seeders/source/seed_data_loader.rb
@@ -52,13 +52,15 @@ class Source::SeedDataLoader
end
def seed_data
- @seed_data ||= Source::SeedData.new(translate(raw_seed_files_content))
+ @seed_data ||= Source::SeedData.new(translated_seed_files_content)
end
- def raw_seed_files_content
- seed_files.reduce({}) do |raw_content, seed_file|
- raw_content.deep_merge(seed_file.raw_content)
- end
+ def translated_seed_files_content
+ seed_files
+ .map { |seed_file| translate_seed_file(seed_file) }
+ .reduce({}) do |merged_content, seed_file_content|
+ merged_content.deep_merge(seed_file_content)
+ end
end
private
@@ -66,4 +68,8 @@ class Source::SeedDataLoader
def seed_files
Source::SeedFile.with_names(seed_name, 'common')
end
+
+ def translate_seed_file(seed_file)
+ translate(seed_file.raw_content, "#{Source::Translate::I18N_PREFIX}.#{seed_file.name}")
+ end
end
diff --git a/app/seeders/source/translate.rb b/app/seeders/source/translate.rb
index 43354245998..5935a7d594a 100644
--- a/app/seeders/source/translate.rb
+++ b/app/seeders/source/translate.rb
@@ -33,7 +33,7 @@ module Source::Translate
TRANSLATABLE_PREFIX = 't_'
TRANSLATABLE_PREFIX_PATTERN = /^#{TRANSLATABLE_PREFIX}/
- def translate(hash, i18n_key = "#{I18N_PREFIX}.#{seed_name}")
+ def translate(hash, i18n_key)
translate_translatable_keys(hash, i18n_key)
translate_nested_enumerations(hash, i18n_key)
hash
diff --git a/modules/bim/spec/seeders/root_seeder_bim_edition_spec.rb b/modules/bim/spec/seeders/root_seeder_bim_edition_spec.rb
index 239b721c802..a1aad9cca7d 100644
--- a/modules/bim/spec/seeders/root_seeder_bim_edition_spec.rb
+++ b/modules/bim/spec/seeders/root_seeder_bim_edition_spec.rb
@@ -189,10 +189,10 @@ RSpec.describe RootSeeder,
end
end
- # Check that the tests are running with German as default language during seeding
it 'seeds with the specified language' do
expect(Status.where(name: 'Neu')).to exist
- expect(Type.where(name: 'Arbeitspaket')).to exist
+ expect(Type.where(name: 'Meilenstein')).to exist
+ expect(Color.where(name: 'Gelb')).to exist
end
include_examples 'creates BIM demo data'
diff --git a/spec/seeders/root_seeder_standard_edition_spec.rb b/spec/seeders/root_seeder_standard_edition_spec.rb
index 7ba9d97a526..3eed9314b2c 100644
--- a/spec/seeders/root_seeder_standard_edition_spec.rb
+++ b/spec/seeders/root_seeder_standard_edition_spec.rb
@@ -190,6 +190,9 @@ RSpec.describe RootSeeder,
it 'seeds with the specified language' do
willkommen = I18n.t("#{Source::Translate::I18N_PREFIX}.standard.welcome.title", locale: 'de')
expect(Setting.welcome_title).to eq(willkommen)
+ expect(Status.where(name: 'Neu')).to exist
+ expect(Type.where(name: 'Meilenstein')).to exist
+ expect(Color.where(name: 'Gelb')).to exist
end
it 'sets Setting.default_language to the given language' do
diff --git a/spec/seeders/source/seed_data_loader_spec.rb b/spec/seeders/source/seed_data_loader_spec.rb
index b2401b66cfd..26241956597 100644
--- a/spec/seeders/source/seed_data_loader_spec.rb
+++ b/spec/seeders/source/seed_data_loader_spec.rb
@@ -31,7 +31,9 @@
require 'spec_helper'
RSpec.describe Source::SeedDataLoader do
- subject(:loader) { described_class.new(seed_file_name:) }
+ let(:seed_name) { 'standard' }
+
+ subject(:loader) { described_class.new(seed_name:) }
def seed_file_double(name:, raw_content:)
instance_double(
@@ -45,19 +47,18 @@ RSpec.describe Source::SeedDataLoader do
it 'merges the data from seed files matching the given seed name' do
allow(Source::SeedFile).to receive(:all).and_return(
[
- seed_file_double(name: 'standard',
+ seed_file_double(name: seed_name,
raw_content: {
'data_from_file1' => 'hello'
}),
- seed_file_double(name: 'standard',
+ seed_file_double(name: seed_name,
raw_content: {
'data_from_file2' => 'world'
})
]
)
- loader = described_class.new(seed_name: 'standard')
- expect(loader.raw_seed_files_content).to eq(
+ expect(loader.translated_seed_files_content).to eq(
{
'data_from_file1' => 'hello',
'data_from_file2' => 'world'
@@ -68,7 +69,7 @@ RSpec.describe Source::SeedDataLoader do
it 'merges also the data from all common seed files regardless of the given seed name' do
allow(Source::SeedFile).to receive(:all).and_return(
[
- seed_file_double(name: 'standard',
+ seed_file_double(name: seed_name,
raw_content: {
'data_from_file1' => 'hello'
}),
@@ -82,12 +83,20 @@ RSpec.describe Source::SeedDataLoader do
})
]
)
+
+ expect(loader.translated_seed_files_content).to eq(
+ {
+ 'data_from_file1' => 'hello',
+ 'data_from_common_file1' => 'world',
+ 'data_from_common_file2' => '!!!'
+ }
+ )
end
it 'does not merge the data from seed files with a name different from the given name' do
allow(Source::SeedFile).to receive(:all).and_return(
[
- seed_file_double(name: 'standard',
+ seed_file_double(name: seed_name,
raw_content: {
'data_from_standard_file' => 'hello'
}),
@@ -102,8 +111,7 @@ RSpec.describe Source::SeedDataLoader do
]
)
- loader = described_class.new(seed_name: 'standard')
- expect(loader.raw_seed_files_content).to eq(
+ expect(loader.translated_seed_files_content).to eq(
{
'data_from_standard_file' => 'hello'
}
@@ -113,19 +121,18 @@ RSpec.describe Source::SeedDataLoader do
it 'deep merges hashes with identical paths' do
allow(Source::SeedFile).to receive(:all).and_return(
[
- seed_file_double(name: 'standard',
+ seed_file_double(name: seed_name,
raw_content: {
'welcome' => { title: 'welcome title' }
}),
- seed_file_double(name: 'standard',
+ seed_file_double(name: seed_name,
raw_content: {
'welcome' => { description: 'welcome description' }
})
]
)
- loader = described_class.new(seed_name: 'standard')
- expect(loader.raw_seed_files_content).to eq(
+ expect(loader.translated_seed_files_content).to eq(
{
'welcome' => {
title: 'welcome title',
@@ -138,19 +145,18 @@ RSpec.describe Source::SeedDataLoader do
it 'does not concat arrays with identical paths' do
allow(Source::SeedFile).to receive(:all).and_return(
[
- seed_file_double(name: 'standard',
+ seed_file_double(name: seed_name,
raw_content: {
'project' => { 'users' => ['Alice', 'Bob'] }
}),
- seed_file_double(name: 'standard',
+ seed_file_double(name: seed_name,
raw_content: {
'project' => { 'users' => ['Caroline', 'Devanshi'] }
})
]
)
- loader = described_class.new(seed_name: 'standard')
- expect(loader.raw_seed_files_content).to eq(
+ expect(loader.translated_seed_files_content).to eq(
{
'project' => {
'users' => ['Caroline', 'Devanshi'] # last one wins...
diff --git a/spec/seeders/source/translate_spec.rb b/spec/seeders/source/translate_spec.rb
index 6a8bcca14ea..e6f7e730c5b 100644
--- a/spec/seeders/source/translate_spec.rb
+++ b/spec/seeders/source/translate_spec.rb
@@ -36,21 +36,19 @@ RSpec.describe Source::Translate do
let(:title_fr) { 'Bienvenue sur OpenProject' }
let(:text_fr) { 'Apprenez à planifier des projets efficacement.' }
let(:locale) { 'fr' }
- let(:seed_name) { 'special_edition' }
- # a class including the Translate module needs to provide seed_name and locale
+ # a class including the Translate module needs to provide locale
subject(:translator) do
described_module = described_class
Class.new do
include described_module
- attr_reader :seed_name, :locale
+ attr_reader :locale
- def initialize(seed_name:, locale:)
- @seed_name = seed_name
+ def initialize(locale:)
@locale = locale
end
- end.new(seed_name:, locale:)
+ end.new(locale:)
end
def mock_translations(locale, translations_map)
@@ -68,8 +66,8 @@ RSpec.describe Source::Translate do
it 'translates keys with a "t_" prefix' do
mock_translations(
locale,
- "#{Source::Translate::I18N_PREFIX}.#{seed_name}.welcome.title" => title_fr,
- "#{Source::Translate::I18N_PREFIX}.#{seed_name}.welcome.text" => text_fr
+ "i18n_key_prefix.welcome.title" => title_fr,
+ "i18n_key_prefix.welcome.text" => text_fr
)
hash = {
'welcome' => {
@@ -79,7 +77,7 @@ RSpec.describe Source::Translate do
}
}
- translated = translator.translate(hash)
+ translated = translator.translate(hash, 'i18n_key_prefix')
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:')
@@ -88,8 +86,8 @@ RSpec.describe Source::Translate do
it 'translates nothing if prefix "t_" is absent' do
mock_translations(
locale,
- "#{Source::Translate::I18N_PREFIX}.#{seed_name}.welcome.title" => title_fr,
- "#{Source::Translate::I18N_PREFIX}.#{seed_name}.welcome.text" => text_fr
+ "i18n_key_prefix.welcome.title" => title_fr,
+ "i18n_key_prefix.welcome.text" => text_fr
)
hash = {
'welcome' => {
@@ -98,7 +96,7 @@ RSpec.describe Source::Translate do
}
}
- translated = translator.translate(hash)
+ translated = translator.translate(hash, 'i18n_key_prefix')
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)
@@ -112,7 +110,7 @@ RSpec.describe Source::Translate do
}
}
- translated = translator.translate(hash)
+ translated = translator.translate(hash, 'i18n_key_prefix')
expect(I18n).to have_received(:t).at_least(:twice)
expect(translated.dig('welcome', 'title')).to eq(title_en)
@@ -127,7 +125,7 @@ RSpec.describe Source::Translate do
}
}
- translated = translator.translate(hash)
+ translated = translator.translate(hash, 'i18n_key_prefix')
expect(translated['welcome']).to eq(
'title' => title_en,
'text' => text_en
@@ -140,8 +138,8 @@ RSpec.describe Source::Translate do
it 'translates each values using indices' do
mock_translations(
locale,
- "#{Source::Translate::I18N_PREFIX}.#{seed_name}.categories.item_0" => 'Erste Kategorie',
- "#{Source::Translate::I18N_PREFIX}.#{seed_name}.categories.item_1" => 'Zweite Kategorie'
+ "i18n_key_prefix.categories.item_0" => 'Erste Kategorie',
+ "i18n_key_prefix.categories.item_1" => 'Zweite Kategorie'
)
hash = {
't_categories' => [
@@ -151,7 +149,7 @@ RSpec.describe Source::Translate do
]
}
- translated = translator.translate(hash)
+ translated = translator.translate(hash, 'i18n_key_prefix')
expect(translated['categories'])
.to eq(['Erste Kategorie', 'Zweite Kategorie', 'Missing translations are kept as-is'])
end
@@ -161,16 +159,19 @@ RSpec.describe Source::Translate do
it 'translates keys in the nested values if they have translatable keys' do
mock_translations(
locale,
- "#{Source::Translate::I18N_PREFIX}.#{seed_name}.queries.item_0.name" => 'Plan projet',
- "#{Source::Translate::I18N_PREFIX}.#{seed_name}.queries.item_1.name" => 'Tâches'
+ "i18n_key_prefix.queries.item_0.name" => 'Plan projet',
+ "i18n_key_prefix.queries.item_1.name" => 'Tâches'
)
translated = translator.translate(
- 'queries' => [
- { 't_name' => 'Project plan', 'open' => true },
- { 't_name' => 'Tasks', 'open' => true },
- { 't_name' => 'Missing translations are kept as-is', 'open' => true }
- ]
+ {
+ 'queries' => [
+ { 't_name' => 'Project plan', 'open' => true },
+ { 't_name' => 'Tasks', 'open' => true },
+ { 't_name' => 'Missing translations are kept as-is', 'open' => true }
+ ]
+ },
+ 'i18n_key_prefix'
)
expect(translated['queries']).to eq(
[