Class: Zena::Use::I18n::TranslationDict

Inherits:
Object
  • Object
show all
Includes:
RubyLess, Acts::Secure
Defined in:
lib/zena/use/i18n.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Acts::Secure

#secure_scope, #secure_write_scope, #visitor=

Methods included from Acts::Secure::SecureResult

#construct_id_map, #secure_result

Constructor Details

#initialize(node_id, static = nil) ⇒ TranslationDict

Returns a new instance of TranslationDict.



19
20
21
22
# File 'lib/zena/use/i18n.rb', line 19

def initialize(node_id, static = nil)
  @node_id = node_id
  @static  = static
end

Instance Attribute Details

#last_errorObject (readonly)

Returns the value of attribute last_error.



11
12
13
# File 'lib/zena/use/i18n.rb', line 11

def last_error
  @last_error
end

#node_idObject (readonly)

Returns the value of attribute node_id.



11
12
13
# File 'lib/zena/use/i18n.rb', line 11

def node_id
  @node_id
end

#staticObject (readonly)

Returns the value of attribute static.



11
12
13
# File 'lib/zena/use/i18n.rb', line 11

def static
  @static
end

Instance Method Details

#get(key, use_global = true) ⇒ Object



24
25
26
27
# File 'lib/zena/use/i18n.rb', line 24

def get(key, use_global = true)
  load!
  get_without_loading(key, use_global)
end

#get_without_loading(key, use_global = true) ⇒ Object



29
30
31
32
# File 'lib/zena/use/i18n.rb', line 29

def get_without_loading(key, use_global = true)
  # SECURITY: We consider all strings in dictionaries as SAFE.
  @dict[key] || (use_global && ApplicationController.send(:_, key))
end

#load!(text = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zena/use/i18n.rb', line 34

def load!(text = nil)
  unless text
    if @node_id
      if dict = secure(Node) { Node.find(:first, :conditions => {:id => @node_id}) }
        text = dict.prop['text']
      else
        @dict = {}
        error("missing 'dictionary'")
        return false
      end
    else
      text, ignore = ::Skin.text_from_fs_skin(@static[:brick_name], @static[:skin_name], @static[:path], :ext => 'yml')
      unless text
        @dict = {}
        error("missing 'dictionary'")
        return false
      end
    end
  end
  
  begin
    definitions = YAML::load(text)
    if translations = definitions['translations']
      if translations.kind_of?(Hash)
        # ok
        @dict = translations
      else
        return error("bad 'translations' content (should be a dictionary)")
      end
    else
      return error("missing 'translations' top-level key in dictionary")
    end
  rescue
    return error("invalid dictionary content #{dict.inspect}")
  end

  class << self
    alias get get_without_loading
  end

  true
end