Sha256: 68d9e02a41ffda8add344f53628956118768e3b537532b21083de7fd2a7fab9a

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

class Hayabusa
  #Translates a given key for a given object.
  #===Examples
  # print _hb.trans(obj, :title) #=> "Trala"
  def trans(obj, key, args = {})
    args[:locale] = self.trans_locale if !args[:locale]
    trans_val = @translations.get(obj, key, args).to_s
    trans_val = @events.call(:trans_no_str, {:obj => obj, :key => key, :args => args}) if trans_val.length <= 0
    return trans_val
  end
  
  #Returns the locale for the current thread.
  def trans_locale(args = {})
    if args.is_a?(Hash) and args[:locale]
      return args[:locale]
    elsif _session and _session[:locale]
      return _session[:locale]
    elsif _httpsession and _httpsession.data[:locale]
      return _httpsession.data[:locale]
    elsif Thread.current[:locale]
      return Thread.current[:locale]
    elsif @config[:locale_default]
      return @config[:locale_default]
    end
    
    raise "Could not figure out locale."
  end
  
  #Sets new translations for the given object.
  #===Examples
  # _hb.trans_set(obj, {:title => "Trala"})
  def trans_set(obj, values, args = {})
    args[:locale] = self.trans_locale if !args[:locale]
    @translations.set(obj, values, args)
  end
  
  #Deletes all translations for the given object.
  #===Examples
  # _hb.trans_del(obj)
  def trans_del(obj)
    @translations.delete(obj)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hayabusa-0.0.3 lib/hayabusa_ext/translations.rb
hayabusa-0.0.2 lib/hayabusa_ext/translations.rb
hayabusa-0.0.1 lib/hayabusa_ext/translations.rb