Sha256: fa686e8d82bebf302dbfa1f5963b1e660387dad5045ffa1c4da3654e40b3575a

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

require "set"

module Localite::Translate
  # 
  # translate a string 
  #
  # returns the translated string in the current locale.
  # If no translation is found try the base locale. 
  # If still no translation is found, return nil
  # 
  def translate(s, raise_mode)
    r = do_translate(locale, s)
    return r if r

    r = do_translate(base, s) if base != locale
    return r if r
    
    raise Missing, [locale, s] if raise_mode != :no_raise
  end

  private

  def do_translate(locale, s)
    scopes.each(s) do |scoped_string|
      tr = translate_via_i18n locale, scoped_string
      return tr if tr
    end

    record_missing locale, scopes.first(s)
    nil
  end
  
  def translate_via_i18n(locale, s)
    locale = base unless I18n.backend.available_locales.include?(locale)
    I18n.locale = locale
    I18n.translate(s, :raise => true)
  rescue I18n::MissingTranslationData
    nil
  end
  
  #
  # log a missing translation and raise an exception 
  def record_missing(locale, s)
    @missing_translations ||= Set.new
    entry = [ locale, s ]
    return if @missing_translations.include?(entry)
    @missing_translations << entry
    logger.warn "Missing translation: [#{locale}] #{s.inspect}"
  end
  
  public
  
  class Missing < RuntimeError
    attr :locale
    attr :string

    def initialize(opts)
      @locale, @string = *opts
    end

    def to_s
      "Missing translation: [#{locale}] #{string.inspect}"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
localite-0.3 lib/localite/translate.rb
localite-0.2.1 lib/localite/translate.rb
localite-0.2.0 lib/localite/translate.rb
localite-0.1.3 lib/localite/translate.rb