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:
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.
By default it use ‘R18n.get` to get I18n object (so, you must set it before use model), but you can overwrite object `r18n` method to change default logic.
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
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#r18n ⇒ Object
Access to I18n object.
Class Method Details
.included(base) ⇒ Object
:nodoc:
80 81 82 83 84 |
# File 'lib/r18n-core/translated.rb', line 80 def included(base) #:nodoc: base.send :extend, Base base.instance_variable_set '@unlocalized_getters', {} base.instance_variable_set '@unlocalized_setters', {} end |