Class: TranslationsSync

Inherits:
Object
  • Object
show all
Defined in:
lib/translations_sync.rb

Constant Summary collapse

EXCLUDE_LIST =
''
TAIL =
'=TODO'
PKORDER =
{'zero' => 0, 'one' => 1, 'two' => 2, 'few' => 3, 'many' => 5, 'other' => 9}
PKLIST =
PKORDER.keys
RE =
Regexp.new ' \d(' + PKORDER.keys.join('|') + '):'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TranslationsSync

class << self



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/translations_sync.rb', line 58

def initialize(*args)
  params = args.pop if args.last.is_a? Hash
  params = (params || {}).with_indifferent_access
  unless args.size == 0
    list, exclude, source = agrs
    puts 'Deprecation warning: a Hash should be used to pass paramteters'
  end
  list ||= params[:list]
  exclude ||= params[:exclude]
  source ||= params[:source]
  ignore = params[:ignore]
  @full_list = list ? list.split(',').map(&:to_sym) : I18n.available_locales
  @full_list = @full_list.uniq
  exclude = (exclude || EXCLUDE_LIST).split(',').map(&:to_sym)
  @list = @full_list - exclude
  rules_key = %w[i18n.plural.rule pluralize].detect do |rule|
    @list.detect do |lang|
      locale_pluralize = I18n.backend.send(:lookup, lang, rule) and
        locale_pluralize.respond_to?(:call)
    end
  end
  @pluralize_keys = @list.inject({}) do |acc, lang|
    acc[lang] = if rules_key and
        locale_pluralize = I18n.backend.send(:lookup, lang, rules_key) and
        locale_pluralize.respond_to?(:call)
      ((0..100).map do |n|
        locale_pluralize.call n
      end << :other).uniq
    else
      [:one, :other]
    end
    acc
  end

  translations_path_re = Regexp.new "#{translations_dir}(\\/[a-z]{2}(?:-[A-Z]{2})?)?\\/[-_/0-9a-zA-Z]+(?:(_|\\.)[a-z]{2}(?:-[A-Z]{2})?)?\\.(?:yml|rb)\\Z"
  I18n.load_path.find do  |path|
    path.match translations_path_re
  end
  @prefix = $1 or @separator = $2

  if source
    translations_path_re = Regexp.new "#{translations_dir}(\\/[a-z]{2}(?:-[A-Z]{2})?)?\\/#{Regexp.escape source}(?:(_|\\.)[a-z]{2}(?:-[A-Z]{2})?)?\\.(?:yml|rb)\\Z"
    I18n.load_path.reject! do |path|
      path !~ translations_path_re
    end
  end

	puts ignore.inspect
  if ignore
    translations_path_re = Regexp.new "#{translations_dir}#{'\\/[^/]+' if @prefix}\\/#{Regexp.escape ignore}"
    I18n.load_path.reject! do |path|
      puts "#{path} #{(path !~ translations_path_re).inspect}"
      path =~ translations_path_re
    end
  end

  I18n.reload!
  I18n.backend.send(:init_translations)
  translations = I18n.backend.send :translations
  @flat = {}
  @list.each do |lang|
    flatten_keys(lang, translations[lang] || {}, @flat)
  end
  @flat.delete(rules_key.split('.').map(&:to_sym)) if rules_key # we do not need this proc if it exists
  transliterate = [:i18n, :transliterate]
  @flat.keys.select do |key|
    @flat.delete(key) if key[0..1] == transliterate
  end
end

Instance Attribute Details

#flatObject (readonly)

Returns the value of attribute flat.



26
27
28
# File 'lib/translations_sync.rb', line 26

def flat
  @flat
end

#listObject

Returns the value of attribute list.



25
26
27
# File 'lib/translations_sync.rb', line 25

def list
  @list
end

#translationsObject

Returns the value of attribute translations.



25
26
27
# File 'lib/translations_sync.rb', line 25

def translations
  @translations
end

Class Method Details

.to_yaml(hash) ⇒ Object



34
35
36
# File 'lib/translations_sync.rb', line 34

def to_yaml(hash)
  translate_keys(hash).ya2yaml.gsub(RE, ' \1:')
end

Instance Method Details

#filename_for(name, lang = nil) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/translations_sync.rb', line 238

def filename_for(name, lang = nil)
  if lang
    if @prefix
      "#{translations_dir}/#{lang}/#{name}.yml"
    else
      "#{translations_dir}/#{name}#{@separator}#{lang}.yml"
    end
  else
    "#{translations_dir}/#{name}.yml"
  end
end

#locales_with_missingObject

initialize



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/translations_sync.rb', line 128

def locales_with_missing
  unless @locales_with_missing
    size = list.size
    @locales_with_missing = []
    flat.each_pair do |key, val|
      v = val.reject{|k, v| v.nil?}
      if v.size < size
        (@locales_with_missing += list - v.keys).uniq!
        break if @locales_with_missing.size == size
      end
    end
  end
  @locales_with_missing
end

#locales_with_singlesObject



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/translations_sync.rb', line 159

def locales_with_singles
  unless @locales_with_singles
    size = list.size
    @locales_with_singles = []
    flat.each_pair do |key, val|
      if val.size < size
        (@locales_with_singles += val.keys).uniq! if val.size == 1
        break if @locales_with_singles.size == size
      end
    end
  end
  @locales_with_singles
end

#missingObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/translations_sync.rb', line 143

def missing
  unless @missing
    @missing = {}
    locales_with_missing.each do |lang|
      @missing[lang] = {}
    end
    flat.each_pair do |key, val|
      (locales_with_missing - val.keys).each do |lang|
        push_to_hash @missing[lang], lang, key, val, :missing
      end
    end
    @missing.stringify_keys!
  end
  @missing
end

#move(key, destination) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/translations_sync.rb', line 188

def move(key, destination)
  key = key.split('.').map(&:to_sym)
  key_length = key.length
  return false if key_length < 1
  destination ||= ''
  destination = destination.split('.').map(&:to_sym)
  destination << key.last if destination.size == 0
  result = false
  flat.each_pair do |array, val|
    if array[0, key_length] == key
      array[0, key_length] = destination
      result = true
    end
  end
  @moved = nil
  result
end

#movedObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/translations_sync.rb', line 206

def moved
  unless @moved
    @moved = {}
    @list.each do |lang|
      @moved[lang] = {}
    end
    flat.each_pair do |key, val|
      val.keys.each do |lang|
        push_to_hash @moved[lang], lang, key, val, :moved
      end
    end
    @moved.keys.each do |key|
      @moved.delete(key) if @moved[key].size == 0
    end
    @moved.stringify_keys!
  end
  @moved
end

#remove(key) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/translations_sync.rb', line 225

def remove(key)
  key = key.split('.').map(&:to_sym)
  key_length = key.length
  result = false
  flat.reject! do |array, val|
    r = array[0, key_length] == key
    result ||= r
    r
  end
  @moved = nil
  result
end

#singlesObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/translations_sync.rb', line 173

def singles
  unless @singles
    @singles = {}
    locales_with_singles.each do |lang|
      @singles[lang] = {}
    end
    flat.each_pair do |key, val|
      lang = val.keys.first
      push_to_hash @singles[lang], lang, key, val, :singles if val.size == 1
    end
    @singles.stringify_keys!
  end
  @singles
end