diff --git a/app/models/locomotive/extensions/site/locales.rb b/app/models/locomotive/extensions/site/locales.rb index df1a5cf6..206132a8 100644 --- a/app/models/locomotive/extensions/site/locales.rb +++ b/app/models/locomotive/extensions/site/locales.rb @@ -10,8 +10,12 @@ module Locomotive ## fields ## field :locales, :type => 'RawArray', :default => [] + ## validations ## + validate :can_not_remove_default_locale + ## callbacks ## - after_validation :add_default_locale + after_validation :add_default_locale + before_update :verify_localized_default_pages_integrity end @@ -48,6 +52,10 @@ module Locomotive self.locales.first || Locomotive.config.site_locales.first end + def default_locale_was + self.locales_was.first || Locomotive.config.site_locales.first + end + def locale_fallbacks(locale) [locale.to_s] + (locales - [locale.to_s]) end @@ -58,6 +66,29 @@ module Locomotive self.locales = [Locomotive.config.site_locales.first] if self.locales.blank? end + def can_not_remove_default_locale + if self.persisted? && !self.locales.include?(self.default_locale_was) + self.errors.add :locales, I18n.t(:default_locale_removed, :scope => [:errors, :messages, :site]) + end + end + + def verify_localized_default_pages_integrity + if self.persisted? && self.locales_changed? + self.pages.where(:"slug.#{self.default_locale_was}".in => %w(index 404), :depth => 0).each do |page| + modifications = { 'title' => {}, 'slug' => {} } + + self.locales.each do |locale| + slug = page.attributes['slug'][self.default_locale_was] + + modifications['slug'][locale] = slug + modifications['title'][locale] = page.attributes['title'][locale] || ::I18n.t("attributes.defaults.pages.#{slug}.title", :locale => locale) + end + + page.collection.update({ :_id => page._id }, { '$set' => modifications }) + end + end + end + end end diff --git a/config/locales/default.en.yml b/config/locales/default.en.yml index 016cadf0..18ecdaa1 100644 --- a/config/locales/default.en.yml +++ b/config/locales/default.en.yml @@ -12,6 +12,8 @@ en: extname_changed: "New file does not have the original extension" array_too_short: "is too small (minimum element number is %{count})" invalid_theme_file: "can't be blank or isn't a zip file" + site: + default_locale_removed: The previous default locale can not be removed right away. page: liquid_syntax: "Liquid Syntax error ('%{error}' on '%{fullpath}')" liquid_extend: "The page '%{fullpath}' extends a template which does not exist" diff --git a/config/locales/default.fr.yml b/config/locales/default.fr.yml index 45d043ae..d3a9b332 100644 --- a/config/locales/default.fr.yml +++ b/config/locales/default.fr.yml @@ -33,6 +33,8 @@ fr: extname_changed: "Nouveau fichier n'a pas l'extension original" array_too_short: "est trop petit (le nombre minimum d'éléments est %{count})" security: "présente un problème de sécurité" + site: + default_locale_removed: La langue par défaut ne peut pas être supprimée de cette façon. page: liquid_syntax: "Erreur de syntaxe dans les sections de page, veuillez vérifier la syntaxe ('%{error}'/'%{fullpath}')" liquid_extend: "La page '%{fullpath}' étend le contenu d'une page qui n'existe pas" diff --git a/spec/models/locomotive/site_spec.rb b/spec/models/locomotive/site_spec.rb index 3d8ce016..a3c9722c 100644 --- a/spec/models/locomotive/site_spec.rb +++ b/spec/models/locomotive/site_spec.rb @@ -1,3 +1,5 @@ +# coding: utf-8 + require 'spec_helper' describe Locomotive::Site do @@ -114,6 +116,47 @@ describe Locomotive::Site do @site.pages.map(&:fullpath).sort.should == %w{404 index} end + it 'translates the index/404 pages if a new locale is added' do + @site.update_attributes :locales => %w(en fr) + + @site.errors.should be_empty + + ::Mongoid::Fields::I18n.with_locale('fr') do + @site.pages.root.first.tap do |page| + page.title.should == "Page d'accueil" + page.slug.should == 'index' + end + + @site.pages.not_found.first.tap do |page| + page.title.should == 'Page non trouvée' + page.slug.should == '404' + end + end + end + + it 'translates the index/404 pages if the default locale changes' do + @site.update_attributes :locales => %w(fr en) + + @site.errors.should be_empty + + ::Mongoid::Fields::I18n.with_locale('fr') do + @site.pages.root.first.tap do |page| + page.title.should == "Page d'accueil" + page.slug.should == 'index' + end + + @site.pages.not_found.first.tap do |page| + page.title.should == 'Page non trouvée' + page.slug.should == '404' + end + end + end + + it 'does not allow to remove the default locale' do + @site.update_attributes :locales => %w(fr) + @site.errors[:locales].should == ['The previous default locale can not be removed right away.'] + end + end describe 'deleting in cascade' do