Module: RSence::Plugins::Localization

Included in:
GUIPlugin__
Defined in:
lib/rsence/plugins/plugin_localization.rb

Instance Method Summary collapse

Instance Method Details

#deep_merge_hash(src, tgt = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rsence/plugins/plugin_localization.rb', line 29

def deep_merge_hash( src, tgt={} )
  src.each do |key,val|
    if val.class == Hash
      tgt[key] = {} unless tgt.has_key? key and tgt[key].class == Hash
      deep_merge_hash( src[key], tgt[key] )
    else
      if [String,Array].include? val.class
        dupval = val.dup
      else
        dupval = val
      end
      tgt[key] = dupval unless tgt.has_key? key and tgt[key].class == val.class
    end
  end
end

#fill_strs_raw_path(hash, path, val) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/rsence/plugins/plugin_localization.rb', line 7

def fill_strs_raw_path( hash, path, val )
  key = path.shift
  if path.empty?
    hash[key] = val
  else
    hash[key] = {} unless hash.has_key? key
    fill_strs_raw_path( hash[key], path, val )
  end
end

#gui_params(msg) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/rsence/plugins/plugin_localization.rb', line 93

def gui_params( msg )
  params = {}
  if @strings
    params[:strings] = strings()
  end
  if @localized_strings
    params[:localized] = localized_strings( msg )
  end
  params
end

#initObject



88
89
90
91
92
# File 'lib/rsence/plugins/plugin_localization.rb', line 88

def init
  super
  init_strings
  init_localized_strings
end

#init_localized_stringsObject



81
82
83
84
85
86
87
# File 'lib/rsence/plugins/plugin_localization.rb', line 81

def init_localized_strings
  [ 'localized.yaml', 'gui/localized.yaml' ].each do |yaml_name|
    strs = read_localized_strings( yaml_name )
    @localized_strings = strs
    break if strs
  end
end

#init_stringsObject



74
75
76
77
78
79
80
# File 'lib/rsence/plugins/plugin_localization.rb', line 74

def init_strings
  [ 'strings.yaml', 'gui/strings.yaml' ].each do |yaml_name|
    strs = read_strings( yaml_name )
    @strings = strs
    break if strs
  end
end

#localized_strings(msg, string_path = nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rsence/plugins/plugin_localization.rb', line 127

def localized_strings( msg, string_path=nil )
  return nil unless @localized_strings
  if msg.class == String
    lang = msg
  else
    lang = msg.lang
  end
  default_lang = RSence.config[:lang]
  if @localized_strings.has_key?( lang )
    search_obj = @localized_strings[lang]
  elsif lang.length == 5 and lang[2] == '-' and @localized_strings.has_key?( lang[0..1] )
    warn "Warning in #{bundle_path} #localized_strings: No '#{lang}' language, using variant '#{lang[0..1]}' instead" if RSence.args[:verbose]
    lang = lang[0..1]
    search_obj = @localized_strings[lang]
  elsif @localized_strings.has_key?( default_lang )
    warn "Warning in #{bundle_path} #localized_strings: No '#{lang}' language, using default '#{default_lang}' instead" if RSence.args[:verbose]
    search_obj = @localized_strings[lang]
  else
    warn "Error in #{bundle_path} #localized_strings: No '#{lang}' language, no default '#{default_lang}' either"
    return nil
  end
  return search_obj if string_path.nil?
  strings_params_search( string_path, search_obj )
end

#parse_strs_raw(hash_out, hash_node_in, path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rsence/plugins/plugin_localization.rb', line 16

def parse_strs_raw( hash_out, hash_node_in, path )
  hash_node_in.each do |key,val|
    if val.class == Hash
      child_path = path.dup
      child_path.push( key )
      parse_strs_raw( hash_out, val, child_path )
    else
      out_path = path.dup
      out_path.unshift( key )
      fill_strs_raw_path( hash_out, out_path, val )
    end
  end
end

#process_localized_strings(str_hash) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rsence/plugins/plugin_localization.rb', line 44

def process_localized_strings( str_hash )
  langs = str_hash.keys.sort { |a,b| (a > b) ? ( ( a.length > b.length ) ? 1 : -1 ) : -1 }
  default_lang = RSence.config[:lang]
  if langs.include? default_lang
    default_hash = deep_merge_hash( str_hash[default_lang] )
  else
    warn "Warning in #{bundle_path} #process_localized_strings: No default language in localized, using '#{langs.first}' instead"
    default_hash = deep_merge_hash( str_hash[langs.first] )
  end
  langs.each do |lang|
    # create a new copy of the default hash
    base_hash = deep_merge_hash( default_hash )
    if lang.length == 5 and langs.include? lang[0..1]
      # create a base of extended language from the base languge (eg. 'en' for 'en-US')
      base_hash = deep_merge_hash( str_hash[lang[0..1]], base_hash )
    end
    # finally complete the language hash with the base
    deep_merge_hash( base_hash, str_hash[lang] )
  end
end

#read_localized_strings(fn = 'localized.yaml') ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/rsence/plugins/plugin_localization.rb', line 64

def read_localized_strings( fn='localized.yaml' )
  strs_raw = read_strings( fn )
  return nil unless strs_raw
  str_hash = {}
  if strs_raw
    parse_strs_raw( str_hash, strs_raw, [] )
  end
  process_localized_strings( str_hash )
  str_hash
end

#read_strings(fn = 'strings.yaml') ⇒ Object



4
5
6
# File 'lib/rsence/plugins/plugin_localization.rb', line 4

def read_strings( fn='strings.yaml' )
  yaml_read( fn )
end

#strings(string_path = nil) ⇒ Object



122
123
124
125
126
# File 'lib/rsence/plugins/plugin_localization.rb', line 122

def strings( string_path=nil )
  return nil unless @strings
  return @strings if string_path.nil?
  strings_params_search( arr_path.dup, @strings )
end

#strings_params_search(arr_path, params) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rsence/plugins/plugin_localization.rb', line 103

def strings_params_search( arr_path, params )
  if arr_path.class == String
    arr_path = arr_path.split('.')
  elsif arr_path.class != Array
    warn "Warning in #{bundle_path} #strings_params_search: Invalid string_path class #{arr_path.class.to_s}"
    return nil
  end
  item = arr_path.shift
  if params.class == Hash
    if params.has_key?( item )
      if arr_path.size == 0
        return params[item]
      else
        return strings_params_search( arr_path, params[ item ] )
      end
    end
  end
  ''
end