lib/bisu.rb in bisu-1.4.7 vs lib/bisu.rb in bisu-1.5.0

- old
+ new

@@ -3,12 +3,12 @@ require 'fileutils' require 'bisu/logger' require 'bisu/object_extension' require 'bisu/config' -require 'bisu/google_sheet' -require 'bisu/one_sky' +require 'bisu/source/google_sheet' +require 'bisu/source/one_sky' require 'bisu/dictionary' require 'bisu/localizer' require 'bisu/version' module Bisu @@ -17,11 +17,11 @@ def run(options) options = command_line_options(options) if config_file = open_file("translatable.yml", "r", true) config = Bisu::Config.new(hash: YAML::load(config_file)) - dictionary = dictionary_for(config: config.dictionary, save_to_path: options[:dictionary_save_path]) + dictionary = dictionary_for(config: config.dictionary, options: options) localizer = Bisu::Localizer.new(dictionary, config.type) config.localize_files do |in_path, out_path, language, locale| unless dictionary.has_language?(language) Logger.error("Unknown language #{language}") @@ -35,35 +35,58 @@ Bisu::Logger.print_summary end private - def dictionary_for(config:, save_to_path:) + def dictionary_for(config:, options:) source = + if from_file_path = options[:from_file_path] + i18n_from(path: from_file_path) + else + i18n_for(config: config, options: options) + end + + Bisu::Dictionary.new(source) + end + + def i18n_for(config:, options:) + source = case config[:type] when "google_sheet" - Bisu::GoogleSheet.new(config[:sheet_id], config[:keys_column]) + Bisu::Source::GoogleSheet.new(config[:sheet_id], config[:keys_column]) when "one_sky" - Bisu::OneSky.new(config[:api_key], config[:api_secret], config[:project_id], config[:file_name]) + Bisu::Source::OneSky.new(config[:api_key], config[:api_secret], config[:project_id], config[:file_name]) end source = source.to_i18 + save_to_path = options[:dictionary_save_path] if save_to_path && file = open_file(save_to_path, "w", false) file.write(source.to_json) file.flush file.close end - Bisu::Dictionary.new(source) + source end + def i18n_from(path:) + file = open_file(path, "r", true) + data = file.read + file.close + JSON.parse(data) + end + def command_line_options(options) opts_hash = {} opts_parser = OptionParser.new do |opts| opts.on("-d LANGUAGE", "--default LANGUAGE", "Language to use when there is no available translation") do |language| opts_hash[:default_language] = language + end + + opts.on("--file PATH", "Loads i18n source directly from a local file") do |file| + opts_hash[:from_file_path] = file end opts.on("--save-dictionary PATH", "Save downloaded dictionary locally at given path") do |path| opts_hash[:dictionary_save_path] = path end