radius parser for multilingual - dev.rake

This commit is contained in:
chris 2011-03-02 17:28:33 +08:00 committed by ihower
parent fc98757772
commit 5151449bc9
9 changed files with 171 additions and 70 deletions

View File

@ -18,13 +18,14 @@ class Admin::PagesController < ApplicationController
def edit def edit
@page = Page.find(params[:id]) @page = Page.find(params[:id])
@page.content = parse_content(@page.content, {:locale => 'show'})
@i18n_variable = @page.i18n_variable @i18n_variable = @page.i18n_variable
session[:last_page] = get_go_back || admin_items_url session[:last_page] = get_go_back || admin_items_url
end end
def create def create
@page = Page.new(params[:page]) @page = Page.new(params[:page])
@page.content = parse_content(@page.content, {:locale => 'create'})
if @page.save if @page.save
flash[:notice] = t('admin.create_success_page') flash[:notice] = t('admin.create_success_page')
redirect_to admin_items_url( :parent_id => @page.parent_id ) redirect_to admin_items_url( :parent_id => @page.parent_id )
@ -35,7 +36,10 @@ class Admin::PagesController < ApplicationController
def update def update
@page = Page.find(params[:id]) @page = Page.find(params[:id])
parse_content(@page.content, {:locale => 'destroy'})
if @page.update_attributes(params[:page]) if @page.update_attributes(params[:page])
@page.content = parse_content(@page.content, {:locale => 'create'})
@page.save!
flash[:notice] = t('admin.update_success_page') flash[:notice] = t('admin.update_success_page')
redirect_to admin_items_url( :parent_id => @page.parent_id ) redirect_to admin_items_url( :parent_id => @page.parent_id )
else else

View File

@ -1,6 +1,8 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery protect_from_forgery
include Parser
helper :all helper :all
before_filter :set_locale, :set_site before_filter :set_locale, :set_site
@ -36,41 +38,10 @@ class ApplicationController < ActionController::Base
redirect_to root_url unless current_user.admin? redirect_to root_url unless current_user.admin?
end end
# Parse and render the pages with Radius # Render the page
def render_page def render_page
if @page if @page
@layout = @page.layout render :text => parse_page(@page)
@page_content = @page.content
@layout_content = (@page.layout)? @layout.content : "<r:content />"
context = Radius::Context.new do |c|
c.define_tag 'content' do |tag|
@page_content
end
c.define_tag 'snippet' do |tag|
snippet = Snippet.first(:conditions => {:name => tag.attr['name']})
if snippet
snippet.content
else
t('nothing')
end
end
c.define_tag 'language_bar' do
@site_in_use_locales.map{ |locale|
lang = I18nVariable.first(:conditions => {:key => locale})[locale]
if I18n.locale.to_s.eql?(locale)
lang
else
"<a href='?locale=#{locale}'>#{lang}</a>"
end
}.join(' | ')
end
end
parser = Radius::Parser.new(context, :tag_prefix => 'r')
render :text => parser.parse(@layout_content)
else else
render :text => '404 Not Found' render :text => '404 Not Found'
end end
@ -97,10 +68,10 @@ class ApplicationController < ActionController::Base
# Set the site variables # Set the site variables
def set_site def set_site
# set site if exist or create site # set site if exist or create site
site = Site.first || Site.create({:valid_locales => [], :in_use_locales => []}) @site = Site.first || Site.create({:valid_locales => [], :in_use_locales => []})
session[:site] = site.id session[:site] = @site.id
@site_in_use_locales = site.in_use_locales @site_in_use_locales = @site.in_use_locales
@site_valid_locales = site.valid_locales @site_valid_locales = @site.valid_locales
end end
end end

73
app/models/parser.rb Normal file
View File

