diff --git a/db/migrate/20140120155706_add_lounge_category.rb b/db/migrate/20140120155706_add_lounge_category.rb index 140d0856ed9beb..5f7c5aa5167209 100644 --- a/db/migrate/20140120155706_add_lounge_category.rb +++ b/db/migrate/20140120155706_add_lounge_category.rb @@ -1,11 +1,13 @@ class AddLoungeCategory < ActiveRecord::Migration def up - unless Rails.env.test? + return if Rails.env.test? + + I18n.backend.overrides_disabled do result = Category.exec_sql "SELECT 1 FROM site_settings where name = 'lounge_category_id'" if result.count == 0 - description = I18n.t('vip_category_description') + description = I18n.t('vip_category_description', skip_overrides: true) - default_name = I18n.t('vip_category_name') + default_name = I18n.t('vip_category_name', skip_overrides: true) name = if Category.exec_sql("SELECT 1 FROM categories where name = '#{default_name}'").count == 0 default_name else diff --git a/db/migrate/20140122043508_add_meta_category.rb b/db/migrate/20140122043508_add_meta_category.rb index ae63c48edb1e44..782519663bb78c 100644 --- a/db/migrate/20140122043508_add_meta_category.rb +++ b/db/migrate/20140122043508_add_meta_category.rb @@ -1,6 +1,8 @@ class AddMetaCategory < ActiveRecord::Migration def up - unless Rails.env.test? + return if Rails.env.test? + + I18n.backend.overrides_disabled do result = Category.exec_sql "SELECT 1 FROM site_settings where name = 'meta_category_id'" if result.count == 0 description = I18n.t('meta_category_description') diff --git a/db/migrate/20140227201005_add_staff_category.rb b/db/migrate/20140227201005_add_staff_category.rb index 74d96405b75689..3d3cccdec64c9f 100644 --- a/db/migrate/20140227201005_add_staff_category.rb +++ b/db/migrate/20140227201005_add_staff_category.rb @@ -1,6 +1,8 @@ class AddStaffCategory < ActiveRecord::Migration def up - unless Rails.env.test? + return if Rails.env.test? + + I18n.backend.overrides_disabled do result = Category.exec_sql "SELECT 1 FROM site_settings where name = 'staff_category_id'" if result.count == 0 description = I18n.t('staff_category_description') diff --git a/db/migrate/20141014191645_fix_tos_name.rb b/db/migrate/20141014191645_fix_tos_name.rb index ff8ac0aa18e3f5..1c2f3d5ab646f9 100644 --- a/db/migrate/20141014191645_fix_tos_name.rb +++ b/db/migrate/20141014191645_fix_tos_name.rb @@ -1,6 +1,8 @@ class FixTosName < ActiveRecord::Migration def up - execute ActiveRecord::Base.sql_fragment('UPDATE user_fields SET name = ? WHERE name = ?', I18n.t('terms_of_service.title'), I18n.t("terms_of_service.signup_form_message")) + I18n.backend.overrides_disabled do + execute ActiveRecord::Base.sql_fragment('UPDATE user_fields SET name = ? WHERE name = ?', I18n.t('terms_of_service.title'), I18n.t("terms_of_service.signup_form_message")) + end end end diff --git a/db/migrate/20150728210202_migrate_old_moderator_posts.rb b/db/migrate/20150728210202_migrate_old_moderator_posts.rb index eeeaf7f9000541..47cc9654320eee 100644 --- a/db/migrate/20150728210202_migrate_old_moderator_posts.rb +++ b/db/migrate/20150728210202_migrate_old_moderator_posts.rb @@ -1,9 +1,11 @@ class MigrateOldModeratorPosts < ActiveRecord::Migration def migrate_key(action_code) - text = I18n.t("topic_statuses.#{action_code.gsub('.', '_')}") + I18n.backend.overrides_disabled do + text = I18n.t("topic_statuses.#{action_code.gsub('.', '_')}") - execute "UPDATE posts SET action_code = '#{action_code}', raw = '', cooked = '', post_type = 3 where post_type = 2 AND raw = #{ActiveRecord::Base.connection.quote(text)}" + execute "UPDATE posts SET action_code = '#{action_code}', raw = '', cooked = '', post_type = 3 where post_type = 2 AND raw = #{ActiveRecord::Base.connection.quote(text)}" + end end def up diff --git a/db/migrate/20150729150523_migrate_auto_close_posts.rb b/db/migrate/20150729150523_migrate_auto_close_posts.rb index 26c2db812c1f87..798338b2934dd0 100644 --- a/db/migrate/20150729150523_migrate_auto_close_posts.rb +++ b/db/migrate/20150729150523_migrate_auto_close_posts.rb @@ -1,16 +1,18 @@ class MigrateAutoClosePosts < ActiveRecord::Migration def up - strings = [] - %w(days hours lastpost_days lastpost_hours lastpost_minutes).map do |k| - strings << I18n.t("topic_statuses.autoclosed_enabled_#{k}.one") - strings << I18n.t("topic_statuses.autoclosed_enabled_#{k}.other").sub("%{count}", "\\d+") - end + I18n.backend.overrides_disabled do + strings = [] + %w(days hours lastpost_days lastpost_hours lastpost_minutes).map do |k| + strings << I18n.t("topic_statuses.autoclosed_enabled_#{k}.one") + strings << I18n.t("topic_statuses.autoclosed_enabled_#{k}.other").sub("%{count}", "\\d+") + end - sql = "UPDATE posts SET action_code = 'autoclosed.enabled', post_type = 3 " - sql << "WHERE post_type = 2 AND (" - sql << strings.map {|s| "raw ~* #{ActiveRecord::Base.connection.quote(s)}" }.join(' OR ') - sql << ")" + sql = "UPDATE posts SET action_code = 'autoclosed.enabled', post_type = 3 " + sql << "WHERE post_type = 2 AND (" + sql << strings.map {|s| "raw ~* #{ActiveRecord::Base.connection.quote(s)}" }.join(' OR ') + sql << ")" - execute sql + execute sql + end end end diff --git a/lib/i18n/backend/discourse_i18n.rb b/lib/i18n/backend/discourse_i18n.rb index e76c1c5eddaadc..c901601bcc7e91 100644 --- a/lib/i18n/backend/discourse_i18n.rb +++ b/lib/i18n/backend/discourse_i18n.rb @@ -5,6 +5,10 @@ module Backend class DiscourseI18n < I18n::Backend::Simple include I18n::Backend::Pluralization + def initialize + @overrides_enabled = true + end + def available_locales # in case you are wondering this is: # Dir.glob( File.join(Rails.root, 'config', 'locales', 'client.*.yml') ) @@ -30,6 +34,15 @@ def overrides_for(locale) @overrides[locale] end + # In some environments such as migrations we don't want to use overrides. + # Use this to disable them over a block of ruby code + def overrides_disabled + @overrides_enabled = false + yield + ensure + @overrides_enabled = true + end + # force explicit loading def load_translations(*filenames) unless filenames.empty? @@ -42,7 +55,7 @@ def fallbacks(locale) end def translate(locale, key, options = {}) - overrides_for(locale)[key] || super(locale, key, options) + (@overrides_enabled && overrides_for(locale)[key]) || super(locale, key, options) end def exists?(locale, key)