Module: R18n

Defined in:
lib/r18n-core/utils.rb,
lib/r18n-core.rb,
lib/r18n-core/i18n.rb,
lib/r18n-core/locale.rb,
lib/r18n-core/filters.rb,
lib/r18n-core/helpers.rb,
lib/r18n-core/version.rb,
lib/r18n-core/translated.rb,
lib/r18n-core/filter_list.rb,
lib/r18n-core/translation.rb,
lib/r18n-core/yaml_loader.rb,
lib/r18n-core/untranslated.rb,
lib/r18n-core/translated_string.rb,
lib/r18n-core/unsupported_locale.rb

Overview

Locale withou information file to i18n support.

Copyright © 2008 Andrey “A.I.” Sitnik <[email protected]>

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Defined Under Namespace

Modules: Filters, Helpers, Loader, Locales, Translated, Utils Classes: CustomFilterList, FilterList, GlobalFilterList, I18n, Locale, TranslatedString, Translation, Typed, UnpluralizetedTranslation, UnsupportedLocale, Untranslated

Constant Summary collapse

VERSION =
'1.1.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cacheObject

Hash of hash-like (see Moneta) object to store loaded translations.



155
156
157
# File 'lib/r18n-core.rb', line 155

def cache
  @cache
end

.default_loaderObject

Default loader class, which will be used if you didn’t send loader to I18n.new (object with available and load methods).



148
149
150
# File 'lib/r18n-core.rb', line 148

def default_loader
  @default_loader
end

.default_places(&block) ⇒ Object

Default places for R18n.set and R18n.available_locales.

You can set block to calculate places dynamically:

R18n.default_places { settings.i18n_places }


134
135
136
# File 'lib/r18n-core.rb', line 134

def default_places
  @default_places
end

.extension_placesObject

Loaders with extension translations. If application translations with same locale isn’t exists, extension file willn’t be used.



152
153
154
# File 'lib/r18n-core.rb', line 152

def extension_places
  @extension_places
end

Class Method Details

.available_locales(places = R18n.default_places) ⇒ Object

Return Array of locales with available translations. You can miss translation places, it will be taken from R18n.default_places.



126
127
128
# File 'lib/r18n-core.rb', line 126

def available_locales(places = R18n.default_places)
  R18n::I18n.convert_places(places).map { |i| i.available }.flatten.uniq
end

.change(locale) ⇒ Object

Return I18n object for locale. Useful to temporary change locale, for example, to show text in locales list:

- R18n.available_locales.each do |locale|
  - R18n.change(locale).t.language_title


111
112
113
114
115
116
# File 'lib/r18n-core.rb', line 111

def change(locale)
  locale = locale.code if locale.is_a? Locale
  exists = get ? get.locales.map { |i| i.code } : []
  places = get ? get.translation_places : R18n.default_places
  R18n::I18n.new([locale] + exists, places)
end

.clear_cache!Object

Clean translations cache.



78
79
80
# File 'lib/r18n-core.rb', line 78

def clear_cache!
  self.cache = { }
end

.getObject

Get I18n object for current thread.



70
71
72
73
74
75
# File 'lib/r18n-core.rb', line 70

def get
  thread[:r18n_i18n] ||
  (thread[:r18n_setter] && thread_set(thread[:r18n_setter].call)) ||
  @i18n ||
  (@setter && set(@setter.call))
end

.l(*params) ⇒ Object

Localize object. Alias for R18n.get.l.



102
103
104
# File 'lib/r18n-core.rb', line 102

def l(*params)
  get.l(*params)
end

.locale(code) ⇒ Object

Return Locale object by locale code. It’s shortcut for R18n::Locale.load(code).



120
121
122
# File 'lib/r18n-core.rb', line 120

def locale(code)
  R18n::Locale.load(code)
end

.reset!Object Also known as: reset

Delete I18n object from current thread and global variable.



83
84
85
86
# File 'lib/r18n-core.rb', line 83

def reset!
  thread[:r18n_i18n] = thread[:r18n_setter] = @i18n = @setter = nil
  clear_cache!
end

.set(i18n = nil, places = R18n.default_places, &block) ⇒ Object

Set I18n object globally. You can miss translation places, it will be taken from R18n.default_places.



48
49
50
51
52
53
54
55
56
57
# File 'lib/r18n-core.rb', line 48

def set(i18n = nil, places = R18n.default_places, &block)
  if block_given?
    @setter = block
    @i18n   = nil
  elsif i18n.is_a? I18n
    @i18n = i18n
  else
    @i18n = I18n.new(i18n, places)
  end
end

.t(*params) ⇒ Object

Translate message. Alias for R18n.get.t.



97
98
99
# File 'lib/r18n-core.rb', line 97

def t(*params)
  get.t(*params)
end

.threadObject

Get the current thread.



92
93
94
# File 'lib/r18n-core.rb', line 92

def thread
  Thread.current
end

.thread_set(i18n = nil, &block) ⇒ Object

Set I18n object to current thread.



60
61
62
63
64
65
66
67
# File 'lib/r18n-core.rb', line 60

def thread_set(i18n = nil, &block)
  if block_given?
    thread[:r18n_setter] = block
    thread[:r18n_i18n]   = nil
  else
    thread[:r18n_i18n] = i18n
  end
end