Module: Stringex::Localization

Includes:
DefaultConversions
Defined in:
lib/stringex/localization.rb,
lib/stringex/localization/converter.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



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

def backend
  @backend ||= defined?(I18n) ? Backend::I18n : Backend::Internal
end

.backend=(sym_or_class) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/stringex/localization.rb', line 17

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



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

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

.default_localeObject



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

def default_locale
  backend.default_locale
end

.default_locale=(new_locale) ⇒ Object



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

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

.localeObject



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

def locale
  backend.locale
end

.locale=(new_locale) ⇒ Object



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

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

.reset!Object



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

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

.store_translations(locale, scope, data) ⇒ Object



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

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

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



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

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



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

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

.with_locale(new_locale, &block) ⇒ Object



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

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