Module: LostInTranslations

Defined in:
lib/lost_in_translations.rb,
lib/lost_in_translations/base.rb,
lib/lost_in_translations/ruby.rb,
lib/lost_in_translations/config.rb,
lib/lost_in_translations/version.rb,
lib/lost_in_translations/active_record.rb,
lib/lost_in_translations/translator/base.rb

Defined Under Namespace

Modules: ActiveRecord, Base, Ruby, Translator Classes: Config

Constant Summary collapse

VERSION =
'1.5.0'.freeze

Class Method Summary collapse

Class Method Details

.assign_translation(*args) ⇒ Object



31
32
33
# File 'lib/lost_in_translations.rb', line 31

def self.assign_translation(*args)
  config.translator.assign_translation(*args)
end

.configObject



18
19
20
21
22
23
24
25
# File 'lib/lost_in_translations.rb', line 18

def self.config
  @config ||= Config.new(
    'translation_data',
    Translator::Base,
    nil,
    'force_locale'
  )
end

.configure {|config| ... } ⇒ Object

Yields:



74
75
76
# File 'lib/lost_in_translations.rb', line 74

def self.configure
  yield(config)
end

.define_dynamic_translation_method(object, method_name) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/lost_in_translations.rb', line 46

def self.define_dynamic_translation_method(object, method_name)
  object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{method_name}
      translate(:#{method_name}, I18n.locale)
    end
  RUBY
end

.define_particular_translation_methods(object, attribute) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/lost_in_translations.rb', line 54

def self.define_particular_translation_methods(object, attribute)
  I18n.available_locales.each do |locale|
    method_name = "#{locale.to_s.downcase.tr('-', '_')}_#{attribute}"

    define_translation_method(object, method_name, attribute, locale)
  end
end

.define_translation_method(object, method_name, attribute, locale) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lost_in_translations.rb', line 62

def self.define_translation_method(object, method_name, attribute, locale)
  object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{method_name}
      translate(:#{attribute}, :'#{locale}')
    end

    def #{method_name}=(value)
      assign_translation(:#{attribute}, value, :'#{locale}')
    end
  RUBY
end

.define_translation_methods(object, *fields) ⇒ Object



39
40
41
42
43
44
# File 'lib/lost_in_translations.rb', line 39

def self.define_translation_methods(object, *fields)
  fields.each do |field|
    define_dynamic_translation_method(object, field)
    define_particular_translation_methods(object, field)
  end
end

.included(base_class) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/lost_in_translations.rb', line 9

def self.included(base_class)
  if defined?(::ActiveRecord::Base) &&
     base_class.ancestors.include?(::ActiveRecord::Base)
    base_class.send(:include, LostInTranslations::ActiveRecord)
  else
    base_class.send(:include, Ruby)
  end
end

.infected_classesObject



78
79
80
# File 'lib/lost_in_translations.rb', line 78

def self.infected_classes
  @infected_classes ||= []
end

.reloadObject



82
83
84
# File 'lib/lost_in_translations.rb', line 82

def self.reload
  infected_classes.each(&:define_translation_methods)
end

.translate(*args) ⇒ Object



27
28
29
# File 'lib/lost_in_translations.rb', line 27

def self.translate(*args)
  config.translator.translate(*args)
end

.translation_data(object) ⇒ Object



35
36
37
# File 'lib/lost_in_translations.rb', line 35

def self.translation_data(object)
  config.translator.translation_data(object)
end