Module: R18n::Translated

Defined in:
lib/r18n-core/translated.rb

Overview

Module to add i18n support to any class. It will be useful for ORM or R18n plugin with out-of-box i18n support.

Module can add proxy-methods to find translation in object methods. For example, if you class have title_en and title_ru methods, you can add proxy-method title, which will use title_ru for Russian users and title_en for English:

require 'r18n-core/translated'

class Product
  include DataMapper::Resource
  property :title_ru, String
  property :title_en, String
  property :desciption_ru, String
  property :desciption_en, String

  include R18n::Translated
  translations :title, :desciption
end

# User know only Russian
R18n.set('ru')

product.title #=> Untranslated

# Set value to English (default) title
product.title_en = "Anthrax"
product.title #=> "Anthrax"
product.title.locale #=> Locale en (English)

# Set value to title on user locale (Russian)
product.title = "Сибирская язва"
product.title #=> "Сибирская язва"
product.title.locale #=> Locale ru (Russian)

product.title_en #=> "Anthrax"
product.title_ru #=> "Сибирская язва"

Proxy-method support all funtion from I18n: global and type filters, pluralization, variables. It also return TranslatedString or Untranslated.

Note, you must set your I18n object by R18n.set and don’t forget to require 'r18n-core/translated'. R18n plugins (sinatra-r18n, r18-desktop) set I18n object by R18n.set automatically, but you must call i18n helper in Sinatra before use models.

See R18n::Translated::Base for class method documentation.

Options

You can set option for proxy-method as Hash with keys;

  • type – YAML type for filters. For example, “markdown” or “escape_html”.

  • methods – manual method map as Hash of locale codes to method names.

  • no_params – set it to true if you proxy-method must send it parameters only to filters.

  • no_write – set it to true if you don’t want to create proxy-setters.

Method translation will be more useful for options:

translation :title, :methods => {:ru => :russian, :en => :english}

Defined Under Namespace

Modules: Base

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



84
85
86
87
88
89
# File 'lib/r18n-core/translated.rb', line 84

def included(base) #:nodoc:
  base.send :extend, Base
  base.instance_variable_set '@unlocalized_getters', {}
  base.instance_variable_set '@unlocalized_setters', {}
  base.instance_variable_set '@translation_types', {}
end