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:
parent
11383f9c05
commit
c0b8587eb2
@ -78,8 +78,10 @@ class Locomotive.Views.InlinEditor.ApplicationView extends Backbone.View
|
|||||||
_jQuery('a').each ->
|
_jQuery('a').each ->
|
||||||
link = _jQuery(this)
|
link = _jQuery(this)
|
||||||
url = link.attr('href')
|
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 == '/'
|
url = '/index' if url == '/'
|
||||||
|
|
||||||
|
unless url.indexOf('_edit') > 0
|
||||||
if url.indexOf('?') > 0
|
if url.indexOf('?') > 0
|
||||||
link.attr('href', url.replace('?', '/_edit?'))
|
link.attr('href', url.replace('?', '/_edit?'))
|
||||||
else
|
else
|
||||||
|
@ -113,7 +113,7 @@ class Locomotive.Views.InlinEditor.ToolbarView extends Backbone.View
|
|||||||
|
|
||||||
picker.toggle()
|
picker.toggle()
|
||||||
|
|
||||||
window.addParameterToURL 'content_locale', selected[1], _window.document
|
_window.location.href = '/' + @model.get('localized_fullpaths')[selected[1]] + '/_edit'
|
||||||
|
|
||||||
get_locale_attributes: (context) ->
|
get_locale_attributes: (context) ->
|
||||||
[context.find('img').attr('src'), context.find('span.text').html()]
|
[context.find('img').attr('src'), context.find('span.text').html()]
|
||||||
|
@ -33,11 +33,14 @@ module Locomotive
|
|||||||
|
|
||||||
def set_ui_locale
|
def set_ui_locale
|
||||||
::I18n.locale = current_locomotive_account.locale rescue Locomotive.config.default_locale
|
::I18n.locale = current_locomotive_account.locale rescue Locomotive.config.default_locale
|
||||||
|
::Mongoid::Fields::I18n.locale = params[:locale] || current_site.default_locale
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_locale
|
def set_locale
|
||||||
self.set_current_content_locale
|
::Mongoid::Fields::I18n.locale = params[:locale] || current_site.default_locale
|
||||||
::I18n.locale = ::Mongoid::Fields::I18n.locale
|
::I18n.locale = ::Mongoid::Fields::I18n.locale
|
||||||
|
|
||||||
|
self.setup_i18n_fallbacks
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -18,7 +18,7 @@ module Locomotive
|
|||||||
end
|
end
|
||||||
|
|
||||||
def locale_to_html(locale)
|
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)) +
|
builder.radio_button(:locale, locale, :id => choice_input_dom_id(locale)) +
|
||||||
template.content_tag(:label,
|
template.content_tag(:label,
|
||||||
|
@ -37,7 +37,7 @@ module Locomotive
|
|||||||
end
|
end
|
||||||
|
|
||||||
def choice_label(choice)
|
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
|
template.image_tag("locomotive/icons/flags/#{choice}.png", :alt => text) << text
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -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
|
|
86
app/models/locomotive/extensions/site/locales.rb
Normal file
86
app/models/locomotive/extensions/site/locales.rb
Normal 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
|
@ -64,35 +64,6 @@ module Locomotive
|
|||||||
self.index? || self.not_found?
|
self.index? || self.not_found?
|
||||||
end
|
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?
|
def with_cache?
|
||||||
self.cache_strategy != 'none'
|
self.cache_strategy != 'none'
|
||||||
end
|
end
|
||||||
@ -131,7 +102,7 @@ module Locomotive
|
|||||||
end
|
end
|
||||||
|
|
||||||
def build_fullpath
|
def build_fullpath
|
||||||
if self.index? || self.not_found? || self.templatized?
|
if self.index? || self.not_found?
|
||||||
self.fullpath = self.slug
|
self.fullpath = self.slug
|
||||||
else
|
else
|
||||||
slugs = self.self_and_ancestors.sort_by(&:depth).map(&:slug)
|
slugs = self.self_and_ancestors.sort_by(&:depth).map(&:slug)
|
||||||
|
@ -7,7 +7,7 @@ module Locomotive
|
|||||||
extend Extensions::Site::SubdomainDomains
|
extend Extensions::Site::SubdomainDomains
|
||||||
extend Extensions::Site::FirstInstallation
|
extend Extensions::Site::FirstInstallation
|
||||||
include Extensions::Shared::Seo
|
include Extensions::Shared::Seo
|
||||||
include Extensions::Site::I18n
|
include Extensions::Site::Locales
|
||||||
|
|
||||||
## fields ##
|
## fields ##
|
||||||
field :name
|
field :name
|
||||||
|
@ -12,7 +12,17 @@ module Locomotive
|
|||||||
end
|
end
|
||||||
|
|
||||||
def included_methods
|
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
|
end
|
||||||
|
|
||||||
def as_json_for_html_view
|
def as_json_for_html_view
|
||||||
|
@ -9,6 +9,17 @@ en:
|
|||||||
delete: "Delete"
|
delete: "Delete"
|
||||||
close: "Close"
|
close: "Close"
|
||||||
|
|
||||||
|
locales:
|
||||||
|
en: English
|
||||||
|
de: German
|
||||||
|
fr: French
|
||||||
|
pt-BR: "Brazilian Portuguese"
|
||||||
|
it: Italian
|
||||||
|
nl: Dutch
|
||||||
|
"no": Norwegian
|
||||||
|
es: Spanish
|
||||||
|
ru: Russian
|
||||||
|
|
||||||
messages:
|
messages:
|
||||||
confirm: Are you sure ?
|
confirm: Are you sure ?
|
||||||
|
|
||||||
@ -172,15 +183,6 @@ en:
|
|||||||
edit:
|
edit:
|
||||||
help: "Your name can be updated by clicking it. To apply your changes, click on the \"Save\" button."
|
help: "Your name can be updated by clicking it. To apply your changes, click on the \"Save\" button."
|
||||||
new_site: new site
|
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"
|
ask_for_name: "Please type your new name"
|
||||||
|
|
||||||
theme_assets:
|
theme_assets:
|
||||||
|
@ -10,6 +10,17 @@ fr:
|
|||||||
notice: "La page que vous avez demandée n'existe pas."
|
notice: "La page que vous avez demandée n'existe pas."
|
||||||
link: "→ Retour application"
|
link: "→ 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:
|
buttons:
|
||||||
login: Se connecter
|
login: Se connecter
|
||||||
send_password: Envoyer
|
send_password: Envoyer
|
||||||
@ -163,16 +174,6 @@ fr:
|
|||||||
edit:
|
edit:
|
||||||
help: "Votre nom est modifiable en cliquant dessus. Pour appliquer votre modification, cliquez après sur le bouton \"Modifier\""
|
help: "Votre nom est modifiable en cliquant dessus. Pour appliquer votre modification, cliquez après sur le bouton \"Modifier\""
|
||||||
new_site: nouveau site
|
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"
|
ask_for_name: "Veuillez entrer le nouveau nom"
|
||||||
|
|
||||||
theme_assets:
|
theme_assets:
|
||||||
|
@ -90,12 +90,18 @@ Rails.application.routes.draw do
|
|||||||
|
|
||||||
# magic urls
|
# magic urls
|
||||||
match '/_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 '*path/_admin' => 'locomotive/public/pages#show_toolbar'
|
||||||
|
|
||||||
match '/_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 '*path/_edit' => 'locomotive/public/pages#edit'
|
||||||
|
|
||||||
match '/' => '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'
|
match '*path' => 'locomotive/public/pages#show'
|
||||||
|
|
||||||
|
|
||||||
|
12
doc/TODO
12
doc/TODO
@ -69,9 +69,11 @@ x edit my site
|
|||||||
- i18n
|
- i18n
|
||||||
x add locales a site responds to
|
x add locales a site responds to
|
||||||
x locale switcher
|
x locale switcher
|
||||||
- back to default locale (if changed in settings)
|
x constraint: one locale at least
|
||||||
- front
|
x front
|
||||||
- pages
|
x pages
|
||||||
|
x inline-editor
|
||||||
|
- back to default locale (if changed in settings) and when creating new pages / entries
|
||||||
- inline editor
|
- inline editor
|
||||||
x rack
|
x rack
|
||||||
x iframe
|
x iframe
|
||||||
@ -99,8 +101,8 @@ x deployment
|
|||||||
x CRUD pages
|
x CRUD pages
|
||||||
x CRUD content types
|
x CRUD content types
|
||||||
x data ?
|
x data ?
|
||||||
- script to migrate existing site
|
x script to migrate existing site
|
||||||
- i18n
|
x i18n
|
||||||
- refactoring
|
- refactoring
|
||||||
- remove the import / export scripts
|
- remove the import / export scripts
|
||||||
- remove the cross domain authentication (use auth_token instead)
|
- remove the cross domain authentication (use auth_token instead)
|
||||||
|
@ -24,17 +24,22 @@ module Locomotive
|
|||||||
end
|
end
|
||||||
|
|
||||||
::Mongoid::Fields::I18n.locale = session[:content_locale]
|
::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
|
end
|
||||||
|
|
||||||
def set_back_office_locale
|
def set_back_office_locale
|
||||||
::I18n.locale = current_locomotive_account.locale rescue Locomotive.config.default_locale
|
::I18n.locale = current_locomotive_account.locale rescue Locomotive.config.default_locale
|
||||||
end
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -14,4 +14,22 @@ module Kaminari
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
@ -66,7 +66,7 @@ module Locomotive
|
|||||||
protected
|
protected
|
||||||
|
|
||||||
def paginate(options = {})
|
def paginate(options = {})
|
||||||
@collection.page(options[:page]).per(options[:per_page])
|
self.collection.page(options[:page]).per(options[:per_page])
|
||||||
end
|
end
|
||||||
|
|
||||||
def collection
|
def collection
|
||||||
|
75
lib/locomotive/liquid/tags/locale_switcher.rb
Normal file
75
lib/locomotive/liquid/tags/locale_switcher.rb
Normal 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
|
@ -56,14 +56,14 @@ module Locomotive
|
|||||||
|
|
||||||
# Determines root node for the list
|
# Determines root node for the list
|
||||||
def fetch_entries(context)
|
def fetch_entries(context)
|
||||||
@current_page = context.registers[:page]
|
@site, @page = context.registers[:site], context.registers[:page]
|
||||||
|
|
||||||
children = (case @source
|
children = (case @source
|
||||||
when 'site' then context.registers[:site].pages.root.minimal_attributes.first # start from home page
|
when 'site' then @site.pages.root.minimal_attributes.first # start from home page
|
||||||
when 'parent' then @current_page.parent || @current_page
|
when 'parent' then @page.parent || @page
|
||||||
when 'page' then @current_page
|
when 'page' then @page
|
||||||
else
|
else
|
||||||
context.registers[:site].pages.fullpath(@source).minimal_attributes.first
|
@site.pages.fullpath(@source).minimal_attributes.first
|
||||||
end).children_with_minimal_attributes.to_a
|
end).children_with_minimal_attributes.to_a
|
||||||
|
|
||||||
children.delete_if { |p| !include_page?(p) }
|
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
|
# Returns a list element, a link to the page and its children
|
||||||
def render_entry_link(page, css, depth)
|
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>' : ''
|
icon = @options[:icon] ? '<span></span>' : ''
|
||||||
label = %{#{icon if @options[:icon] != 'after' }#{page.title}#{icon if @options[:icon] == 'after' }}
|
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 = %{<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 << render_entry_children(page, depth.succ) if (depth.succ <= @options[:depth].to_i)
|
||||||
output << %{</li>}
|
output << %{</li>}
|
||||||
|
|
||||||
|
@ -41,7 +41,8 @@ module Locomotive
|
|||||||
else
|
else
|
||||||
pagination = collection.send(:paginate, {
|
pagination = collection.send(:paginate, {
|
||||||
:page => context['current_page'],
|
:page => context['current_page'],
|
||||||
:per_page => @per_page }).to_liquid.stringify_keys
|
:per_page => @per_page
|
||||||
|
}).to_liquid.stringify_keys
|
||||||
end
|
end
|
||||||
page_count, current_page = pagination['total_pages'], pagination['current_page']
|
page_count, current_page = pagination['total_pages'], pagination['current_page']
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ module Locomotive
|
|||||||
end
|
end
|
||||||
|
|
||||||
def locomotive_page
|
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 = 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!(/\.[a-zA-Z][a-zA-Z0-9]{2,}$/, '') # remove the page extension
|
||||||
path.gsub!(/^\//, '') # remove the leading slash
|
path.gsub!(/^\//, '') # remove the leading slash
|
||||||
@ -68,7 +68,10 @@ module Locomotive
|
|||||||
'path' => request.path,
|
'path' => request.path,
|
||||||
'url' => request.url,
|
'url' => request.url,
|
||||||
'now' => Time.now.utc,
|
'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)
|
assigns.merge!(Locomotive.config.context_assign_extensions)
|
||||||
@ -88,7 +91,7 @@ module Locomotive
|
|||||||
:current_locomotive_account => current_locomotive_account
|
:current_locomotive_account => current_locomotive_account
|
||||||
}
|
}
|
||||||
|
|
||||||
::Liquid::Context.new({}, assigns, registers)
|
::Liquid::Context.new({}, assigns, registers, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def prepare_and_set_response(output)
|
def prepare_and_set_response(output)
|
||||||
|
Loading…
Reference in New Issue
Block a user