let page model support i18n

This commit is contained in:
Wen-Tien Chang 2009-06-19 17:31:10 +08:00
parent bd9ef6d286
commit db8d2bfa8f
4 changed files with 29 additions and 5 deletions

View File

@ -3,6 +3,8 @@ class ApplicationController < ActionController::Base
helper :all helper :all
protect_from_forgery protect_from_forgery
before_filter :set_locale
filter_parameter_logging :password filter_parameter_logging :password
Liquid::Template.register_filter(SnippetFilter) Liquid::Template.register_filter(SnippetFilter)
@ -11,7 +13,7 @@ class ApplicationController < ActionController::Base
if @page if @page
@layout = @page.layout @layout = @page.layout
@page_options ||= {} @page_options ||= {}
@page_content = Liquid::Template.parse(@page.content).render(@page_options) @page_content = Liquid::Template.parse( @page.read_attribute( "content_#{I18n.locale}" ) ).render(@page_options)
@layout_content = (@page.layout)? @layout.content : "{{page_content}}" @layout_content = (@page.layout)? @layout.content : "{{page_content}}"
render :text => Liquid::Template.parse(@layout_content).render( 'page_content' => @page_content ) render :text => Liquid::Template.parse(@layout_content).render( 'page_content' => @page_content )
else else
@ -19,4 +21,14 @@ class ApplicationController < ActionController::Base
end end
end end
def set_locale
# update session if passed
if params[:locale] && VALID_LOCALES.include?( params[:locale] )
session[:locale] = params[:locale]
end
# set locale based on session or default
I18n.locale = session[:locale] || I18n.default_locale
end
end end

View File

@ -1,8 +1,14 @@
class Page < CouchFoo::Base class Page < CouchFoo::Base
property :lang, String
property :name, String property :name, String
property :parent_page_id, String property :parent_page_id, String
property :content, String
property :content_zh_tw, String
property :content_en, String
property :content_ja, String
# TODO: more language can dynamic added
property :layout_id, String property :layout_id, String
property :layout_name, String property :layout_name, String
property :use_engine, String property :use_engine, String

View File

@ -9,8 +9,13 @@
</p> </p>
<p> <p>
<%= f.label :content, "Content" %> <%= f.label "content_en", "Content (en)" %>
<%= f.text_area :content, :size => '100x30' %> <%= f.text_area "content_en", :size => '100x30' %>
</p>
<p>
<%= f.label "content_zh_tw", "Content (zh_tw)" %>
<%= f.text_area "content_zh_tw", :size => '100x30' %>
</p> </p>
<p> <p>

View File

@ -37,9 +37,10 @@ Rails::Initializer.run do |config|
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de config.i18n.default_locale = "zh_tw"
end end
VALID_LOCALES = ["en", "zh_tw"]
require 'couch_foo' require 'couch_foo'
CouchFoo::Base.set_database(:host => "http://localhost:5984", :database => "r4") CouchFoo::Base.set_database(:host => "http://localhost:5984", :database => "r4")