![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
simple_model_translations
Advanced tools
= SimpleModelTranslations
Yet another implementation of ActiveRecord translations. Created for zn.ua, because globalize3 was too uncomfortable to use. It borrows some things from globalize3[http://github.com/svenfuchs/globalize3] and puret[http://github.com/jo/puret].
== Basic Usage
For example, you are dealing with a website for some magazine, and you want your articles to be translated. So, you'll need the following models to achieve this behaviour (warning: ArticleTranslation should be defined before Article, or Rails should be able to autoload it):
class ArticleTranslation < ActiveRecord::Base translation_for :article end
class Article < ActiveRecord::Base translates :name, :content end
or, if you are not going to add some additional behavior to translation class (for example, validations), you can use translation class, created for you by default:
class Article < ActiveRecord::Base translates :name, :content end
ArticleTranslation will be generated automagically :)
Also, you'll need a migration:
create_table(:article_translations) do |t| t.references :article t.string :locale
t.string :name
t.text :content
end add_index :article_translations, [:article_id, :locale], :unique => true
Now you are able to translate values for the attributes :title and :description per locale:
I18n.locale = :en article = Article.new(:name => 'Translations are so simple!') I18n.locale = :uk article.name = 'Hello, from Ukraine!'
I18n.locale = :en article.name #=> 'Translations are so simple!' I18n.locale = :uk article.name #=> 'Hello, from Ukraine!'
Additional features can be discovered by searching the code and specs. Documentation is not available yet. I hope, it'll be done when releasing zn.ua. :)
== Nested attributes
Usually, when creating bootstrap data (e.g. seeds.rb) you want to pass all translations in one statement. You can do it using Rails' nested attributes, and we propose a very convenient option for that:
class Category < ActiveRecord::Base translates :name, :attributes => true end
Usage:
Category.create!(:translations_attributes => [ { :locale => :en, :name => 'Science' }, { :locale => :ru, :name => 'Наука' } ])
== Fetching records with selected translations
If you want (and you probably do want) to fetch only records with specified translation, you can do it now:
Article.with_translation(:ru)
This code returns an ActiveRecord::Relation object, so you can chain your query furthermore.
== Note on Patches/Pull Requests
== Copyright
Copyright (c) 2010 Pavel Forkert. See LICENSE for details.
FAQs
Unknown package
We found that simple_model_translations demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.