Module: MiniI18n

Extended by:
Localization
Defined in:
lib/mini_i18n.rb,
lib/mini_i18n/utils.rb,
lib/mini_i18n/version.rb,
lib/mini_i18n/localization.rb,
lib/mini_i18n/pluralization.rb

Defined Under Namespace

Modules: Localization, Pluralization, Utils

Constant Summary collapse

DEFAULT_LOCALE =
:en
DEFAULT_SEPARATOR =
'.'
VERSION =
"0.8.0"

Constants included from Localization

Localization::DAYS_MONTHS_REGEX, Localization::DELIMITER_REGEX

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Localization

localize

Class Attribute Details

.fallbacksObject

Returns the value of attribute fallbacks


14
15
16
# File 'lib/mini_i18n.rb', line 14

def fallbacks
  @fallbacks
end

Class Method Details

.available_localesObject


24
25
26
# File 'lib/mini_i18n.rb', line 24

def available_locales
  @@available_locales ||= translations.keys
end

.available_locales=(new_locales) ⇒ Object


28
29
30
# File 'lib/mini_i18n.rb', line 28

def available_locales=(new_locales)
  @@available_locales = Array(new_locales).map(&:to_s)
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (MiniI18n)

    the object that the method was called on


60
61
62
# File 'lib/mini_i18n.rb', line 60

def configure
  yield(self) if block_given?
end

.default_localeObject


16
17
18
# File 'lib/mini_i18n.rb', line 16

def default_locale
  @@default_locale ||= DEFAULT_LOCALE
end

.default_locale=(new_locale) ⇒ Object


20
21
22
# File 'lib/mini_i18n.rb', line 20

def default_locale=(new_locale)
  @@default_locale = available_locale?(new_locale) || default_locale
end

.load_translations(path) ⇒ Object


64
65
66
67
68
69
70
# File 'lib/mini_i18n.rb', line 64

def load_translations(path)
  Dir[path.to_s].each do |file|
    YAML.load_file(file).each do |locale, new_translations|
      add_translations(locale.to_s, new_translations)
    end
  end
end

.localeObject


36
37
38
# File 'lib/mini_i18n.rb', line 36

def locale
  Thread.current[:mini_i18n_locale] ||= default_locale
end

.locale=(new_locale) ⇒ Object


40
41
42
# File 'lib/mini_i18n.rb', line 40

def locale=(new_locale)
  set_locale(new_locale)
end

.pluralization_rulesObject


52
53
54
# File 'lib/mini_i18n.rb', line 52

def pluralization_rules
  @@pluralization_rules ||= {}
end

.pluralization_rules=(new_rules) ⇒ Object


56
57
58
# File 'lib/mini_i18n.rb', line 56

def pluralization_rules=(new_rules)
  @@pluralization_rules = new_rules
end

.separatorObject


44
45
46
# File 'lib/mini_i18n.rb', line 44

def separator
  @@separator ||= DEFAULT_SEPARATOR
end

.separator=(new_separator) ⇒ Object


48
49
50
# File 'lib/mini_i18n.rb', line 48

def separator=(new_separator)
  @@separator = new_separator || DEFAULT_SEPARATOR
end

.translate(key, options = {}) ⇒ Object Also known as: t


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mini_i18n.rb', line 72

def translate(key, options = {})
  return if key.empty? || translations.empty?

  return multiple_translate(key, options) if key.is_a?(Array)
  return multiple_locales(key, options) if options[:locale].is_a?(Array)

  _locale = available_locale?(options[:locale]) || locale
  scope = options[:scope]

  keys = [_locale.to_s]
  keys << scope.to_s.split(separator) if scope
  keys << key.to_s.split(separator)
  keys = keys.flatten

  result = lookup(*keys)

  result = with_fallbacks(result, keys)
  result = with_pluralization(result, options, _locale)
  result = with_interpolation(result, options)

  result || options[:default]
end

.translationsObject


32
33
34
# File 'lib/mini_i18n.rb', line 32

def translations
  @@translations ||= {}
end