From a96029c21b8b0cce6ca1aa48cc1dd1e3299ce28b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B3mez?= Date: Fri, 15 Sep 2017 07:41:27 +0200 Subject: [PATCH 1/2] Add upload_all method to upload base locale and variants --- lib/onesky/rails/file_client.rb | 131 ++++++++++---------------------- lib/tasks/onesky.rake | 18 +---- 2 files changed, 45 insertions(+), 104 deletions(-) diff --git a/lib/onesky/rails/file_client.rb b/lib/onesky/rails/file_client.rb index c7d4ba1..ac5ac57 100644 --- a/lib/onesky/rails/file_client.rb +++ b/lib/onesky/rails/file_client.rb @@ -9,6 +9,9 @@ class FileClient < Onesky::Rails::Client FILE_FORMAT = 'RUBY_YAML' ENCODING = 'UTF-8' DIR_PREFIX = 'onesky_' + DEFAULT_LOCALE = 'en' + APP_FILE = 'app.yml' + DEVISE_FILE = 'devise.yml' TRANSLATION_NOTICE = <<-NOTICE # This file is generated by onesky-rails gem and will be overwritten at the next download # Therefore, you should not modify this file @@ -18,93 +21,45 @@ class FileClient < Onesky::Rails::Client NOTICE def upload(string_path) - verify_languages! - - get_default_locale_files(string_path).map do |path| - filename = File.basename(path) - puts "Uploading #{filename}" - @project.upload_file(file: path, file_format: FILE_FORMAT, is_keeping_all_strings: is_keep_strings?) - path + all_available_locales.each do |locale| + app_file_path = File.join(string_path, app_locale_file(locale)) + devise_file_path = File.join(string_path, devise_locale_file(locale)) + + puts "Uploading #{locale}..." + + @project.upload_file(file: app_file_path, + file_format: FILE_FORMAT, + is_keeping_all_strings: is_keep_strings?, + locale: locale) + puts " - #{File.basename(app_file_path)} done!" + @project.upload_file(file: devise_file_path, + file_format: FILE_FORMAT, + is_keeping_all_strings: is_keep_strings?, + locale: locale) + puts " - #{File.basename(devise_file_path)} done!" end end - ## - # Download translations from OneSky platform - # - # +string_path+ specify the folder path where all localization files locate - # +options+ indicate which files to download - # - :base_only => base language only - # - :all => all languages including base language - # - => default value; translation languages - # def download(string_path, options = {}) - verify_languages! - - files = get_default_locale_files(string_path).map {|path| File.basename(path)} - - locales = if options[:base_only] - [@base_locale] - elsif options[:all] - [@base_locale] + @onesky_locales - else - @onesky_locales - end - - locales.each do |locale| - locale = locale.to_s - puts "#{locale_dir(locale)}/" - onesky_locale = locale.gsub('_', '-') - files.each do |file| - response = @project.export_translation(source_file_name: file, locale: onesky_locale) - if response.code == 200 - saved_file = save_translation(response, string_path, locale, file) - puts " #{saved_file}" - end - end + all_available_locales.each do |locale| + puts "Downloading #{locale}..." + + response = @project.export_translation(source_file_name: APP_FILE, locale: locale) + save_translation(response, string_path, app_locale_file(locale)) + puts " - #{APP_FILE} done!" + response = @project.export_translation(source_file_name: DEVISE_FILE, locale: locale) + save_translation(response, string_path, devise_locale_file(locale)) + puts " - #{DEVISE_FILE} done!" end end protected - def locale_dir(locale) - DIR_PREFIX + locale - end - - def make_translation_dir(dir_path, locale) - return dir_path if locale == @base_locale.to_s - - target_path = File.join(dir_path, locale_dir(locale)) - Dir.mkdir(target_path) unless File.directory?(target_path) - target_path - end - - def locale_file_name(file, to_locale) - if File.basename(file, '.*') == @base_locale.to_s - file.sub(@base_locale.to_s, to_locale) - else - file - end - end - - def get_default_locale_files(string_path) - string_path = Pathname.new(string_path) - locale_files_filter = generate_locale_files_filter - Dir.glob("#{string_path}/**/*.yml").map do |path| - relative_path = Pathname.new(path).relative_path_from(string_path).to_s - next if locale_files_filter && !locale_files_filter.call(relative_path) - content_hash = YAML.load_file(path) - path if content_hash && content_hash.has_key?(@base_locale.to_s) - end.compact - end - - def save_translation(response, string_path, locale, file) - locale_path = make_translation_dir(string_path, locale) - target_file = locale_file_name(file, locale) - - File.open(File.join(locale_path, target_file), 'w') do |f| + def save_translation(response, string_path, target_file) + file_path = File.join(string_path, target_file) + File.open(file_path, 'w') do |f| f.write(TRANSLATION_NOTICE + response.body.force_encoding(ENCODING)) end - target_file end def is_keep_strings? @@ -113,23 +68,21 @@ def is_keep_strings? !!upload_config['is_keeping_all_strings'] end - def generate_locale_files_filter - only = Array(upload_config['only']) - except = Array(upload_config['except']) + def upload_config + @config['upload'] ||= {} + end + + def all_available_locales + [:en] + I18n.available_locales.reject { |locale| locale == :en } + end - if only.any? && except.any? - raise ArgumentError, "Invalid config. Can't use both `only` and `except` options." - end - if only.any? - ->(path) { only.include?(path) } - elsif except.any? - ->(path) { !except.include?(path) } - end + def app_locale_file(locale) + "#{locale}/#{APP_FILE}" end - def upload_config - @config['upload'] ||= {} + def devise_locale_file(locale) + "#{locale}/#{DEVISE_FILE}" end end diff --git a/lib/tasks/onesky.rake b/lib/tasks/onesky.rake index 641d80f..5b8894e 100644 --- a/lib/tasks/onesky.rake +++ b/lib/tasks/onesky.rake @@ -1,26 +1,14 @@ namespace :onesky do - desc 'Upload string files of base locale to OneSky platform.' - task :upload => :environment do + desc 'Upload all string files locale to OneSky platform.' + task :upload_all => :environment do file_client.upload(locale_path) puts 'Done!' end desc 'Download translations from OneSky platform.' - task :download => :environment do - file_client.download(locale_path) - puts 'Done!' - end - - desc 'Download base language translations from OneSky platform.' - task :download_base => :environment do - file_client.download(locale_path, base_only: true) - puts 'Done!' - end - - desc 'Download all languages translations from OneSky platform.' task :download_all => :environment do - file_client.download(locale_path, all: true) + file_client.download(locale_path) puts 'Done!' end From 759b94364052fa0adab625fe84e1d65a912c58f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B3mez?= Date: Fri, 15 Sep 2017 11:50:05 +0200 Subject: [PATCH 2/2] Clean --- lib/onesky/rails/file_client.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/onesky/rails/file_client.rb b/lib/onesky/rails/file_client.rb index ac5ac57..2b3d274 100644 --- a/lib/onesky/rails/file_client.rb +++ b/lib/onesky/rails/file_client.rb @@ -9,7 +9,6 @@ class FileClient < Onesky::Rails::Client FILE_FORMAT = 'RUBY_YAML' ENCODING = 'UTF-8' DIR_PREFIX = 'onesky_' - DEFAULT_LOCALE = 'en' APP_FILE = 'app.yml' DEVISE_FILE = 'devise.yml' TRANSLATION_NOTICE = <<-NOTICE