Class: I18nYamlEditor::Store

Inherits:
Object
  • Object
show all
Includes:
Transformation
Defined in:
lib/i18n_yaml_editor/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transformation

flatten_hash, nest_hash

Constructor Details

#initializeStore

Returns a new instance of Store.



19
20
21
22
23
24
# File 'lib/i18n_yaml_editor/store.rb', line 19

def initialize
  @categories = {}
  @keys = {}
  @translations = {}
  @locales = Set.new
end

Instance Attribute Details

#categoriesObject

Returns the value of attribute categories.



17
18
19
# File 'lib/i18n_yaml_editor/store.rb', line 17

def categories
  @categories
end

#keysObject

Returns the value of attribute keys.



17
18
19
# File 'lib/i18n_yaml_editor/store.rb', line 17

def keys
  @keys
end

#localesObject

Returns the value of attribute locales.



17
18
19
# File 'lib/i18n_yaml_editor/store.rb', line 17

def locales
  @locales
end

#translationsObject

Returns the value of attribute translations.



17
18
19
# File 'lib/i18n_yaml_editor/store.rb', line 17

def translations
  @translations
end

Instance Method Details

#add_key(key) ⇒ Object



43
44
45
# File 'lib/i18n_yaml_editor/store.rb', line 43

def add_key key
  self.keys[key.name] = key
end

#add_locale(locale) ⇒ Object



47
48
49
# File 'lib/i18n_yaml_editor/store.rb', line 47

def add_locale locale
  self.locales.add(locale)
end

#add_translation(translation) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/i18n_yaml_editor/store.rb', line 26

def add_translation translation
  if existing = self.translations[translation.name]
    message = "#{translation.name} detected in #{translation.file} and #{existing.file}"
    raise DuplicateTranslationError.new(message)
  end

  self.translations[translation.name] = translation

  add_locale(translation.locale)

  key = (self.keys[translation.key] ||= Key.new(name: translation.key))
  key.add_translation(translation)

  category = (self.categories[key.category] ||= Category.new(name: key.category))
  category.add_key(key)
end

#create_missing_keysObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/i18n_yaml_editor/store.rb', line 73

def create_missing_keys
  self.keys.each {|name, key|
    missing_locales = self.locales - key.translations.map(&:locale)
    missing_locales.each {|locale|
      translation = key.translations.first

      # this just replaces the locale part of the file name. should
      # be possible to do in a simpler way. gsub, baby.
      path = Pathname.new(translation.file)
      dirs, file = path.split
      file = file.to_s.split(".")
      file[-2] = locale
      file = file.join(".")
      path = dirs.join(file).to_s

      new_translation = Translation.new(name: "#{locale}.#{key.name}", file: path)
      add_translation(new_translation)
    }
  }
end

#filter_keys(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/i18n_yaml_editor/store.rb', line 51

def filter_keys options={}
  filters = []
  if options.has_key?(:key)
    filters << lambda {|k| k.name =~ options[:key]} 
  end
  if options.has_key?(:complete)
    filters << lambda {|k| k.complete? == options[:complete]}
  end
  if options.has_key?(:empty)
    filters << lambda {|k| k.empty? == options[:empty]}
  end
  if options.has_key?(:text)
    filters << lambda {|k|
      k.translations.any? {|t| t.text =~ options[:text]}
    }
  end

  self.keys.select {|name, key|
    filters.all? {|filter| filter.call(key)}
  }
end

#from_yaml(yaml, file = nil) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/i18n_yaml_editor/store.rb', line 94

def from_yaml yaml, file=nil
  translations = flatten_hash(yaml)
  translations.each {|name, text|
    translation = Translation.new(name: name, text: text, file: file)
    add_translation(translation)
  }
end

#to_yamlObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/i18n_yaml_editor/store.rb', line 102

def to_yaml
  result = {}
  files = self.translations.values.group_by(&:file)
  files.each {|file, translations|
    file_result = {}
    translations.each {|translation|
      file_result[translation.name] = translation.text
    }
    result[file] = nest_hash(file_result)
  }
  result
end