@ -0,0 +1,73 @@
module Parser
def parser_context(page_content, attributes = {})
Radius::Context.new do |c|
c.define_tag 'content' do |tag|
page_content
end
c.define_tag 'snippet' do |tag|
snippet = Snippet.first(:conditions => {:name => tag.attr['name']})
if snippet
snippet.content
else
t('nothing')
end
end
c.define_tag 'language_bar' do
@site.in_use_locales.map{ |locale|
lang = I18nVariable.first(:conditions => {:key => locale})[locale]
if I18n.locale.to_s.eql?(locale)
lang
else
"<a href='?locale=#{locale}'>#{lang}</a>"
end
}.join(' | ')
end
c.define_tag 'locale' do |tag|
case attributes[:locale]
when 'create'
var = I18nVariable.new(:key => (tag.attr['name'] rescue nil), :document_class => 'Text')
@site.valid_locales.each do |locale|
var[locale] = tag.attr[locale] rescue nil
end
var.save!
res = ''
res << "<r:locale id='#{var.id}' "
res << "name='#{var.key}' " if var.key
@site.valid_locales.each do |locale|
res << "#{locale}='#{var[locale]}' "
end
res << '/>'
when 'show'
var = I18nVariable.find(tag.attr['id'])
res = ''
res << "<r:locale "
res << "name='#{var.key}' " if var.key
@site.valid_locales.each do |locale|
res << "#{locale}='#{var[locale]}' "
end
res << '/>'
when 'destroy'
var = I18nVariable.find(tag.attr['id'])
var.destroy
else
tag.attr[I18n.locale.to_s] rescue nil
end
end
end
end
def parse_content(page_content, attributes = {})
context = parser_context(page_content, attributes)
parser = Radius::Parser.new(context, :tag_prefix => 'r')
parser.parse(page_content)
end
def parse_page(page)
layout_content = (page.layout)? page.layout.content : "<r:content />"
context = parser_context(page.content)
parser = Radius::Parser.new(context, :tag_prefix => 'r')
parser.parse(parser.parse(layout_content))
end
end

View File

