rename the i18n site module into locales in order to avoid conflicts with the original i18n + rendering a page according to a locale specified in the url seems to work (tests are following)

This commit is contained in:
did 2012-01-26 02:33:39 +01:00
parent 11383f9c05
commit c0b8587eb2
22 changed files with 275 additions and 147 deletions

View File

@ -78,12 +78,14 @@ class Locomotive.Views.InlinEditor.ApplicationView extends Backbone.View
_jQuery('a').each ->
link = _jQuery(this)
url = link.attr('href')
if url? && url != '#' && /^(www|http)/.exec(url) == null && /(\/_edit)$/.exec(url) == null
if url? && url.indexOf('#') != 0 && /^(www|http)/.exec(url) == null && /(\/_edit)$/.exec(url) == null
url = '/index' if url == '/'
if url.indexOf('?') > 0
link.attr('href', url.replace('?', '/_edit?'))
else
link.attr('href', "#{url}/_edit")
unless url.indexOf('_edit') > 0
if url.indexOf('?') > 0
link.attr('href', url.replace('?', '/_edit?'))
else
link.attr('href', "#{url}/_edit")
link.bind 'click', ->
toolbar_view.show_status 'loading'

View File

@ -113,7 +113,7 @@ class Locomotive.Views.InlinEditor.ToolbarView extends Backbone.View
picker.toggle()
window.addParameterToURL 'content_locale', selected[1], _window.document
_window.location.href = '/' + @model.get('localized_fullpaths')[selected[1]] + '/_edit'
get_locale_attributes: (context) ->
[context.find('img').attr('src'), context.find('span.text').html()]

View File

@ -33,11 +33,14 @@ module Locomotive
def set_ui_locale
::I18n.locale = current_locomotive_account.locale rescue Locomotive.config.default_locale
::Mongoid::Fields::I18n.locale = params[:locale] || current_site.default_locale
end
def set_locale
self.set_current_content_locale
::Mongoid::Fields::I18n.locale = params[:locale] || current_site.default_locale
::I18n.locale = ::Mongoid::Fields::I18n.locale
self.setup_i18n_fallbacks
end
end

View File

