Add MongoMapper key_i18n support

This commit is contained in:
Wen-Tien Chang 2010-01-08 15:32:44 +08:00
parent a674e46c91
commit 5ab32baf88
7 changed files with 23 additions and 36 deletions

View File

@ -13,8 +13,8 @@ class ApplicationController < ActionController::Base
if @page
@layout = @page.layout
@page_options ||= {}
@page_content = Liquid::Template.parse( @page.send( "content_#{I18n.locale}" ) ).render(@page_options)
@layout_content = (@page.layout)? @layout.send( "content_#{I18n.locale}" ) : "{{page_content}}"
@page_content = Liquid::Template.parse( @page.content ).render(@page_options)
@layout_content = (@page.layout)? @layout.content : "{{page_content}}"
render :text => Liquid::Template.parse(@layout_content).render( 'page_content' => @page_content )
else
render :text => '404 Not Found'

View File

@ -2,21 +2,11 @@ class Announcement
include MongoMapper::Document
key :title_en, String
key :title_zh_tw, String
key :content_en, String
key :content_zh_tw, String
key_i18n :title, String
key_i18n :content, String
def to_liquid
{ "id" => self.id, "title" => self.send("title_#{I18n.locale}"), "content" => self.send("content_#{I18n.locale}") }
end
def title
self.send("title_#{I18n.locale}")
end
def content
self.send("content_#{I18n.locale}")
{ "id" => self.id, "title" => self.title, "content" => self.content }
end
end

View File

@ -3,8 +3,7 @@ class Layout
include MongoMapper::Document
key :name, String
key :content_en, String
key :content_zh_tw, String
key_i18n :content, String
validates_presence_of :name

View File

@ -3,9 +3,8 @@ class Page
key :name, String
key :parent_page_id, String
key :content_en, String
key :content_zh_tw, String
key_i18n :content, String
key :layout_id, String
key :layout_name, String

View File

@ -3,8 +3,7 @@ class Snippet
include MongoMapper::Document
key :name, String
key :content_en, String
key :content_zh_tw, String
key_i18n :content, String
validates_presence_of :name

View File

@ -4,7 +4,7 @@ module SnippetFilter
snippet = Snippet.find_by_name(snippet_name)
if snippet
return Liquid::Template.parse( snippet.send( "content_#{I18n.locale}" ) ).render
return Liquid::Template.parse( snippet.content ).render
else
return "nothing"
end

View File

@ -50,18 +50,18 @@ VALID_LOCALES = ["en", "zh_tw"]
MongoMapper.database = "r4-#{Rails.env}"
#class CouchFoo::Base
#
# def self.property_i18n(property_name, property_type)
# VALID_LOCALES.each do |locale|
# property "#{property_name.to_s}_#{locale}".to_sym, property_type
# end
#
# define_method( property_name ) do
# self.read_attribute("#{property_name.to_s}_#{I18n.locale}")
# end
# end
#
#end
module MongoMapper::Document::ClassMethods
def key_i18n(key, *options)
VALID_LOCALES.each do |locale|
key "#{key.to_s}_#{locale}".to_sym, *options
end
define_method(key) do
self.send("#{key.to_s}_#{I18n.locale}")
end
end
end