lib/bisu.rb in bisu-1.2.4 vs lib/bisu.rb in bisu-1.3.0

- old
+ new

@@ -1,33 +1,35 @@ +require 'optparse' +require 'yaml' + require 'bisu/logger' +require 'bisu/object_extension' require 'bisu/config' require 'bisu/google_sheet' require 'bisu/dictionary' require 'bisu/localizer' require 'bisu/version' -require 'optparse' module Bisu extend self def run(options) options = command_line_options(options) - if config = Bisu::Config.parse("translatable.yml") - google_sheet = Bisu::GoogleSheet.new(config[:sheet_id], config[:keys_column]) - dictionary = Bisu::Dictionary.new(google_sheet.to_hash) - localizer = Bisu::Localizer.new(dictionary, config[:type]) + if config_file = open_file("translatable.yml", "r", true) + config = Bisu::Config.new(hash: YAML::load(config_file)) + google_sheet = Bisu::GoogleSheet.new(config.dictionary[:sheet_id], config.dictionary[:keys_column]) + dictionary = Bisu::Dictionary.new(google_sheet.to_h) + localizer = Bisu::Localizer.new(dictionary, config.type) - config[:in].each do |in_path| - config[:out].each do |out| - unless dictionary.has_language?(out[:kb_language]) - Logger.error("Unknown language #{out[:kb_language]}") - return false - end - - localize_file(localizer, out[:locale], out[:kb_language], options[:default_language], in_path, out[:path] || config[:out_path]) + config.localize_files do |in_path, out_path, language, locale| + unless dictionary.has_language?(language) + Logger.error("Unknown language #{language}") + return false end + + localize_file(localizer, locale, language, options[:default_language], in_path, out_path) end end Bisu::Logger.print_summary end @@ -55,45 +57,35 @@ opts_parser.parse!(options) opts_hash end - def localize_file(localizer, locale, language, default_language, in_path, out_path) - in_name = File.basename(in_path) - out_name = in_name.gsub(/\.translatable$/, "") - - unless in_name.match /\.translatable$/ - Logger.error("Expected .translatable file. Got '#{in_name}'") - return false + def open_file(file_name, method, should_exist) + if !File.file?(File.expand_path(file_name)) + if should_exist + Logger.error("File #{file_name} not found!") + return nil + else + FileUtils.mkdir_p(File.dirname(file_name)) + end end - out_path = out_path % { locale: locale, android_locale: locale.gsub("-", "-r"), out_name: out_name } + File.open(File.expand_path(file_name), method) + end + def localize_file(localizer, locale, language, default_language, in_path, out_path) + Logger.info("Translating #{in_path} to #{language} > #{out_path}...") + return false unless in_file = open_file(in_path, "r", true) return false unless out_file = open_file(out_path, "w", false) - Logger.info("Translating #{in_path} to #{language} > #{out_path}...") - in_file.each_line do |line| out_file.write(localizer.localize(line, language, locale, default_language)) end out_file.flush out_file.close in_file.close true - end - - def open_file(file_name, method, should_exist) - if !File.file?(File.expand_path(file_name)) - if should_exist - Logger.error("File #{file_name} not found!") - return nil - else - FileUtils.mkdir_p(File.dirname(file_name)) - end - end - - File.open(File.expand_path(file_name), method) end end