Module: I18n::Translate

Defined in:
lib/i18n/translator.rb,
lib/i18n/translate.rb,
lib/i18n/processor.rb

Overview

vi: fenc=utf-8:expandtab:ts=2:sw=2:sts=2

@author: Petr Kovar <[email protected]>

Defined Under Namespace

Modules: Processor Classes: Translate, Translator

Constant Summary collapse

FLAGS =
%w(ok incomplete changed untranslated obsolete)
FORMATS =

FORMATS = %w(yml rb po pot ts properties) # the first one is preferred if :format => auto function I18n::Translate::Processor.init will register known formats

%w(
RESERVED_WORDS =

the first one is preferred if :format => auto

%w(comment extracted_comment reference file line default old_default fuzzy flag translation old t)

Class Method Summary collapse

Class Method Details

.create_locale(lang, opts = {}) ⇒ Object

create new locale and returns I18n::Translate::Translate object



177
178
179
180
181
182
# File 'lib/i18n/translate.rb', line 177

def self.create_locale(lang, opts={})
  tr = I18n::Translate::Translate.new(lang, opts)
  tr.assign(tr.merge)
  tr.export!
  tr
end

.delete(key, hash, separator = ".") ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/i18n/translate.rb', line 112

def self.delete(key, hash, separator=".")
  path = key.split(separator)
  set(key, nil, hash, separator)
  i = path.size - 1
  while i >= 0
    k = path[0..i].join(separator)
    trg = find(k, hash, separator)
    if trg and trg.kind_of?(Hash) and trg.empty?
      set(k, nil, hash, separator)
    end
    i -= 1
  end
end

.find(key, hash, separator = ".") ⇒ Object

returns what is stored under key



88
89
90
91
92
93
94
95
96
# File 'lib/i18n/translate.rb', line 88

def self.find(key, hash, separator=".")
  h = hash
  path = key.to_s.split(separator)
  path.each do |key|
    h = h[key]
    return nil if h.nil?
  end
  h
end

.hash_to_keys(hash, separator = ".", prefix = "") ⇒ Object

returns flat array of all keys e.g. [“system.message.ok”, “system.message.error”, …]



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/i18n/translate.rb', line 74

def self.hash_to_keys(hash, separator=".", prefix="")
  res = []
  hash.keys.each do |key|
    str = prefix.empty? ? key : "#{prefix}#{separator}#{key}"
    enhanced = I18n::Translate.is_enhanced?(hash[key])
    if hash[key].kind_of?(Hash) and (not enhanced)
      str = hash_to_keys( hash[key], separator, str )
    end
    res << str
  end if hash
  res.flatten
end

.is_enhanced?(hash) ⇒ Boolean

checks if all keys are reserved keywords

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/i18n/translate.rb', line 65

def self.is_enhanced?(hash)
  return false unless hash.kind_of?(Hash)
  hash.keys.each do |key|
    return false unless I18n::Translate::RESERVED_WORDS.include?(key)
  end
  true
end

.read_config(filename) ⇒ Object

read configuration file config format is ruby file which returns hash



60
61
62
# File 'lib/i18n/translate.rb', line 60

def self.read_config(filename)
  eval File.read(filename)
end

.scan(opts = Translate::DEFAULT_OPTIONS, &block) ⇒ Object

scans :locale_dir for files with valid formats and returns list of files with locales. If block is given then it creates Translate object for each entry and pass it to the block



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/i18n/translate.rb', line 129

def self.scan(opts=Translate::DEFAULT_OPTIONS, &block)
  o = Translate::DEFAULT_OPTIONS.merge(opts)
  o[:exclude] ||= []

  entries = []
  if o[:deep] == true
    Find.find(o[:locale_dir]) {|e| entries << e}
  else
    entries = Dir[File.join(o[:locale_dir], "*")]
  end

  locales = []
  entries.each do |entry|
    locale, format = Translate.valid_file?(entry, o[:format])
    if (not format) or (locale == o[:default])
      puts "#{entry}...skipping" if o[:verbose]
      next
    end

    # skip if not desired locale
    if o[:locale] and (o[:locale] != "auto") and (o[:locale] != locale)
      puts "#{entry}...skipping" if o[:verbose]
      next 
    end

    exclude = false
    o[:exclude].each do |ex|
      if entry =~ %r|#{ex}|
        exclude = true
        break
      end
    end
    puts "#{entry}...excluded" if exclude and o[:verbose]
    next if exclude

    locales << entry 
    dir = File.dirname(entry)

    if block
      yield Translate.new(locale, o.merge({:format => format, :locale_dir => dir, :default => o[:default]}))
    end

  end

  locales
end

.set(key, value, hash, separator = ".") ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/i18n/translate.rb', line 98

def self.set(key, value, hash, separator=".")
  h = hash
  path = key.to_s.split(separator)
  path[0..-2].each do |chunk|
    h[chunk] ||= {}
    h = h[chunk]
  end
  unless value
    h.delete(path[-1])
  else
    h[path[-1]] = value
  end
end