crowdin-cli 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -87,6 +87,26 @@ files:
87
87
  source: /locale/en/**/*.po
88
88
  translation: /locale/%two_letters_code%/**/%original_file_name%
89
89
  ```
90
+
91
+ Languages mapping.
92
+
93
+ ```
94
+ ---
95
+ project_id: test
96
+ api_key: KeepTheAPIkeySecret
97
+ base_url: http://api.crowdin.net
98
+ base_path: /path/to/your/project
99
+
100
+ files:
101
+ -
102
+ source: /locale/en/**/*.po
103
+ translation: /locale/%two_letters_code%/**/%original_file_name%
104
+ languages_mapping:
105
+ '%two_letters_code%':
106
+ ru: ros
107
+ uk: ukr
108
+ ```
109
+
90
110
  ## Usage
91
111
 
92
112
 
data/bin/crowdin-cli CHANGED
@@ -48,13 +48,13 @@ end
48
48
  # @option lang [String] :iso_639_3
49
49
  # @option lang [String] :locale
50
50
  #
51
- def export_pattern_to_path(path, export_pattern, lang)
51
+ def export_pattern_to_path(path, export_pattern, lang, languages_mapping = nil)
52
52
  original_path = File.dirname(path)
53
53
  original_file_name = File.basename(path)
54
54
  file_extension = File.extname(path)[1..-1]
55
- file_name = File.basename(path, file_extension)
55
+ file_name = File.basename(path, File.extname(path))
56
56
 
57
- export_pattern.gsub(/%.+?%/, {
57
+ pattern = {
58
58
  '%language%' => lang['name'],
59
59
  '%two_letters_code%' => lang['iso_639_1'],
60
60
  '%tree_letters_code%' => lang['iso_639_3'],
@@ -65,7 +65,16 @@ def export_pattern_to_path(path, export_pattern, lang)
65
65
  '%original_path%' => original_path,
66
66
  '%file_extension%' => file_extension,
67
67
  '%file_name%' => file_name,
68
- })
68
+ }
69
+
70
+ unless languages_mapping.nil?
71
+ pattern = Hash[pattern.map{|k, str| [
72
+ k,
73
+ (languages_mapping[k[/%(.*)%/, 1]][str] rescue nil) || str]
74
+ }]
75
+ end
76
+
77
+ export_pattern.gsub(/%.+?%/, pattern)
69
78
  end
70
79
 
71
80
  def android_locale_code(locale_code)
@@ -92,21 +101,21 @@ def find_common_directory_path(paths)
92
101
  first.slice(0, i).join('/')
93
102
  end
94
103
 
95
- def unzip_file(file, dest)
104
+ def unzip_file(zip, dest, files_list)
96
105
  # overwrite files if they already exist inside of the extracted path
97
106
  Zip.options[:on_exists_proc] = true
98
107
 
99
- Zip::ZipFile.open(file) do |zip_file|
100
- zip_file.each do |f|
101
- f_path = File.join(dest, f.name)
108
+ Zip::ZipFile.open(zip) do |zip_file|
109
+ zip_file.select{|f| f.file?}.each do |f|
110
+ file = files_list[f.name]
111
+ f_path = File.join(dest, file)
102
112
  FileUtils.mkdir_p(File.dirname(f_path))
103
- puts "Download: #{f}"
113
+ puts "Download: #{file}"
104
114
  zip_file.extract(f, f_path)
105
115
  end
106
116
  end
107
117
  end
108
118
 
109
-
110
119
  ###
111
120
  include GLI::App
112
121
 
@@ -262,6 +271,8 @@ command :upload do |c|
262
271
  dest_files = []
263
272
 
264
273
  @config['files'].each do |file|
274
+ languages_mapping = file['languages_mapping']
275
+
265
276
  if File.exists?("#{@base_path}#{file['source']}")
266
277
  dest = file['source'].sub("#{@base_path}", '')
267
278
  dest_files << dest
@@ -278,10 +289,10 @@ command :upload do |c|
278
289
  file_pattern = export_pattern_to_path(dest, file['translation'], source_language)
279
290
 
280
291
  diff = (dest.split('/') - file_pattern.split('/')).join('/')
281
- export_pattern = file['translation'].sub('**', diff)
292
+ export_pattern = file['translation'].sub('**', diff) # !!!
282
293
 
283
294
  translation_languages.each do |lang|
284
- source = export_pattern_to_path(dest, export_pattern, lang)
295
+ source = export_pattern_to_path(dest, export_pattern, lang, languages_mapping)
285
296
  translated_files[lang['crowdin_code']] << { source: "#{@base_path}#{source}", dest: dest }
286
297
  end
287
298
 
@@ -330,14 +341,67 @@ command :download do |c|
330
341
 
331
342
  c.action do |global_options ,options, args|
332
343
  # use export API method before to download the most recent translations
333
- @crowdin.export_translations
344
+ #@crowdin.export_translations
334
345
 
335
346
  language = options[:language]
347
+ project_info = @crowdin.project_info
348
+
349
+ remote_project_tree = get_remote_files_hierarchy(project_info['files'])
350
+
351
+ if language == 'all'
352
+ project_languages = project_info['languages'].collect{ |h| h['code'] }
353
+ else
354
+ project_languages = [] << language
355
+ end
356
+
357
+ supported_languages = @crowdin.supported_languages
358
+ translation_languages = supported_languages.select{ |lang| project_languages.include?(lang['crowdin_code']) }
359
+
360
+ source_language = project_info['details']['source_language']['code']
361
+ source_language = supported_languages.find{ |lang| lang['crowdin_code'] == source_language }
362
+
363
+ # keys is all possible files in zip archive
364
+ # values is resulted local files
365
+ # usually they are equal
366
+ downloadable_files = {}
367
+
368
+ @config['files'].each do |file|
369
+ languages_mapping = file['languages_mapping'] #Hash or NilClass
370
+
371
+ if File.exists?("#{@base_path}#{file['source']}")
372
+ dest = file['source'].sub("#{@base_path}", '')
373
+
374
+ translation_languages.each do |lang|
375
+ zipper_file = export_pattern_to_path(dest, file['translation'], lang)
376
+ local_file = export_pattern_to_path(dest, file['translation'], lang, languages_mapping)
377
+
378
+ downloadable_files[zipped_file] = local_file
379
+ end
380
+ else
381
+ Dir.glob("#{@base_path}#{file['source']}").each do |source|
382
+ dest = source.sub("#{@base_path}", '') # relative path in Crowdin
383
+
384
+ file_pattern = export_pattern_to_path(dest, file['translation'], source_language)
385
+
386
+ diff = (dest.split('/') - file_pattern.split('/')).join('/')
387
+ export_pattern = file['translation'].sub('**', diff)
388
+
389
+ translation_languages.each do |lang|
390
+ zipped_file = export_pattern_to_path(dest, export_pattern, lang)
391
+ local_file = export_pattern_to_path(dest, export_pattern, lang, languages_mapping)
392
+
393
+ downloadable_files[zipped_file] = local_file
394
+ end
395
+
396
+ end
397
+ end # if
398
+ end # @config['files']
336
399
 
400
+ ##
337
401
  file = Tempfile.new(language)
338
402
  begin
339
403
  @crowdin.download_translation(language, :output => file)
340
- unzip_file(file, @base_path)
404
+ unzip_file(file, @base_path, downloadable_files)
341
405
  ensure
342
406
  file.close
343
407
  file.unlink # delete the temp file
@@ -1,5 +1,5 @@
1
1
  module Crowdin
2
2
  module CLI
3
- VERSION = '0.0.12'
3
+ VERSION = '0.0.13'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crowdin-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Anton Maminov
8
+ - Crowdin
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-16 00:00:00.000000000 Z
12
+ date: 2012-10-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -109,7 +109,7 @@ dependencies:
109
109
  version: 0.0.8
110
110
  description:
111
111
  email:
112
- - anton.linux@gmail.com
112
+ - support@crowdin.net
113
113
  executables:
114
114
  - crowdin-cli
115
115
  extensions: []
@@ -120,7 +120,7 @@ files:
120
120
  - lib/crowdin-cli.rb
121
121
  - README.md
122
122
  - LICENSE
123
- homepage: https://github.com/mamantoha/crowdin-cli
123
+ homepage: https://github.com/crowdin/crowdin-cli/
124
124
  licenses:
125
125
  - LICENSE
126
126
  post_install_message: