From ff2dffaeb40cbccfc18748d40197db1e26696c03 Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Mon, 6 Jul 2009 16:23:34 +0800 Subject: [PATCH] add i18n for layout and snippet --- app/controllers/admin/snippets_controller.rb | 5 +++- app/controllers/application_controller.rb | 2 +- app/controllers/pages_controller.rb | 4 ++-- app/models/announcement.rb | 6 ++--- app/models/layout.rb | 1 - app/models/snippet.rb | 1 - app/models/snippet_filter.rb | 2 +- app/views/admin/layouts/_form.html.erb | 25 ++++++++++++++++++++ app/views/admin/layouts/edit.html.erb | 11 +-------- app/views/admin/layouts/new.html.erb | 10 +------- app/views/admin/snippets/_form.html.erb | 25 ++++++++++++++++++++ app/views/admin/snippets/edit.html.erb | 10 +------- app/views/admin/snippets/new.html.erb | 10 +------- config/environment.rb | 2 +- 14 files changed, 65 insertions(+), 49 deletions(-) create mode 100644 app/views/admin/layouts/_form.html.erb create mode 100644 app/views/admin/snippets/_form.html.erb diff --git a/app/controllers/admin/snippets_controller.rb b/app/controllers/admin/snippets_controller.rb index 4e94206f..620bc59e 100644 --- a/app/controllers/admin/snippets_controller.rb +++ b/app/controllers/admin/snippets_controller.rb @@ -36,7 +36,10 @@ class Admin::SnippetsController < ApplicationController end end - @snippet.content = "" + VALID_LOCALES.each do |locale| + @snippet.write_attribute( "content_#{locale}", "" ) + end + end respond_to do |format| diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5bed4b2c..5fb975d7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,7 +14,7 @@ class ApplicationController < ActionController::Base @layout = @page.layout @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.read_attribute( "content_#{I18n.locale}" ) : "{{page_content}}" render :text => Liquid::Template.parse(@layout_content).render( 'page_content' => @page_content ) else render :text => '404 Not Found' diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index ecbf101a..2877225e 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -13,8 +13,8 @@ class PagesController < ApplicationController @page = Page.find_by_name(params[:page_name]) if @page && !@page.external_link.blank? - redirect_to @page.external_link - elsif @page && @page.use_engine + # redirect_to @page.external_link + elsif @page && !@page.use_engine.blank? #model_class = Kernel.const_get( "Announcement" ) # page.use_engine redirect_to announcements_path else diff --git a/app/models/announcement.rb b/app/models/announcement.rb index b6d6c64b..474ccc05 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -1,11 +1,9 @@ require 'couch_foo' class Announcement < CouchFoo::Base - property :title, String - property :content, String + property_i18n :title, String + property_i18n :content, String - validates_presence_of :title - def to_liquid { "id" => self.id, "title" => self.title, "content" => self.content } end diff --git a/app/models/layout.rb b/app/models/layout.rb index 24425784..e5408386 100644 --- a/app/models/layout.rb +++ b/app/models/layout.rb @@ -1,7 +1,6 @@ class Layout < CouchFoo::Base property :name, String - property :content, String #remove later property_i18n :content, String validates_presence_of :name diff --git a/app/models/snippet.rb b/app/models/snippet.rb index e2a6f3d9..4e49667e 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -1,7 +1,6 @@ class Snippet < CouchFoo::Base property :name, String - property :content, String # remove later property_i18n :content, String validates_presence_of :name diff --git a/app/models/snippet_filter.rb b/app/models/snippet_filter.rb index e0b12c23..18b265d7 100644 --- a/app/models/snippet_filter.rb +++ b/app/models/snippet_filter.rb @@ -4,7 +4,7 @@ module SnippetFilter snippet = Snippet.find_by_name(snippet_name) if snippet - return Liquid::Template.parse(snippet.content).render + return Liquid::Template.parse( snippet.read_attribute( "content_#{I18n.locale}" ) ).render else return "nothing" end diff --git a/app/views/admin/layouts/_form.html.erb b/app/views/admin/layouts/_form.html.erb new file mode 100644 index 00000000..f5a2750e --- /dev/null +++ b/app/views/admin/layouts/_form.html.erb @@ -0,0 +1,25 @@ +