@ -18,7 +18,7 @@ module Locomotive
end
def locale_to_html(locale)
text = I18n.t("locomotive.my_account.edit.#{locale}")
text = I18n.t("locomotive.locales.#{locale}")
builder.radio_button(:locale, locale, :id => choice_input_dom_id(locale)) +
template.content_tag(:label,

View File

@ -37,7 +37,7 @@ module Locomotive
end
def choice_label(choice)
text = I18n.t("locomotive.my_account.edit.#{choice}")
text = I18n.t("locomotive.locales.#{choice}")
template.image_tag("locomotive/icons/flags/#{choice}.png", :alt => text) << text
end

View File

@ -1,57 +0,0 @@
module Locomotive
module Extensions
module Site
module I18n
extend ActiveSupport::Concern
included do
## fields ##
field :locales, :type => 'RawArray', :default => []
## callbacks ##
# after_validation :add_missing_locales_for_all_pages
end
module InstanceMethods
def locales=(array)
array = [] if array.blank?; super(array)
end
def default_locale
self.locales.first || Locomotive.config.site_locales.first
end
def locale_fallbacks(locale)
[locale.to_s] + (locales - [locale.to_s])
end
# protected
#
# def add_missing_locales_for_all_pages
# if self.locales_changed?
# list = self.pages.to_a
#
# while !list.empty? do
# page = list.pop
# begin
# page.send(:set_slug_and_fullpath_for_all_locales, self.locales)
#
# page.save
#
# rescue TypeError => e
# list.insert(0, page)
# end
# end
# end
# end
end
end
end
end
end

View File

@ -0,0 +1,86 @@
module Locomotive
module Extensions
module Site
module Locales
extend ActiveSupport::Concern
included do
## fields ##
field :locales, :type => 'RawArray', :default => []
## callbacks ##
after_validation :add_default_locale
# after_validation :add_missing_locales_for_all_pages
end
module InstanceMethods
# Returns the fullpath of a page in the context of the current locale (I18n.locale)
# or the one passed in parameter. It also depends on the default site locale.
#
# Ex:
# For a site with its default site locale to 'en'
# # context 1: i18n.locale is 'en'
# contact_us.fullpath <= 'contact_us'
#
# # context 2: i18n.locale is 'fr'
# contact_us.fullpath <= 'fr/nous_contacter'
#
# @params [ Page ] page The page we want the localized fullpath
# @params [ String ] locale The optional locale in place of the current one
#
# @returns [ String ] The localized fullpath according to the current locale
#
def localized_page_fullpath(page, locale = nil)
locale = (locale || I18n.locale).to_s
fullpath = page.fullpath_translations[locale]
locale == self.default_locale ? fullpath : File.join(locale, fullpath)
end
def locales=(array)
array = [] if array.blank?; super(array)
end
def default_locale
self.locales.first || Locomotive.config.site_locales.first
end
def locale_fallbacks(locale)
[locale.to_s] + (locales - [locale.to_s])
end
protected
def add_default_locale
self.locales = [Locomotive.config.site_locales.first] if self.locales.blank?
end
#
# def add_missing_locales_for_all_pages
# if self.locales_changed?
# list = self.pages.to_a
#
# while !list.empty? do
# page = list.pop
# begin
# page.send(:set_slug_and_fullpath_for_all_locales, self.locales)
#
# page.save
#
# rescue TypeError => e
# list.insert(0, page)
# end
# end
# end
# end
end
end
end
end
end

View File

@ -64,35 +64,6 @@ module Locomotive
self.index? || self.not_found?
end
# def localized_fullpath(locale)
# ::Mongoid::Fields::I18n.with_locale(locale) do
# self.fullpath
# # locale != self.site.default_locale ? File.join(locale, self.fullpath) : self.fullpath
# end
# end
# def fullpath(locale = nil)
# locale ||= ::Mongoid::Fields::I18n.locale
#
# localized_fullpath = self.attributes['fullpath'][locale]
#
# return localized_fullpath if localized_fullpath.present? && !force
#
# if self.index? || self.not_found?
# self.slug
# else
# slugs = self.self_and_ancestors.sort_by(&:depth).map(&:slug)
# slugs.shift unless slugs.size == 1
# File.join slugs.compact
# end
# end
# def fullpath_with_locale(locale)
# url, locale = self.fullpath(true), locale.to_s
#
# locale != self.site.default_locale ? File.join(locale, url) : url
# end
def with_cache?
self.cache_strategy != 'none'
end
@ -131,7 +102,7 @@ module Locomotive
end
def build_fullpath
if self.index? || self.not_found? || self.templatized?
if self.index? || self.not_found?
self.fullpath = self.slug
else
slugs = self.self_and_ancestors.sort_by(&:depth).map(&:slug)

View File

@ -7,7 +7,7 @@ module Locomotive
extend Extensions::Site::SubdomainDomains
extend Extensions::Site::FirstInstallation
include Extensions::Shared::Seo
include Extensions::Site::I18n
include Extensions::Site::Locales
## fields ##
field :name

View File

@ -12,7 +12,17 @@ module Locomotive
end
def included_methods
super + %w(title slug fullpath raw_template published listed templatized redirect redirect_url cache_strategy template_changed editable_elements edit_url)
super + %w(title slug fullpath raw_template published listed templatized redirect redirect_url cache_strategy template_changed editable_elements edit_url localized_fullpaths)
end
def localized_fullpaths
site = self.source.site
{}.tap do |hash|
site.locales.each do |locale|
hash[locale] = site.localized_page_fullpath(self.source, locale)
end
end
end
def as_json_for_html_view

View File

@ -9,6 +9,17 @@ en:
delete: "Delete"
close: "Close"
locales:
en: English
de: German
fr: French
pt-BR: "Brazilian Portuguese"
it: Italian
nl: Dutch
"no": Norwegian
es: Spanish
ru: Russian
messages:
confirm: Are you sure ?
@ -172,15 +183,6 @@ en:
edit:
help: "Your name can be updated by clicking it. To apply your changes, click on the \"Save\" button."
new_site: new site
en: English
de: German
fr: French
pt-BR: "Brazilian Portuguese"
it: Italian
nl: Dutch
"no": Norwegian
es: Spanish
ru: Russian
ask_for_name: "Please type your new name"
theme_assets:

View File

@ -10,6 +10,17 @@ fr:
notice: "La page que vous avez demandée n'existe pas."
link: "&rarr; Retour application"
locales:
en: Anglais
de: Allemand
fr: Français
pt-BR: "Portugais"
it: "Italien"
nl: "Hollandais"
"no": "Norvégien"
es: Espagnol
ru: Russe
buttons:
login: Se connecter
send_password: Envoyer
@ -163,16 +174,6 @@ fr:
edit:
help: "Votre nom est modifiable en cliquant dessus. Pour appliquer votre modification, cliquez après sur le bouton \"Modifier\""
new_site: nouveau site
en: en Anglais
de: en Allemand
fr: en Français
pt-BR: "en Portugais"
it: "en Italien"
nl: "en Hollandais"
"no": "en Norvégien"
es: en Espagnol
ru: en Russe
ask_for_name: "Veuillez entrer le nouveau nom"
theme_assets:

View File

@ -89,14 +89,20 @@ Rails.application.routes.draw do
resources :locomotive_entry_submissions, :controller => 'locomotive/public/content_entries', :path => 'entry_submissions/:slug'
# magic urls
match '/_admin' => 'locomotive/public/pages#show_toolbar'
match '*path/_admin' => 'locomotive/public/pages#show_toolbar'
match '/_admin' => 'locomotive/public/pages#show_toolbar'
match ':locale/_admin' => 'locomotive/public/pages#show_toolbar', :locale => /(#{Locomotive.config.site_locales.join('|')})/
match ':locale/*path/_admin' => 'locomotive/public/pages#show_toolbar', :locale => /(#{Locomotive.config.site_locales.join('|')})/
match '*path/_admin' => 'locomotive/public/pages#show_toolbar'
match '/_edit' => 'locomotive/public/pages#edit'
match '*path/_edit' => 'locomotive/public/pages#edit'
match '/_edit' => 'locomotive/public/pages#edit'
match ':locale/_edit' => 'locomotive/public/pages#edit', :page_path => 'index', :locale => /(#{Locomotive.config.site_locales.join('|')})/
match ':locale/*path/_edit' => 'locomotive/public/pages#edit', :locale => /(#{Locomotive.config.site_locales.join('|')})/
match '*path/_edit' => 'locomotive/public/pages#edit'
match '/' => 'locomotive/public/pages#show'
match '*path' => 'locomotive/public/pages#show'
match '/' => 'locomotive/public/pages#show'
match ':locale' => 'locomotive/public/pages#show', :page_path => 'index', :locale => /(#{Locomotive.config.site_locales.join('|')})/
match ':locale/*path' => 'locomotive/public/pages#show', :locale => /(#{Locomotive.config.site_locales.join('|')})/
match '*path' => 'locomotive/public/pages#show'
end

View File

@ -69,9 +69,11 @@ x edit my site
- i18n
x add locales a site responds to
x locale switcher
- back to default locale (if changed in settings)
- front
- pages
x constraint: one locale at least
x front
x pages
x inline-editor
- back to default locale (if changed in settings) and when creating new pages / entries
- inline editor
x rack
x iframe
@ -99,8 +101,8 @@ x deployment
x CRUD pages
x CRUD content types
x data ?
- script to migrate existing site
- i18n
x script to migrate existing site
x i18n
- refactoring
- remove the import / export scripts
- remove the cross domain authentication (use auth_token instead)

View File

@ -24,17 +24,22 @@ module Locomotive
end
::Mongoid::Fields::I18n.locale = session[:content_locale]
(current_site.locales || []).each do |locale|
::Mongoid::Fields::I18n.fallbacks_for(locale, current_site.locale_fallbacks(locale))
end
logger.debug "*** content locale = #{session[:content_locale]} / #{::Mongoid::Fields::I18n.locale}"
self.setup_i18n_fallbacks
# logger.debug "*** content locale = #{session[:content_locale]} / #{::Mongoid::Fields::I18n.locale}"
end
def set_back_office_locale
::I18n.locale = current_locomotive_account.locale rescue Locomotive.config.default_locale
end
def setup_i18n_fallbacks
(current_site.locales || []).each do |locale|
::Mongoid::Fields::I18n.fallbacks_for(locale, current_site.locale_fallbacks(locale))
end
end
end
end

View File

@ -14,4 +14,22 @@ module Kaminari
}
end
end
module PageScopeMethods
def to_liquid(options = {})
{
:collection => to_a,
:current_page => current_page,
:previous_page => first_page? ? nil : current_page - 1,
:total_entries => total_count,
:per_page => limit_value
}.tap do |hash|
# note: very important to avoid extra and useless mongodb requests
hash[:total_pages] = (hash[:total_entries].to_f / limit_value).ceil
hash[:next_page] = current_page >= hash[:total_pages] ? nil : current_page + 1
end
end
end
end

View File

@ -66,7 +66,7 @@ module Locomotive
protected
def paginate(options = {})
@collection.page(options[:page]).per(options[:per_page])
self.collection.page(options[:page]).per(options[:per_page])
end
def collection

View File

@ -98,8 +98,8 @@ module Locomotive
options = args_to_options(args)
previous_label = options[:previous_label] || I18n.t('pagination.previous')
next_label = options[:next_label] || I18n.t('pagination.next')
previous_label = options[:previous_label] || I18n.t('pagination.previous')
next_label = options[:next_label] || I18n.t('pagination.next')
previous_link = (if paginate['previous'].blank?
"<span class=\"disabled prev_page\">#{previous_label}</span>"

View File

@ -0,0 +1,75 @@
module Locomotive
module Liquid
module Tags
# Display the links to change the locale of the current page
#
# Usage:
#
# {% locale_switcher %} => <div id="locale-switcher"><a href="/features" class="current en">Features</a><a href="/fr/fonctionnalites" class="fr">Fonctionnalités</a></div>
#
# {% locale_switcher label: locale, sep: ' - ' }
#
# options:
# - label: iso (de, fr, en, ...etc), locale (Deutsch, Français, English, ...etc), title (page title)
# - sep: piece of html code seperating 2 locales
#
# notes:
# - "iso" is the default choice for label
# - " | " is the default seperating code
#
class LocaleSwitcher < ::Liquid::Tag
Syntax = /(#{::Liquid::Expression}+)?/
def initialize(tag_name, markup, tokens, context)
@options = { :label => 'iso', :sep => ' | ' }
if markup =~ Syntax
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
@options[:exclude] = Regexp.new(@options[:exclude]) if @options[:exclude]
else
raise ::Liquid::SyntaxError.new("Syntax Error in 'locale_switcher' - Valid syntax: locale_switcher <options>")
end
super
end
def render(context)
@site, @page = context.registers[:site], context.registers[:page]
output = %(<div id="locale-switcher">)
output += @site.locales.collect do |locale|
::Mongoid::Fields::I18n.with_locale(locale) do
fullpath = @site.localized_page_fullpath(@page, locale)
if @page.templatized?
fullpath.gsub!('content_type_template', context['entry']._permalink)
end
%(<a href="/#{fullpath}" class="#{locale} #{'current' if locale == context['current_locale']}">#{link_label(locale)}</a>)
end
end.join(@options[:sep])
output += %(</div>)
end
private
def link_label(locale)
case @options[:label]
when :iso then locale
when :locale then I18n.t("locomotive.locales.#{locale}", :locale => locale)
when :title then @page.title
else
locale
end
end
end
::Liquid::Template.register_tag('locale_switcher', LocaleSwitcher)
end
end
end

View File

@ -56,14 +56,14 @@ module Locomotive
# Determines root node for the list
def fetch_entries(context)
@current_page = context.registers[:page]
@site, @page = context.registers[:site], context.registers[:page]
children = (case @source
when 'site' then context.registers[:site].pages.root.minimal_attributes.first # start from home page
when 'parent' then @current_page.parent || @current_page
when 'page' then @current_page
when 'site' then @site.pages.root.minimal_attributes.first # start from home page
when 'parent' then @page.parent || @page
when 'page' then @page
else
context.registers[:site].pages.fullpath(@source).minimal_attributes.first
@site.pages.fullpath(@source).minimal_attributes.first
end).children_with_minimal_attributes.to_a
children.delete_if { |p| !include_page?(p) }
@ -71,13 +71,13 @@ module Locomotive
# Returns a list element, a link to the page and its children
def render_entry_link(page, css, depth)
selected = @current_page.fullpath =~ /^#{page.fullpath}/ ? ' on' : ''
selected = @page.fullpath =~ /^#{page.fullpath}/ ? ' on' : ''
icon = @options[:icon] ? '<span></span>' : ''
label = %{#{icon if @options[:icon] != 'after' }#{page.title}#{icon if @options[:icon] == 'after' }}
output = %{<li id="#{page.slug.dasherize}-link" class="link#{selected} #{css}">}
output << %{<a href="/#{page.fullpath}">#{label}</a>}
output << %{<a href="/#{@site.localized_page_fullpath(page)}">#{label}</a>}
output << render_entry_children(page, depth.succ) if (depth.succ <= @options[:depth].to_i)
output << %{</li>}

View File

@ -41,7 +41,8 @@ module Locomotive
else
pagination = collection.send(:paginate, {
:page => context['current_page'],
:per_page => @per_page }).to_liquid.stringify_keys
:per_page => @per_page
}).to_liquid.stringify_keys
end
page_count, current_page = pagination['total_pages'], pagination['current_page']

View File

@ -28,7 +28,7 @@ module Locomotive
end
def locomotive_page
path = (params[:path] || request.fullpath).clone # TODO: params[:path] is more consistent
path = (params[:path] || params[:page_path] || request.fullpath).clone # TODO: params[:path] is more consistent
path = path.split('?').first # take everything before the query string or the lookup fails
path.gsub!(/\.[a-zA-Z][a-zA-Z0-9]{2,}$/, '') # remove the page extension
path.gsub!(/^\//, '') # remove the leading slash
@ -68,7 +68,10 @@ module Locomotive
'path' => request.path,
'url' => request.url,
'now' => Time.now.utc,
'today' => Date.today
'today' => Date.today,
'locale' => I18n.locale,
'default_locale' => current_site.default_locale.to_s,
'locales' => current_site.locales
}
assigns.merge!(Locomotive.config.context_assign_extensions)
@ -85,10 +88,10 @@ module Locomotive
:site => current_site,
:page => @page,
:inline_editor => self.editing_page?,
:current_locomotive_account => current_locomotive_account
:current_locomotive_account => current_locomotive_account
}
::Liquid::Context.new({}, assigns, registers)
::Liquid::Context.new({}, assigns, registers, true)
end
def prepare_and_set_response(output)