module Glossync

Include into ActiveRecords to obtain some nice I18n features

Constants

DEFAULT_CONFIG
METHOD_PATTERN

methods that match this pattern will be responded to in method_missing()

VERSION

Attributes

options[R]

A hash which stores all of Glossync's global options.

tl_store[W]

Public Class Methods

append_features(rcvr) click to toggle source

called when a module is included, add methods to the base class

Calls superclass method
# File lib/glossync.rb, line 30
def append_features(rcvr)
  super

  # attr_writer for @glossync
  rcvr.define_singleton_method(:has_translations_for) do |*keys|
    @glossync =
      keys.map(&:to_sym).reject { |k| glossync.include?(k) }.map do |k|
        # overwrite the reader method for the specified key to just return
        # the translation.
        rcvr.define_method(k) do
          tl(k)
        end

        # overwrite the writer method for the specified key to update the
        # translation for the key as well as update the key
        rcvr.define_method(:"#{k}=") do |val|
          tl_update(k, val)
          defined?(super) ? super(val) : val
        end

        k
      end + glossync
  end

  # attr_reader for @glossync
  rcvr.define_singleton_method(:glossync) do
    @glossync || []
  end
end
config(&block) click to toggle source

If called with a block, executes the block setting the argument to Glossync, else returns the options hash

# File lib/glossync/config.rb, line 33
def config(&block)
  @options ||= DEFAULT_CONFIG.dup
  block_given? ? block.call(Glossync) : @options
end
tl_store() click to toggle source
# File lib/glossync.rb, line 21
def tl_store
  return @tl_store unless @tl_store.nil?

  Translation
rescue NameError
  raise(Glossync::NoTranslationStore)
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source

If you add _translated to the end of a glossync field, call tl_key on that field automatically

Calls superclass method
# File lib/glossync.rb, line 74
def method_missing(method, *args, &block)
  if respond_to_missing?(method)
    tl(method.to_s.gsub(METHOD_PATTERN, ''))
  else
    super
  end
end
respond_to_missing?(method, include_private = false) click to toggle source

Determines is a missing method name can be handled

Calls superclass method
# File lib/glossync.rb, line 83
def respond_to_missing?(method, include_private = false)
  (METHOD_PATTERN.match?(method) &&
    self.class
        .glossync
        .include?(method.to_s.gsub(METHOD_PATTERN, '').to_sym)) ||
    super
end
tl(field) click to toggle source

Call I18n.translate for the key corresponding to field

@example QuizAnswer.first.tl(:answer) => “True”

@param field the field to translate @return [String] the translated string

# File lib/glossync.rb, line 114
def tl(field)
  tl = catch(:exception) do
    I18n.translate(tl_key(field), throw: true)
  end

  if tl.is_a?(I18n::MissingTranslation)
    tlr = Glossync.missing_translation_handler
                  .handle(tl, self, field, I18n.locale)

    tl_update(field, tlr) if Glossync.options[:write_fallback_result]

    tlr
  else
    tl
  end
end
tl_key(field) click to toggle source

Creates a key for the translation of a field, suitable for I18n.t()

The key consists of the name of the model downcased, the id of the model, and the name of the field translated, joined with a '.'

@example QuizAnswer.first.tl_key(:answer) => “quizanswer.1.answer”

@param field the field to generate a key for @return [String] a key

# File lib/glossync.rb, line 100
def tl_key(field)
  [
    self.class.name.split('::').last.downcase,
    id,
    field.to_s
  ].join('.')
end
tl_update(field, value, locale = nil) click to toggle source

Update the translation associated with a field and locale

This method creates a new Translation if no pre-existing translation is found

@param field the field to update the translation of @param value the new translation @param locale the locale of the new translation, if none is specified, the

default locale will be used

@return [Boolean] wether or not an update was performed

# File lib/glossync.rb, line 141
def tl_update(field, value, locale = nil)
  locale ||= I18n.locale
  tls = Glossync.tl_store

  h = { locale: locale, key: tl_key(field) }

  if tls.exists?(h)
    tl = tls.find_by(h)
    return false if tl.value == value

    tl.update(value: value)
  else
    tls.create(h.merge(value: value))
  end

  I18n.backend.reload! if Glossync.options[:reload_backend]

  true
end
update(hash) click to toggle source

This sticks some code in front of update that also updates any translations that were modified

Calls superclass method
# File lib/glossync.rb, line 63
def update(hash)
  hash.to_h.map { |k, v| [k, v] }.to_h # really convert it to a hash
      .transform_keys(&:to_sym)
      .select { |k, _| self.class.glossync.include?(k) }
      .each { |k, v| tl_update(k, v) }

  super if defined?(super)
end