bin/crowdin-cli in crowdin-cli-0.0.14 vs bin/crowdin-cli in crowdin-cli-0.0.15
- old
+ new
@@ -1,8 +1,7 @@
#!/usr/bin/env ruby
-require 'pp'
require 'crowdin-cli'
# Return +hierarchy+ of directories and files in Crowdin project
#
# +files+ - basically, it's project files details from API method `project_info`
@@ -65,18 +64,20 @@
'%original_path%' => original_path,
'%file_extension%' => file_extension,
'%file_name%' => file_name,
}
+ placeholders = pattern.inject([]){ |memo, h| memo << h.first[/%(.*)%/, 1] }
+
unless languages_mapping.nil?
- pattern = Hash[pattern.map{|k, str| [
- k,
- (languages_mapping[k[/%(.*)%/, 1]][lang['crowdin_code']] rescue nil) || str]
+ pattern = Hash[pattern.map{|placeholder, str| [
+ placeholder,
+ (languages_mapping[placeholder[/%(.*)%/, 1]][lang['crowdin_code']] rescue nil) || str]
}]
end
- export_pattern.gsub(/%.+?%/, pattern)
+ export_pattern.gsub(/%(#{placeholders.join('|')})%/, pattern)
end
def android_locale_code(locale_code)
locale_code = case locale_code
when 'he-IL' then 'iw-IL'
@@ -85,10 +86,28 @@
else locale_code
end
return locale_code.sub('-', '-r')
end
+def get_invalid_placeholders(export_pattern)
+ valid_placeholders = [
+ '%language%',
+ '%two_letters_code%',
+ '%tree_letters_code%',
+ '%locale%',
+ '%locale_with_underscore%',
+ '%android_code%',
+ '%original_file_name%',
+ '%original_path%',
+ '%file_extension%',
+ '%file_name%'
+ ]
+
+ all_placeholders = export_pattern.scan(/%[a-z0-9_]*?%/)
+ invalid_placeholders = all_placeholders - valid_placeholders
+end
+
# Return a string representing that part of the directory tree that is common to all the files
#
# @params [Array] paths set of strings representing directory paths
#
def find_common_directory_path(paths)
@@ -163,10 +182,14 @@
local_files = []
dest_files = []
@config['files'].each do |file|
+ get_invalid_placeholders(file['translation']).each do |placeholder|
+ puts "Warning: #{placeholder} is not valid variable supported by Crowdin. See http://crowdin.net/page/cli-client#configuration-file for more details."
+ end
+
if File.exist?("#{@base_path}#{file['source']}")
dest_files << file['source']
local_files << { dest: file['source'], source: "#{@base_path}#{file['source']}", export_pattern: file['translation'] }
else
Dir.glob("#{@base_path}#{file['source']}").each do |source|
@@ -206,22 +229,27 @@
# array containing elements common to the two arrays
update_files = local_project_tree[:files] & remote_project_tree[:files]
files_for_upload = local_files.select{ |file| update_files.include?(file[:dest]) }
files_for_upload.each do |file|
print "Updating file `#{file[:dest]}'"
- @crowdin.update_file([] << file)
- puts "\rUpdating file `#{file[:dest]}' - OK"
+ resp = @crowdin.update_file([] << file)
+ case resp['files'].first[1]
+ when 'skipped'
+ puts "\rUpdating file `#{file[:dest]}' - Skipped"
+ when 'updated'
+ puts "\rUpdating file `#{file[:dest]}' - OK"
+ end
end
end
# Add new files to Crowdin project
#
add_files = local_project_tree[:files] - remote_project_tree[:files]
files_for_add = local_files.select{ |file| add_files.include?(file[:dest]) }
files_for_add.each do |file|
print "Uploading file `#{file[:dest]}'"
- @crowdin.add_file([] << file)
+ res = @crowdin.add_file([] << file)
puts "\rUploading file `#{file[:dest]}' - OK"
end
end # action
end # command
@@ -272,9 +300,13 @@
translated_files = Hash.new{ |hash, key| hash[key] = Array.new }
dest_files = []
@config['files'].each do |file|
+ get_invalid_placeholders(file['translation']).each do |placeholder|
+ puts "Warning: #{placeholder} is not valid variable supported by Crowdin. See http://crowdin.net/page/cli-client#configuration-file for more details."
+ end
+
languages_mapping = file['languages_mapping']
if File.exists?("#{@base_path}#{file['source']}")
dest = file['source'].sub("#{@base_path}", '')
dest_files << dest