2016-12-06 10:46:26 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Translation do
|
|
|
|
|
2018-10-18 12:16:29 +00:00
|
|
|
before(:all) do
|
|
|
|
Translation.where(locale: 'de-de').destroy_all
|
|
|
|
Translation.sync('de-de')
|
|
|
|
end
|
2018-09-04 11:37:51 +00:00
|
|
|
|
2019-02-10 11:01:38 +00:00
|
|
|
context 'default string translations' do
|
2016-12-06 10:46:26 +00:00
|
|
|
|
|
|
|
it 'en with existing word' do
|
|
|
|
expect(Translation.translate('en', 'New')).to eq('New')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'en-us with existing word' do
|
|
|
|
expect(Translation.translate('en-us', 'New')).to eq('New')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'en with not existing word' do
|
|
|
|
expect(Translation.translate('en', 'Some Not Existing Word')).to eq('Some Not Existing Word')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'de-de with existing word' do
|
|
|
|
expect(Translation.translate('de-de', 'New')).to eq('Neu')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'de-de with existing word' do
|
|
|
|
expect(Translation.translate('de-de', 'Some Not Existing Word')).to eq('Some Not Existing Word')
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-02-10 11:01:38 +00:00
|
|
|
context 'default timestamp translations' do
|
|
|
|
|
|
|
|
it 'de-de with array' do
|
|
|
|
expect(Translation.timestamp('de-de', 'Europe/Berlin', ['some value'])).to eq('["some value"]')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'not_existing with timestamp as string' do
|
|
|
|
expect(Translation.timestamp('not_existing', 'Europe/Berlin', '2018-10-10T10:00:00Z0')).to eq('2018-10-10 10:00:00 UTC')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'not_existing with time object' do
|
|
|
|
expect(Translation.timestamp('not_existing', 'Europe/Berlin', Time.zone.parse('2018-10-10T10:00:00Z0'))).to eq('2018-10-10 10:00:00 UTC')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'not_existing with invalid timestamp string' do
|
|
|
|
expect(Translation.timestamp('not_existing', 'Europe/Berlin', 'something')).to eq('something')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'en-us with invalid time zone' do
|
|
|
|
expect(Translation.timestamp('en-us', 'Europe/Berlin', '2018-10-10T10:00:00Z0')).to eq('10/10/2018 12:00 (Europe/Berlin)')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'en-us with timestamp as string' do
|
|
|
|
expect(Translation.timestamp('en-us', 'Europe/Berlin', '2018-10-10T10:00:00Z0')).to eq('10/10/2018 12:00 (Europe/Berlin)')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'en-us with time object' do
|
|
|
|
expect(Translation.timestamp('en-us', 'Europe/Berlin', Time.zone.parse('2018-10-10T10:00:00Z0'))).to eq('10/10/2018 12:00 (Europe/Berlin)')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'de-de with timestamp as string' do
|
|
|
|
expect(Translation.timestamp('de-de', 'Europe/Berlin', '2018-10-10T10:00:00Z0')).to eq('10.10.2018 12:00 (Europe/Berlin)')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'de-de with time object' do
|
|
|
|
expect(Translation.timestamp('de-de', 'Europe/Berlin', Time.zone.parse('2018-10-10T10:00:00Z0'))).to eq('10.10.2018 12:00 (Europe/Berlin)')
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'default date translations' do
|
|
|
|
|
|
|
|
it 'de-de with array' do
|
|
|
|
expect(Translation.date('de-de', ['some value'])).to eq('["some value"]')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'not_existing with date as string' do
|
|
|
|
expect(Translation.date('not_existing', '2018-10-10')).to eq('2018-10-10')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'not_existing with date object' do
|
|
|
|
expect(Translation.date('not_existing', Date.parse('2018-10-10'))).to eq('2018-10-10')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'not_existing with invalid data as string' do
|
|
|
|
expect(Translation.date('not_existing', 'something')).to eq('something')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'en-us with date as string' do
|
|
|
|
expect(Translation.date('en-us', '2018-10-10')).to eq('10/10/2018')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'en-us with date object' do
|
|
|
|
expect(Translation.date('en-us', Date.parse('2018-10-10'))).to eq('10/10/2018')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'de-de with date as string' do
|
|
|
|
expect(Translation.date('de-de', '2018-10-10')).to eq('10.10.2018')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'de-de with date object' do
|
|
|
|
expect(Translation.date('de-de', Date.parse('2018-10-10'))).to eq('10.10.2018')
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-09-04 11:37:51 +00:00
|
|
|
context 'remote_translation_need_update? tests' do
|
|
|
|
|
|
|
|
it 'translation is still the same' do
|
|
|
|
translation = Translation.where(locale: 'de-de', format: 'string').last
|
|
|
|
translations = Translation.where(locale: 'de-de').pluck(:id, :locale, :source, :format, :target, :target_initial).to_a
|
|
|
|
expect(
|
|
|
|
Translation.remote_translation_need_update?(
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
'source' => translation.source,
|
|
|
|
'format' => translation.format,
|
|
|
|
'locale' => translation.locale,
|
|
|
|
'target' => translation.target,
|
2018-09-04 11:37:51 +00:00
|
|
|
'target_initial' => translation.target_initial,
|
|
|
|
}, translations
|
|
|
|
)
|
|
|
|
).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'translation target has locally changed' do
|
|
|
|
translation = Translation.where(locale: 'de-de', format: 'string').last
|
|
|
|
translation.target = 'some new translation'
|
|
|
|
translation.save!
|
|
|
|
translations = Translation.where(locale: 'de-de').pluck(:id, :locale, :source, :format, :target, :target_initial).to_a
|
|
|
|
expect(
|
|
|
|
Translation.remote_translation_need_update?(
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
'source' => translation.source,
|
|
|
|
'format' => translation.format,
|
|
|
|
'locale' => translation.locale,
|
|
|
|
'target' => translation.target,
|
2018-09-04 11:37:51 +00:00
|
|
|
'target_initial' => translation.target_initial,
|
|
|
|
}, translations
|
|
|
|
)
|
|
|
|
).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'translation target has remotely changed' do
|
|
|
|
translation = Translation.where(locale: 'de-de', format: 'string').last
|
|
|
|
translations = Translation.where(locale: 'de-de').pluck(:id, :locale, :source, :format, :target, :target_initial).to_a
|
|
|
|
(result, translation_result) = Translation.remote_translation_need_update?(
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
'source' => translation.source,
|
|
|
|
'format' => translation.format,
|
|
|
|
'locale' => translation.locale,
|
|
|
|
'target' => 'some new translation by remote',
|
2018-09-04 11:37:51 +00:00
|
|
|
'target_initial' => 'some new translation by remote',
|
|
|
|
}, translations
|
|
|
|
)
|
|
|
|
expect(result).to be true
|
|
|
|
expect(translation_result.attributes).to eq translation.attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'translation target has remotely and locally changed' do
|
|
|
|
translation = Translation.where(locale: 'de-de', format: 'string').last
|
|
|
|
translation.target = 'some new translation'
|
|
|
|
translation.save!
|
|
|
|
translations = Translation.where(locale: 'de-de').pluck(:id, :locale, :source, :format, :target, :target_initial).to_a
|
|
|
|
expect(
|
|
|
|
Translation.remote_translation_need_update?(
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
'source' => translation.source,
|
|
|
|
'format' => translation.format,
|
|
|
|
'locale' => translation.locale,
|
|
|
|
'target' => 'some new translation by remote',
|
2018-09-04 11:37:51 +00:00
|
|
|
'target_initial' => 'some new translation by remote',
|
|
|
|
}, translations
|
|
|
|
)
|
|
|
|
).to be false
|
|
|
|
end
|
2016-12-06 10:46:26 +00:00
|
|
|
|
2018-09-04 11:37:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'custom translation tests' do
|
2016-12-06 10:46:26 +00:00
|
|
|
|
|
|
|
it 'cycle of change and reload translation' do
|
2018-09-04 11:37:51 +00:00
|
|
|
locale = 'de-de'
|
2016-12-06 10:46:26 +00:00
|
|
|
|
|
|
|
# check for non existing custom changes
|
|
|
|
list = Translation.lang(locale)
|
2017-10-01 12:25:52 +00:00
|
|
|
list['list'].each do |item|
|
2016-12-06 10:46:26 +00:00
|
|
|
translation = Translation.find_by(source: item[1], locale: locale)
|
|
|
|
expect(translation.class).to be(Translation)
|
|
|
|
expect(locale).to eq(translation.locale)
|
|
|
|
expect(translation.target).to eq(translation.target_initial)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-12-06 10:46:26 +00:00
|
|
|
|
|
|
|
# add custom changes
|
|
|
|
translation = Translation.find_by(locale: locale, source: 'open')
|
2016-12-06 11:07:17 +00:00
|
|
|
expect(translation.class).to be(Translation)
|
2016-12-06 10:46:26 +00:00
|
|
|
expect(translation.target).to eq('offen')
|
|
|
|
expect(translation.target_initial).to eq('offen')
|
|
|
|
translation.target = 'offen2'
|
|
|
|
translation.save!
|
|
|
|
|
|
|
|
list = Translation.lang(locale)
|
2017-10-01 12:25:52 +00:00
|
|
|
list['list'].each do |item|
|
2016-12-06 10:46:26 +00:00
|
|
|
translation = Translation.find_by(source: item[1], locale: locale)
|
|
|
|
expect(translation.class).to be(Translation)
|
|
|
|
expect(locale).to eq(translation.locale)
|
|
|
|
if translation.source == 'open'
|
|
|
|
expect(translation.target).to eq('offen2')
|
|
|
|
expect(translation.target_initial).to eq('offen')
|
|
|
|
else
|
|
|
|
expect(translation.target).to eq(translation.target_initial)
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-12-06 10:46:26 +00:00
|
|
|
|
|
|
|
# check for existing custom changes after new translations are loaded
|
|
|
|
Translation.load(locale)
|
|
|
|
list = Translation.lang(locale)
|
2017-10-01 12:25:52 +00:00
|
|
|
list['list'].each do |item|
|
2016-12-06 10:46:26 +00:00
|
|
|
translation = Translation.find_by(source: item[1], locale: locale)
|
|
|
|
expect(translation.class).to be(Translation)
|
|
|
|
expect(locale).to eq(translation.locale)
|
|
|
|
if translation.source == 'open'
|
|
|
|
expect(translation.target).to eq('offen2')
|
|
|
|
expect(translation.target_initial).to eq('offen')
|
|
|
|
else
|
|
|
|
expect(translation.target).to eq(translation.target_initial)
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-12-06 10:46:26 +00:00
|
|
|
|
|
|
|
# reset custom translations and check for non existing custom changes
|
|
|
|
Translation.reset(locale)
|
|
|
|
list = Translation.lang(locale)
|
2017-10-01 12:25:52 +00:00
|
|
|
list['list'].each do |item|
|
2016-12-06 10:46:26 +00:00
|
|
|
translation = Translation.find_by(source: item[1], locale: locale)
|
|
|
|
expect(translation.class).to be(Translation)
|
|
|
|
expect(locale).to eq(translation.locale)
|
|
|
|
expect(translation.target).to eq(translation.target_initial)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-12-06 10:46:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'file based import' do
|
|
|
|
|
|
|
|
it 'check download of locales' do
|
2017-02-10 15:00:58 +00:00
|
|
|
version = Version.get
|
2016-12-06 10:46:26 +00:00
|
|
|
directory = Rails.root.join('config')
|
2017-11-23 08:09:44 +00:00
|
|
|
file = Rails.root.join(directory, "locales-#{version}.yml")
|
2016-12-06 10:46:26 +00:00
|
|
|
if File.exist?(file)
|
|
|
|
File.delete(file)
|
|
|
|
end
|
|
|
|
expect(File.exist?(file)).to be false
|
|
|
|
Locale.fetch
|
|
|
|
expect(File.exist?(file)).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'check download of translations' do
|
2017-02-10 15:16:18 +00:00
|
|
|
version = Version.get
|
2016-12-06 10:46:26 +00:00
|
|
|
locale = 'de-de'
|
2017-11-23 08:09:44 +00:00
|
|
|
directory = Rails.root.join('config', 'translations')
|
2016-12-06 10:46:26 +00:00
|
|
|
if File.directory?(directory)
|
|
|
|
FileUtils.rm_rf(directory)
|
|
|
|
end
|
2017-11-23 08:09:44 +00:00
|
|
|
file = Rails.root.join(directory, "#{locale}-#{version}.yml")
|
2016-12-06 10:46:26 +00:00
|
|
|
expect(File.exist?(file)).to be false
|
|
|
|
Translation.fetch(locale)
|
|
|
|
expect(File.exist?(file)).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-09-04 11:37:51 +00:00
|
|
|
context 'sync duplicate tests' do
|
|
|
|
|
|
|
|
it 'check duplication of entries' do
|
|
|
|
Translation.where(locale: 'de-de').destroy_all
|
|
|
|
Translation.sync('de-de')
|
|
|
|
translation_count = Translation.where(locale: 'de-de').count
|
|
|
|
Translation.sync('de-de')
|
|
|
|
expect(
|
|
|
|
Translation.where(locale: 'de-de').count
|
|
|
|
).to be translation_count
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-12-06 10:46:26 +00:00
|
|
|
end
|