Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upload_all method to upload base locale and variants #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 41 additions & 89 deletions lib/onesky/rails/file_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class FileClient < Onesky::Rails::Client
FILE_FORMAT = 'RUBY_YAML'
ENCODING = 'UTF-8'
DIR_PREFIX = 'onesky_'
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
Expand All @@ -18,93 +20,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
# - <empty> => 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?
Expand All @@ -113,23 +67,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
Expand Down
18 changes: 3 additions & 15 deletions lib/tasks/onesky.rake
Original file line number Diff line number Diff line change
@@ -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

Expand Down