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 ||= defined?(I18n) ? Backend::I18n : Backend::Internal
end

.backend=(sym_or_class) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# 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 then Backend::Internal
      when :i18n then Backend::I18n
      else raise "Invalid backend :#{sym_or_class}"
    end
  else
    @backend = sym_or_class
  end
end

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



80
81
82
83
84
85
# File 'lib/stringex/localization.rb', line 80

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

.default_localeObject



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

def default_locale
  backend.default_locale
end

.default_locale=(new_locale) ⇒ Object



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

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

.localeObject



50
51
52
# File 'lib/stringex/localization.rb', line 50

def locale
  backend.locale
end

.locale=(new_locale) ⇒ Object



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

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

.reset!Object



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

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

.store_translations(locale, scope, data) ⇒ Object



30
31
32
# File 'lib/stringex/localization.rb', line 30

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

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/stringex/localization.rb', line 34

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



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

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

.with_locale(new_locale, &block) ⇒ Object



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

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