Skip to content

Commit

Permalink
Properly handle i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Feb 21, 2011
1 parent 4cba263 commit e8d3d15
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 7 deletions.
3 changes: 3 additions & 0 deletions lib/mongo_mapper.rb
Expand Up @@ -4,6 +4,8 @@
require 'active_model'
require "mongo_mapper/railtie" if defined?(Rails)

I18n.load_path << File.expand_path('../mongo_mapper/locale/en.yml', __FILE__)

module MongoMapper
autoload :Connection, 'mongo_mapper/connection'

Expand All @@ -16,6 +18,7 @@ module MongoMapper
autoload :Document, 'mongo_mapper/document'
autoload :EmbeddedDocument, 'mongo_mapper/embedded_document'
autoload :Plugins, 'mongo_mapper/plugins'
autoload :Translation, 'mongo_mapper/translation'
autoload :Version, 'mongo_mapper/version'

module Middleware
Expand Down
1 change: 1 addition & 0 deletions lib/mongo_mapper/document.rb
Expand Up @@ -36,6 +36,7 @@ module Document

included do
extend Plugins
extend Translation
end
end # Document
end # MongoMapper
1 change: 1 addition & 0 deletions lib/mongo_mapper/embedded_document.rb
Expand Up @@ -25,6 +25,7 @@ module EmbeddedDocument

included do
extend Plugins
extend Translation
end
end
end
5 changes: 5 additions & 0 deletions lib/mongo_mapper/locale/en.yml
@@ -0,0 +1,5 @@
en:
mongo_mapper:
errors:
messages:
taken: "has already been taken"
10 changes: 10 additions & 0 deletions lib/mongo_mapper/translation.rb
@@ -0,0 +1,10 @@
# encoding: UTF-8
module MongoMapper
module Translation
include ActiveModel::Translation

def i18n_scope
:mongo_mapper
end
end
end
5 changes: 0 additions & 5 deletions test/fixtures/en.yml

This file was deleted.

2 changes: 0 additions & 2 deletions test/test_helper.rb
Expand Up @@ -102,5 +102,3 @@ def drop_indexes(klass)
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => logger)
MongoMapper.database = "mm-test-#{RUBY_VERSION.gsub('.', '-')}"
MongoMapper.database.collections.each { |c| c.drop_indexes }

I18n.load_path << File.expand_path('../fixtures/en.yml', __FILE__)
27 changes: 27 additions & 0 deletions test/unit/test_translation.rb
@@ -0,0 +1,27 @@
require 'test_helper'

class TranslationTest < Test::Unit::TestCase
should "translate add mongo_mapper translations" do
I18n.translate("mongo_mapper.errors.messages.taken").should == "has already been taken"
end

should "set i18n_scope" do
Doc().i18n_scope.should == :mongo_mapper
end

should "translate document attributes" do
I18n.config.backend.store_translations(:en, :mongo_mapper => {:attributes => {:thing => {:foo => 'Bar'}}})
doc = Doc('Thing') do
key :foo, String
end
doc.human_attribute_name(:foo).should == 'Bar'
end

should "translate embedded document attributes" do
I18n.config.backend.store_translations(:en, :mongo_mapper => {:attributes => {:thing => {:foo => 'Bar'}}})
doc = EDoc('Thing') do
key :foo, String
end
doc.human_attribute_name(:foo).should == 'Bar'
end
end

0 comments on commit e8d3d15

Please sign in to comment.