Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Heukmes committed Nov 22, 2009
0 parents commit dfac2d9
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2009 [name of plugin creator]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
54 changes: 54 additions & 0 deletions README.rdoc
@@ -0,0 +1,54 @@
= MongoTranslation

This Rails plugin for MongoMapper allows you to easily translate your documents in multiple languages.

= Setup

Once the plugin is installed, edit your config/environment.rb file to define your locales :

config.i18n.default_locale = :fr
LANGS = [:fr, :nl, :en]

Then in your model, you have to include the MongoTranslation module and define the keys you want to translate :

class Article
include MongoMapper::Document
include MongoTranslation

key :author, String
key :title, String
key :content, String

translates :title, :content
end

The translates method will generate a ArticleTranslation model (EmbeddedDocument) with the required keys.

= Use it!

A call to article.title will load the title depending on the current locale :

article = Article.create(:author => 'antho', :title => 'test', :content => 'content')
translations = {
'nl' => { 'title' => 'testNL', 'content' => 'contentNL' },
'en' => { 'title' => 'testEN', 'content' => 'contentEN' }
}
article.create_or_update_translations(translations)

# I18n.locale = :fr == default locale
article.title # => 'test'

# I18n.locale = :nl
article.title # => 'testNL'

# Specify the locale you want
article.title(:en) => 'testEN'

create_or_update_translations is just a helper offered by the plugin, but you can manipulate your translations manually :

article.article_translations << ArticleTranslation.new(:title => 'testNL', :content => 'contentNL')
article.article_translations << ArticleTranslation.new(:title => 'testEN', :content => 'contentEN')



Copyright (c) 2009 Anthony Heukmes, released under the MIT license
23 changes: 23 additions & 0 deletions Rakefile
@@ -0,0 +1,23 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the mongo_translation plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the mongo_translation plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'MongoTranslation'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
1 change: 1 addition & 0 deletions init.rb
@@ -0,0 +1 @@
# Include hook code here
1 change: 1 addition & 0 deletions install.rb
@@ -0,0 +1 @@
# Install hook code here
68 changes: 68 additions & 0 deletions lib/mongo_translation.rb
@@ -0,0 +1,68 @@
module MongoTranslation
def self.included(klass)
klass.extend ClassMethods
end

def create_or_update_translations(params)
translations = send("#{self.class.to_s}Translation".underscore.pluralize.to_sym)
translation_class = Kernel.const_get("#{self.class.to_s}Translation")

# update existing translations
translations.each do |translation|
translation.update_attributes(params.delete(translation.locale))
end

# create new translations
(LANGS - [I18n.default_locale]).each do |lang|
attrs = params[lang.to_s]
if attrs
tr = translation_class.new(attrs)
tr.locale = lang.to_s
translations << tr
end
end

save
end

module ClassMethods
def translates(*args)
klass_name = "#{name.camelcase}Translation"

# Create the translation model
translation_model = Class.new do
include MongoMapper::EmbeddedDocument
key :locale, String
args.each do |attr|
key attr.to_sym, String
end
end
Object.const_set klass_name, translation_model

instance_eval do
has_many klass_name.underscore.pluralize.to_sym
end

# Add attributes readers
class_eval do
args.each do |attr|
define_method(attr) do | *params |
requested_locale = params.first
requested_locale = I18n.locale if requested_locale.nil?
if requested_locale == I18n.default_locale
instance_variable_get("@#{attr}")
else
translations = send(klass_name.underscore.pluralize.to_sym)
translated_attr = nil
translations.each do |translation|
translated_attr = translation.send(attr.to_sym) if translation.locale.to_sym == requested_locale
end
translated_attr = instance_variable_get("@#{attr}") if translated_attr.nil?
translated_attr
end
end
end
end
end
end
end
4 changes: 4 additions & 0 deletions tasks/mongo_translation_tasks.rake
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :mongo_translation do
# # Task goes here
# end
1 change: 1 addition & 0 deletions uninstall.rb
@@ -0,0 +1 @@
# Uninstall hook code here

0 comments on commit dfac2d9

Please sign in to comment.