Module: Stringex::Localization

Includes:
DefaultConversions
Defined in:
lib/stringex/localization.rb,
lib/stringex/localization/converter.rb,
lib/stringex/localization/backend/base.rb,
lib/stringex/localization/backend/i18n.rb,
lib/stringex/localization/backend/internal.rb,
lib/stringex/localization/default_conversions.rb,
lib/stringex/localization/conversion_expressions.rb

Defined Under Namespace

Modules: Backend, ConversionExpressions, DefaultConversions Classes: Converter

Constant Summary

Constants included from DefaultConversions

DefaultConversions::CHARACTERS, DefaultConversions::CURRENCIES, DefaultConversions::CURRENCIES_COMPLEX, DefaultConversions::CURRENCIES_SIMPLE, DefaultConversions::HTML_ENTITIES, DefaultConversions::TRANSLITERATIONS, DefaultConversions::VULGAR_FRACTIONS

Class Method Summary collapse

Class Method Details

.backendObject


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

def backend
  @backend ||= i18n_present? ? Backend::I18n : Backend::Internal
end

.backend=(sym_or_class) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/stringex/localization.rb', line 18

def backend=(sym_or_class)
  if sym_or_class.is_a?(Symbol)
    @backend = case sym_or_class
    when :internal
      Backend::Internal
    when :i18n
      ensure_i18n!
      Backend::I18n
    else
      raise "Invalid backend :#{sym_or_class}"
    end
  else
    @backend = sym_or_class
  end
end

.convert(string, options = {}, &block) ⇒ Object


84
85
86
87
88
89
# File 'lib/stringex/localization.rb', line 84

def convert(string, options = {}, &block)
  converter = Converter.new(string, options)
  converter.instance_exec &block
  converter.smart_strip!
  converter.string
end

.default_localeObject


62
63
64
# File 'lib/stringex/localization.rb', line 62

def default_locale
  backend.default_locale
end

.default_locale=(new_locale) ⇒ Object


66
67
68
# File 'lib/stringex/localization.rb', line 66

def default_locale=(new_locale)
  backend.default_locale = new_locale
end

.localeObject


54
55
56
# File 'lib/stringex/localization.rb', line 54

def locale
  backend.locale
end

.locale=(new_locale) ⇒ Object


58
59
60
# File 'lib/stringex/localization.rb', line 58

def locale=(new_locale)
  backend.locale = new_locale
end

.reset!Object


79
80
81
82
# File 'lib/stringex/localization.rb', line 79

def reset!
  backend.reset!
  @backend = nil
end

.store_translations(locale, scope, data) ⇒ Object


34
35
36
# File 'lib/stringex/localization.rb', line 34

def store_translations(locale, scope, data)
  backend.store_translations(locale, scope, data)
end

.translate(scope, key, options = {}) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stringex/localization.rb', line 38

def translate(scope, key, options = {})
  return if key == "." # I18n doesn't support dots as translation keys so we don't either

  locale = options[:locale] || self.locale

  translation = initial_translation(scope, key, locale)

  return translation unless translation.nil?

  if locale != default_locale
    translate scope, key, options.merge(:locale => default_locale)
  else
    default_conversion(scope, key) || options[:default]
  end
end

.with_default_locale(&block) ⇒ Object


75
76
77
# File 'lib/stringex/localization.rb', line 75

def with_default_locale(&block)
  with_locale default_locale, &block
end

.with_locale(new_locale, &block) ⇒ Object


70
71
72
73
# File 'lib/stringex/localization.rb', line 70

def with_locale(new_locale, &block)
  new_locale = default_locale if new_locale == :default
  backend.with_locale new_locale, &block
end