From db8d2bfa8fd2d2ba947bb715aa526247c6372a33 Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Fri, 19 Jun 2009 17:31:10 +0800 Subject: [PATCH] let page model support i18n --- app/controllers/application_controller.rb | 14 +++++++++++++- app/models/page.rb | 8 +++++++- app/views/admin/pages/_form.html.erb | 9 +++++++-- config/environment.rb | 3 ++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5abaca55..5bed4b2c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,6 +3,8 @@ class ApplicationController < ActionController::Base helper :all protect_from_forgery + before_filter :set_locale + filter_parameter_logging :password Liquid::Template.register_filter(SnippetFilter) @@ -11,7 +13,7 @@ class ApplicationController < ActionController::Base if @page @layout = @page.layout @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}}" render :text => Liquid::Template.parse(@layout_content).render( 'page_content' => @page_content ) else @@ -19,4 +21,14 @@ class ApplicationController < ActionController::Base 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 diff --git a/app/models/page.rb b/app/models/page.rb index f7270cd1..f7227225 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -1,8 +1,14 @@ class Page < CouchFoo::Base + property :lang, String property :name, 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_name, String property :use_engine, String diff --git a/app/views/admin/pages/_form.html.erb b/app/views/admin/pages/_form.html.erb index 913fd521..cc349334 100644 --- a/app/views/admin/pages/_form.html.erb +++ b/app/views/admin/pages/_form.html.erb @@ -9,8 +9,13 @@

-<%= f.label :content, "Content" %> -<%= f.text_area :content, :size => '100x30' %> +<%= f.label "content_en", "Content (en)" %> +<%= f.text_area "content_en", :size => '100x30' %> +

+ +

+<%= f.label "content_zh_tw", "Content (zh_tw)" %> +<%= f.text_area "content_zh_tw", :size => '100x30' %>

diff --git a/config/environment.rb b/config/environment.rb index 67a14735..5e11e952 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -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. # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] - # config.i18n.default_locale = :de + config.i18n.default_locale = "zh_tw" end +VALID_LOCALES = ["en", "zh_tw"] require 'couch_foo' CouchFoo::Base.set_database(:host => "http://localhost:5984", :database => "r4") \ No newline at end of file