+<%= f.label :name, "Name" %> +<%= f.text_field :name, :class => 'text' %> +

+ +

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

+ +

Edit english

+ +

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

+ +<% content_for :page_specific_javascript do %> + +<% end -%> \ No newline at end of file diff --git a/app/views/admin/layouts/edit.html.erb b/app/views/admin/layouts/edit.html.erb index fd432eb9..ce171a36 100644 --- a/app/views/admin/layouts/edit.html.erb +++ b/app/views/admin/layouts/edit.html.erb @@ -3,16 +3,7 @@ <% form_for @layout, :url => admin_layout_path(@layout) do |f| %> <%= f.error_messages %> -

- <%= f.label :name, "Name" %> - <%= f.text_field :name, :class => 'text' %> -

- -

- <%= f.label :content, "Content" %> - <%= f.text_area :content, :size => '100x30' %> -

- + <%= render :partial => "form", :locals => { :f => f } %>

<%= f.submit 'Update' %>

diff --git a/app/views/admin/layouts/new.html.erb b/app/views/admin/layouts/new.html.erb index 844fdc22..2c746210 100644 --- a/app/views/admin/layouts/new.html.erb +++ b/app/views/admin/layouts/new.html.erb @@ -3,15 +3,7 @@ <% form_for :layout, :url => admin_layouts_path do |f| %> <%= f.error_messages %> -

- <%= f.label :name, "Name" %> - <%= f.text_field :name, :class => 'text' %> -

- -

- <%= f.label :content, "Content" %> - <%= f.text_area :content, :size => '100x30' %> -

+ <%= render :partial => "form", :locals => { :f => f } %>

<%= f.submit 'Create' %> diff --git a/app/views/admin/snippets/_form.html.erb b/app/views/admin/snippets/_form.html.erb new file mode 100644 index 00000000..f5a2750e --- /dev/null +++ b/app/views/admin/snippets/_form.html.erb @@ -0,0 +1,25 @@ +

+<%= f.label :name, "Name" %> +<%= f.text_field :name, :class => 'text' %> +

+ +

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

+ +

Edit english

+ +

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

+ +<% content_for :page_specific_javascript do %> + +<% end -%> \ No newline at end of file diff --git a/app/views/admin/snippets/edit.html.erb b/app/views/admin/snippets/edit.html.erb index c661e5bd..dfa6eb95 100644 --- a/app/views/admin/snippets/edit.html.erb +++ b/app/views/admin/snippets/edit.html.erb @@ -3,15 +3,7 @@ <% form_for @snippet, :url => admin_snippet_path(@snippet) do |f| %> <%= f.error_messages %> -

- <%= f.label :name, "Name" %> - <%= f.text_field :name, :class => 'text' %> -

- -

- <%= f.label :content, "Content" %> - <%= f.text_area :content, :size => '100x30' %> -

+ <%= render :partial => "form", :locals => { :f => f } %>

<%= f.submit 'Update' %> diff --git a/app/views/admin/snippets/new.html.erb b/app/views/admin/snippets/new.html.erb index fe1005c8..d6e845a2 100644 --- a/app/views/admin/snippets/new.html.erb +++ b/app/views/admin/snippets/new.html.erb @@ -3,15 +3,7 @@ <% form_for :snippet, :url => admin_snippets_path do |f| %> <%= f.error_messages %> -

- <%= f.label :name, "Name" %> - <%= f.text_field :name, :class => 'text' %> -

- -

- <%= f.label :content, "Content" %> - <%= f.text_area :content, :size => '100x30' %> -

+ <%= render :partial => "form", :locals => { :f => f } %>

<%= f.submit 'Create' %> diff --git a/config/environment.rb b/config/environment.rb index c6fd1f9b..659f2775 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -49,7 +49,7 @@ class CouchFoo::Base def self.property_i18n(property_name, property_type) VALID_LOCALES.each do |locale| - property "#{property_name.to_s}_#{locale}", property_type + property "#{property_name.to_s}_#{locale}".to_sym, property_type end end