Class: I18nYamlEditor::Store
- Inherits:
-
Object
- Object
- I18nYamlEditor::Store
- Defined in:
- lib/i18n_yaml_editor/store.rb
Overview
Store keeps all i18n data
Constant Summary
Constants included from Cast
Cast::CHECK, Cast::CONVERT, Cast::TYPES
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
Adds a key to this store.
-
#add_translation(translation) ⇒ Object
Adds a given translation to the store.
-
#create_missing_keys ⇒ Object
Creates all keys for each locale that doesn’t have all keys from all other locales.
-
#from_yaml(yaml, file = nil) ⇒ Object
Adds a translation for every entry in a given yaml hash.
-
#init_category(key) ⇒ Object
Generates a new category with the given key.
-
#init_key(translation) ⇒ Object
Generates a new key with the given translation.
-
#initialize ⇒ Store
constructor
A new instance of Store.
-
#to_yaml ⇒ Object
Returns a hash with the structure of a i18n.
Methods included from Update
Methods included from Cast
Methods included from Filter
Constructor Details
#initialize ⇒ Store
Returns a new instance of Store.
24 25 26 27 28 29 |
# File 'lib/i18n_yaml_editor/store.rb', line 24 def initialize @categories = {} @keys = {} @translations = {} @locales = Set.new end |
Instance Attribute Details
#categories ⇒ Object
Returns the value of attribute categories.
22 23 24 |
# File 'lib/i18n_yaml_editor/store.rb', line 22 def categories @categories end |
#keys ⇒ Object
Returns the value of attribute keys.
22 23 24 |
# File 'lib/i18n_yaml_editor/store.rb', line 22 def keys @keys end |
#locales ⇒ Object
Returns the value of attribute locales.
22 23 24 |
# File 'lib/i18n_yaml_editor/store.rb', line 22 def locales @locales end |
#translations ⇒ Object
Returns the value of attribute translations.
22 23 24 |
# File 'lib/i18n_yaml_editor/store.rb', line 22 def translations @translations end |
Instance Method Details
#add_key(key) ⇒ Object
Adds a key to this store
56 57 58 |
# File 'lib/i18n_yaml_editor/store.rb', line 56 def add_key(key) keys[key.name] = key end |
#add_translation(translation) ⇒ Object
Adds a given translation to the store
32 33 34 35 36 37 38 39 40 |
# File 'lib/i18n_yaml_editor/store.rb', line 32 def add_translation(translation) check_duplication! translation translations[translation.name] = translation locales.add(translation.locale) key = init_key(translation) init_category(key) end |
#create_missing_keys ⇒ Object
Creates all keys for each locale that doesn’t have all keys from all other locales
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/i18n_yaml_editor/store.rb', line 62 def create_missing_keys keys.each do |_name, key| missing_locales = locales - key.translations.map(&:locale) missing_locales.each do |locale| translation = key.translations.first name = "#{locale}.#{key.name}" path = translation_path locale, translation add_translation(Translation.new(name: name, file: path)) end end end |
#from_yaml(yaml, file = nil) ⇒ Object
Adds a translation for every entry in a given yaml hash
75 76 77 78 79 80 81 |
# File 'lib/i18n_yaml_editor/store.rb', line 75 def from_yaml(yaml, file = nil) translations = yaml.extend(YamlNormalizer::Ext::Namespaced).namespaced translations.each do |name, text| translation = Translation.new(name: name, text: text, file: file) add_translation(translation) end end |
#init_category(key) ⇒ Object
Generates a new category with the given key
50 51 52 53 |
# File 'lib/i18n_yaml_editor/store.rb', line 50 def init_category(key) category = (categories[key.category] ||= Category.new(name: key.category)) category.add_key(key) end |
#init_key(translation) ⇒ Object
Generates a new key with the given translation
43 44 45 46 47 |
# File 'lib/i18n_yaml_editor/store.rb', line 43 def init_key(translation) key = (keys[translation.key] ||= Key.new(name: translation.key)) key.add_translation(translation) key end |
#to_yaml ⇒ Object
Returns a hash with the structure of a i18n
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/i18n_yaml_editor/store.rb', line 84 def to_yaml result = {} files = translations.values.group_by(&:file) files.each do |file, translations| file_result = {} translations.each do |translation| file_result[translation.name] = translation.text end result[file] = file_result.extend(YamlNormalizer::Ext::Nested).nested end result end |