module Voltron::Translate

Constants

LOG_COLOR
VERSION

Public Instance Methods

_(locale = I18n.locale, **args) click to toggle source
# File lib/voltron/translate.rb, line 17
def _(locale = I18n.locale, **args)
  return (self % args) if !Voltron.config.translate.enabled? || self.blank?

  begin
    raise 'Locale can only contain the characters A-Z, and _' unless locale.to_s =~ /^[A-Z_]+$/i

    # If app is running in one of the environments where translations can be created
    if Voltron.config.translate.buildable?
      Array.wrap(Voltron.config.translate.locales).compact.each { |locale| translator(locale).write self }
    end

    # Translate the text and return it
    translator(locale).translate self, **args
  rescue => e
    # If any errors occurred, log the error and just return the default interpolated text
    Voltron.log e.message.to_s + " (Original Translation Text: #{self})", 'Translate', ::Voltron::Translate::LOG_COLOR
    self % args
  end
end
translator(locale = I18n.locale) click to toggle source
# File lib/voltron/translate.rb, line 37
def translator(locale = I18n.locale)
  @translators ||= {}
  @translators[locale.to_s] ||= Translator.new(locale)
end