-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Make sure duplicates are not importer from the import data - Make sure existing translation keys are not overridden, only new keys will be imported - Make sure the translations for the imported keys are created for all available organization locales
- Loading branch information
Showing
6 changed files
with
173 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
lib/decidim/term_customizer/translation_import_collection.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module TermCustomizer | ||
class TranslationImportCollection | ||
def initialize(translation_set, records, locales) | ||
@translation_set = translation_set | ||
@records = records | ||
@locales = locales | ||
end | ||
|
||
def import_attributes | ||
attributes_for_all_locales | ||
end | ||
|
||
private | ||
|
||
attr_reader :translation_set, :records, :locales | ||
|
||
def collection_attributes | ||
@collection_attributes ||= records.map do |translation| | ||
# Skip all translation keys that already exists | ||
next if translation_set.translations.where( | ||
key: translation.key | ||
).present? | ||
|
||
{ | ||
locale: translation.locale, | ||
key: translation.key, | ||
value: translation.value | ||
} | ||
end.compact | ||
end | ||
|
||
def unique_attributes | ||
@unique_attributes ||= collection_attributes.uniq do |attr| | ||
"#{attr[:locale]}.#{attr[:key]}" | ||
end | ||
end | ||
|
||
def attribute_keys | ||
@attribute_keys ||= unique_attributes.map { |attr| attr[:key] }.uniq | ||
end | ||
|
||
def attributes_for_all_locales | ||
@attributes_for_all_locales ||= attribute_keys.map do |key| | ||
locales.map do |locale| | ||
# Find if the item with the locale already exists in the | ||
# unique collection. | ||
item = unique_attributes.find do |attr| | ||
attr[:key] == key && attr[:locale] == locale.to_s | ||
end | ||
|
||
# In case the item does not exist for the key and locale, create | ||
# a new item with the default I18n translation. Otherwise, return | ||
# the found item to the final array. | ||
if item.nil? | ||
{ | ||
key: key, | ||
locale: locale.to_s, | ||
value: I18n.t(key, locale: locale, default: "") | ||
} | ||
else | ||
item | ||
end | ||
end | ||
end.flatten | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
spec/lib/decidim/term_customizer/translation_import_collection_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe Decidim::TermCustomizer::TranslationImportCollection do | ||
subject { described_class.new(translation_set, records, locales) } | ||
|
||
let(:translation_set) { create(:translation_set) } | ||
let(:records) do | ||
build_list(:translation, 10, translation_set: translation_set) | ||
end | ||
let(:locales) { [:en] } | ||
|
||
describe "#import_attributes" do | ||
let(:expected_attributes) do | ||
records.map do |tr| | ||
{ locale: tr.locale, key: tr.key, value: tr.value } | ||
end | ||
end | ||
|
||
it "returns the correct attributes" do | ||
expect(subject.import_attributes).to eq(expected_attributes) | ||
end | ||
|
||
context "with multiple locales" do | ||
let(:locales) { [:en, :fi, :sv] } | ||
|
||
let(:expected_attributes) do | ||
records.map do |tr| | ||
locales.map do |locale| | ||
if locale.to_s == tr.locale | ||
{ locale: tr.locale, key: tr.key, value: tr.value } | ||
else | ||
{ locale: locale.to_s, key: tr.key, value: "" } | ||
end | ||
end | ||
end.flatten | ||
end | ||
|
||
before do | ||
I18n.available_locales = locales | ||
end | ||
|
||
it "returns the correct attributes" do | ||
expect(subject.import_attributes).to eq(expected_attributes) | ||
end | ||
end | ||
|
||
context "with duplicate keys" do | ||
let(:records) do | ||
duplicate_attributes.map do |attr| | ||
build( | ||
:translation, | ||
locale: attr[:locale], | ||
key: attr[:key], | ||
value: attr[:value], | ||
translation_set: translation_set | ||
) | ||
end | ||
end | ||
|
||
let(:duplicate_attributes) do | ||
[ | ||
{ locale: "en", key: "key1", value: "Value1" }, | ||
{ locale: "en", key: "key1", value: "Value1-1" }, | ||
{ locale: "en", key: "key2", value: "Value2" }, | ||
{ locale: "en", key: "key2", value: "Value2-2" } | ||
] | ||
end | ||
|
||
let(:expected_attributes) do | ||
[ | ||
{ locale: "en", key: "key1", value: "Value1" }, | ||
{ locale: "en", key: "key2", value: "Value2" } | ||
] | ||
end | ||
|
||
it "returns the correct attributes with duplicates removed" do | ||
expect(subject.import_attributes).to eq(expected_attributes) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters