Class: I18nYamlEditor::App
- Inherits:
-
Object
- Object
- I18nYamlEditor::App
- Defined in:
- lib/i18n_yaml_editor/app.rb
Overview
App provides I18n Yaml Editor’s top-level functionality:
* Starting I18n Yaml Editor
* Loading Translation files
* Saving Translation files
Instance Attribute Summary collapse
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
-
#initialize(path, port = 5050) ⇒ App
constructor
A new instance of App.
-
#load_translations ⇒ Object
Loads translations from a given path.
-
#save_translations(translations) ⇒ Object
Write the given translations to the appropriate YAML file example translations: “en.session.new.password”=>“Password”.
-
#start ⇒ Object
Starts I18n Yaml Editor server.
Constructor Details
#initialize(path, port = 5050) ⇒ App
Returns a new instance of App.
18 19 20 21 22 23 |
# File 'lib/i18n_yaml_editor/app.rb', line 18 def initialize(path, port = 5050) @path = File.(path) @port = port || 5050 @store = Store.new I18nYamlEditor.app = self end |
Instance Attribute Details
#store ⇒ Object
Returns the value of attribute store.
16 17 18 |
# File 'lib/i18n_yaml_editor/app.rb', line 16 def store @store end |
Instance Method Details
#load_translations ⇒ Object
Loads translations from a given path
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/i18n_yaml_editor/app.rb', line 39 def load_translations files = if File.directory?(@path) Dir[@path + '/**/*.yml'] elsif File.file?(@path) detect_list_or_file @path else # rubocop:disable Style/StderrPuts $stderr.puts 'No valid translation file given' # rubocop:enable Style/StderrPuts [] end update_store files end |
#save_translations(translations) ⇒ Object
Write the given translations to the appropriate YAML file example translations: “en.session.new.password”=>“Password”
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/i18n_yaml_editor/app.rb', line 56 def save_translations(translations) store.update(translations) changes = files(translations: translations) changes.each do |file, yaml| yaml.extend(YamlNormalizer::Ext::SortByKey) File.open(file, 'w', encoding: 'utf-8') do |f| f.puts yaml.sort_by_key.to_yaml end end end |
#start ⇒ Object
Starts I18n Yaml Editor server
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/i18n_yaml_editor/app.rb', line 26 def start raise "File #{@path} not found." unless File.exist?(@path) $stdout.puts " * Loading translations from #{@path}" load_translations $stdout.puts ' * Creating missing translations' store.create_missing_keys $stdout.puts " * Starting I18n Yaml Editor at port #{@port}" Rack::Server.start app: Web, Port: @port end |