@ -3,40 +3,60 @@
namespace :dev do namespace :dev do
task :build => :environment do task :build => :environment do
[Item, Layout, Site, Snippet, User, UserAttributeModel].each { |m| m.delete_all }
User.create!( :email => 'chris@rulingcom.com', puts 'Empty the MongoDB database, exclude System stuff'
:password => 'password', Mongoid.master.collections.select do |collection|
:password_confirmation => 'password', include = collection.name !~ /system/
:admin => true, puts 'Dropping ' + collection.name if include
:active_attributes => ["teacher"], include
:user_attributes => [ end.each(&:drop)
{:key => 'teacher'}])
Home.create!( :type => "Home", Site.create!( :valid_locales => [ 'en', 'zh_tw' ], :in_use_locales => [ 'zh_tw', 'en' ] )
:content => "This is the home page\r\n{% t bob %}",
:full_name => "home",
:is_published => true,"layout_id" : ObjectId("4d23dadf5b0bad0b1100000e"), "layout_name" : "root", "name" : "home", "parent_id" : null, "parent_name" : null, "position" : 1, "title_en" : "Homepage", "title_zh_tw" : "首頁"
user = User.new( :email => 'chris@rulingcom.com', :password => 'password', :password_confirmation => 'password', :admin => true, :active_roles => ['teacher'])
user.user_roles.build(:key => 'teacher', :discipline_en => 'Database', :discipline_zh_tw => '數據庫' )
user.save!
Layout.create!( :name => 'root', :description => 'root', :content_zh_tw => File.open("#{RAILS_ROOT}/lib/template/root.layout.zh_tw").read, I18nVariable.create!( :document_class => 'language', :key => 'en', :en => 'English', :zh_tw => '英文' )
:content_en => File.open("#{RAILS_ROOT}/lib/template/root.layout.en").read) I18nVariable.create!( :document_class => 'language', :key => 'zh_tw', :en => 'Chinese', :zh_tw => '中文' )
var_1 = I18nVariable.create!( :document_class => 'UserRoleModel', :key => 'teacher', :en => 'Teacher', :zh_tw => '老師' )
var_2 = I18nVariable.create!( :document_class => 'AttributeModel', :key => 'discipline', :en => 'Discipline', :zh_tw => '學科', :parent_id => var_1.id )
var_3 = I18nVariable.create!( :document_class => 'AttributeModel', :key => 'department', :en => 'Department', :zh_tw => '學系', :parent_id => var_1.id )
var_4 = I18nVariable.create!( :document_class => 'UserRoleModel', :key => 'student', :en => 'Student', :zh_tw => '學生' )
var_5 = I18nVariable.create!( :document_class => 'AttributeModel', :key => 'department', :en => 'Department', :zh_tw => '學系', :parent_id => var_4.id )
var_6 = I18nVariable.create!( :document_class => 'AttributeModel', :key => 'major', :en => 'Major', :zh_tw => '主修', :parent_id => var_4.id )
var_7 = I18nVariable.create!( :document_class => 'UserInfoModel', :key => 'profile', :en => 'Profile', :zh_tw => '個人檔案' )
var_8 = I18nVariable.create!( :document_class => 'AttributeModel', :key => 'family_name', :en => 'Family name', :zh_tw => '姓氏', :parent_id => var_7.id )
var_9 = I18nVariable.create!( :document_class => 'AttributeModel', :key => 'first_name', :en => 'First name', :zh_tw => '名字', :parent_id => var_7.id )
var_10 = I18nVariable.create!( :document_class => 'Home', :key => 'home', :en => 'Homepage', :zh_tw => '首頁')
var_11 = I18nVariable.create!( :document_class => 'Page', :key => 'about', :en => 'About', :zh_tw => '關於我們' )
var_12 = I18nVariable.create!( :document_class => 'Link', :key => 'google', :en => 'Google', :zh_tw => 'Google' )
Page.create!( :name => "root", :title => I18n.t(:homepage), :layout_name => "root", :parent_name => nil, urm_1 = UserRoleModel.new( :key => 'teacher', :i18n_variable_id => var_1.id )
:content_zh_tw => File.open("#{RAILS_ROOT}/lib/template/root.page.zh_tw").read, urm_1.attribute_models.build( :key => 'discipline', :locale => true, :i18n_variable_id => var_2.id, :markup => 'text_field', :list_options => [] )
:content_en => File.open("#{RAILS_ROOT}/lib/template/root.page.en").read ) urm_1.attribute_models.build( :key => 'department', :locale => true, :i18n_variable_id => var_3.id, :markup => 'text_field', :list_options => [] )
urm_1.save!
urm_2 = UserRoleModel.new( :key => 'student', :i18n_variable_id => var_4.id )
urm_2.attribute_models.build( :key => 'department', :locale => true, :i18n_variable_id => var_5.id, :markup => 'text_field', :list_options => [] )
urm_2.attribute_models.build( :key => 'major', :locale => true, :i18n_variable_id => var_6.id, :markup => 'text_field', :list_options => [] )
urm_2.save!
uim_1 = UserInfoModel.new( :key => 'profile', :i18n_variable_id => var_7.id )
uim_1.attribute_models.build( :key => 'family_name', :locale => true, :i18n_variable_id => var_8.id, :markup => 'text_field', :list_options => [] )
uim_1.attribute_models.build( :key => 'first_name', :locale => true, :i18n_variable_id => var_9.id, :markup => 'text_field', :list_options => [] )
uim_1.save!
["about"].each do |page_name| layout = Layout.create!( :name => 'root', :description => 'root', :content => File.open("#{RAILS_ROOT}/lib/template/root.layout").read )
Page.create!( :name => page_name, :title => page_name, :layout_name => "root", :parent_name => "root",
:content_zh_tw => File.open("#{RAILS_ROOT}/lib/template/#{page_name}.page.zh_tw").read, home = Home.create!( :i18n_variable_id => var_10.id, :layout_id => layout.id, :name => 'home', :is_published => true, :content => File.open("#{RAILS_ROOT}/lib/template/root.home").read )
:content_en => File.open("#{RAILS_ROOT}/lib/template/#{page_name}.page.en").read )
end Page.create!( :i18n_variable_id => var_11.id, :layout_id => layout.id, :name => 'about', :is_published => true, :parent_id => home.id, :content => File.open("#{RAILS_ROOT}/lib/template/about.page").read )
Link.create!( :i18n_variable_id => var_12.id, :name => 'google', :is_published => true, :parent_id => home.id, :url => 'www.google.com' )
["nav", "footer"].each do |page_name|
Snippet.create!( :name => page_name, :parent_id => home.id, :content => File.open("#{RAILS_ROOT}/lib/template/#{page_name}.snippet").read )
end
["nav", "footer", "locale"].each do |page_name|
Snippet.create!( :name => page_name, :parent_name => "root",
:content_zh_tw => File.open("#{RAILS_ROOT}/lib/template/#{page_name}.snippet.zh_tw").read,
:content_en => File.open("#{RAILS_ROOT}/lib/template/#{page_name}.snippet.en").read )
end
end end
end end

1
lib/template/about.page Normal file
View File

@ -0,0 +1 @@
This is about

View File

@ -0,0 +1 @@
<p>Footer</p>

5
lib/template/nav.snippet Normal file
View File

@ -0,0 +1,5 @@
<ul>
<li><a href='/'><r:locale en='Home' zh_tw='首頁' /></a></li>
<li><a href='/about'><r:locale en='About' zh_tw='關於我們' /></a></li>
<li><a href='/google'>Google</a></li>
</ul>

1
lib/template/root.home Normal file
View File

@ -0,0 +1 @@
<r:locale en='This is the homepage' zh_tw='這是首頁' />

25
lib/template/root.layout Normal file
View File

@ -0,0 +1,25 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>RulingSite</title>
</head>
<body>
<div id="header">
<r:language_bar />
<r:snippet name='nav' />
</div>
<div id="container">
<r:content />
</div>
<div id="footer">
<r:snippet name='footer' />
</div>
</body>
</html>