Class: I18nYamlEditor::Store
- Inherits:
-
Object
- Object
- I18nYamlEditor::Store
- Includes:
- Transformation
- Defined in:
- lib/i18n_yaml_editor/store.rb
Instance Attribute Summary collapse
-
#categories ⇒ Object
Returns the value of attribute categories.
-
#keys ⇒ Object
Returns the value of attribute keys.
-
#locales ⇒ Object
Returns the value of attribute locales.
-
#translations ⇒ Object
Returns the value of attribute translations.
Instance Method Summary collapse
- #add_key(key) ⇒ Object
- #add_locale(locale) ⇒ Object
- #add_translation(translation) ⇒ Object
- #create_missing_keys ⇒ Object
- #filter_keys(options = {}) ⇒ Object
- #from_yaml(yaml, file = nil) ⇒ Object
-
#initialize ⇒ Store
constructor
A new instance of Store.
- #to_yaml ⇒ Object
Methods included from Transformation
Constructor Details
#initialize ⇒ Store
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
#categories ⇒ Object
Returns the value of attribute categories.
17 18 19 |
# File 'lib/i18n_yaml_editor/store.rb', line 17 def categories @categories end |
#keys ⇒ Object
Returns the value of attribute keys.
17 18 19 |
# File 'lib/i18n_yaml_editor/store.rb', line 17 def keys @keys end |
#locales ⇒ Object
Returns the value of attribute locales.
17 18 19 |
# File 'lib/i18n_yaml_editor/store.rb', line 17 def locales @locales end |
#translations ⇒ Object
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] = "#{translation.name} detected in #{translation.file} and #{existing.file}" raise DuplicateTranslationError.new() 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_keys ⇒ Object
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 ={} filters = [] if .has_key?(:key) filters << lambda {|k| k.name =~ [:key]} end if .has_key?(:complete) filters << lambda {|k| k.complete? == [:complete]} end if .has_key?(:empty) filters << lambda {|k| k.empty? == [:empty]} end if .has_key?(:text) filters << lambda {|k| k.translations.any? {|t| t.text =~ [: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_yaml ⇒ Object
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 |