localio 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 37ebfdfd8bd72c867b84d975ce1f069db365fabf
4
- data.tar.gz: d758c6f63079a4e3ff4c7b7e1b29dfdc93932054
3
+ metadata.gz: 557ff500076f77cf2d3a1c650b62892d60e92b32
4
+ data.tar.gz: 6d59d95e89c9d8d76cf66caebf6beb8ca5f925ca
5
5
  SHA512:
6
- metadata.gz: 3ebb579abcb7071d11e5daac7df6d00c6ff85773908557067a0abaad63558c6cb8f15247813dc1f60581404a4c8e246ae4fdaeea99ed9e47b2a866f357f1f0ce
7
- data.tar.gz: bd43ae7b036b857c47e8965ee4e5b0358d5162d4c3aa558d60d76f1c97a96f95e2260ffcc536621ec04744626391e8f6940901092dfdc987fa7d47aa03cad552
6
+ metadata.gz: 1b2feb3cc3c969f72c3ba540c83f7b38ffd81dee6499737203febc72d4e9e1a2ad4718d92a9ad6fa3e046ba951a9af653d521bab9d05aea2aa6966fb9810aa33
7
+ data.tar.gz: fb6bdeeb00d8586974bbebcf7a55cd809e30c9db623a11b4fe086de2a20f19817f1d9b6d54a5b6310e9ff26770fe074daa61209566c366b09ee09fac398de5da
data/README.md CHANGED
@@ -18,7 +18,7 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- You have to create a custom file, Locfile, similar to Rakefile or Gemfile, with some information for this to work. Also you must have some spreadsheet with a particular format, either in Google Drive or in Excel (XLS or XLSX) format.
21
+ You have to create a custom file, Locfile, similar to Rakefile or Gemfile, with some information for this to work. Also you must have some spreadsheet with a particular format, either in Google Drive, CSV files or in Excel (XLS or XLSX) format.
22
22
 
23
23
  In your Locfile directory you can then execute
24
24
 
@@ -45,12 +45,10 @@ A minimal `Locfile` example could be:
45
45
  ````ruby
46
46
  platform :ios
47
47
 
48
- output_path 'out/'
48
+ output_path 'my_output_path/'
49
49
 
50
50
  source :xlsx,
51
51
  :path => 'my_translations.xlsx'
52
-
53
- formatting :smart # This is optional, formatting :smart is used by default.
54
52
  ````
55
53
 
56
54
  This would connect localio to your Google Drive and process the spreadsheet with title "[Localizables] My Project!".
@@ -128,11 +126,28 @@ Setting it up is a bit of a pain, although it is only required the first time an
128
126
  5. Select again the third option, **Installed Application**, and in the platform selector select the last one, **Others**.
129
127
  6. You will have all the necessary information in the next screen: Client ID and Client Secret.
130
128
 
131
- After doing all this, you are ready to add `:client_id` and `:client_secret` fields to your Locfile `source`.
129
+ After doing all this, you are ready to add `:client_id` and `:client_secret` fields to your Locfile `source`. It will look somewhat like this at this stage:
130
+
131
+ ```ruby
132
+ source :google_drive,
133
+ :spreadsheet => '[Localizables] My Project',
134
+ :client_id => 'XXXXXXXXX-XXXXXXXX.apps.googleusercontent.com',
135
+ :client_secret => 'asdFFGGhjKlzxcvbnm'
136
+ ```
132
137
 
133
138
  Then, the first time you run it, you will be prompted to follow some instructions. You will be asked to open a website, where you will be prompted for permission to use the Drive API. After you allow it, you will be given an authorization code, which you will have to paste in your terminal screen when prompted.
134
139
 
135
- After all this is done, you will be given in the `:access_token` in the output. Just add it to the `source` parameters, as you did with the id and secret before, and that's it. Hopefully you won't happen to repeat any of these steps for a long time.
140
+ After all this is done, you will be given in the `:access_token` in the output. Just add it to the `source` parameters, as you did with the id and secret before, and that's it. It will look now like this:
141
+
142
+ ```ruby
143
+ source :google_drive,
144
+ :spreadsheet => '[Localizables] My Project',
145
+ :client_id => 'XXXXXXXXX-XXXXXXXX.apps.googleusercontent.com',
146
+ :client_secret => 'asdFFGGhjKlzxcvbnm',
147
+ :access_token => 'ya29.RAEXXxxxxsadsadajsdhasuidhakjsdhkajhsduiahsduiasd89a8912'
148
+ ```
149
+
150
+ When the Locfile contains the `:access_token`, all the login in process for generating localizables will be automatic and won't require your attention. The parameters for `:client_id` and `:client_secret` are not really needed anymore, although you could leave them there if you want.
136
151
 
137
152
  **NOTE** As it is a very bad practice to put your sensitive information in a plain file, specially when you would want to upload your project to some repository, it is **VERY RECOMMENDED** that you use environment variables in here. Ruby syntax is accepted so you can use `ENV['CLIENT_SECRET']`, `ENV['CLIENT_ID']` and `ENV['ACCESS_TOKEN']` in here.
138
153
 
@@ -180,6 +195,23 @@ source :xlsx,
180
195
  :path => 'YourExcelFileWithTranslations.xlsx'
181
196
  ````
182
197
 
198
+ ##### CSV
199
+
200
+ `source :csv` will use a local CSV file. In the parameter's hash you should specify a `:path`.
201
+
202
+ Option | Description
203
+ ----------------------------|-------------------------------------------------------------------------
204
+ `:path` | (Req.) Path for your CSV file.
205
+ `:column_separator` | By default it is ',', but you can change it with this parameter
206
+
207
+ In this example we specify tabs as separators for translation columns. The `:column_separator` is not needed if the separator is a comma and could be removed.
208
+
209
+ ````ruby
210
+ source :csv,
211
+ :path => 'YourCSVTranslations.csv',
212
+ :column_separator => '\t'
213
+ ````
214
+
183
215
  #### Key formatters
184
216
 
185
217
  If you don't specify a formatter for keys, :smart will be used.
@@ -1,6 +1,7 @@
1
1
  require 'localio/processors/google_drive_processor'
2
2
  require 'localio/processors/xls_processor'
3
3
  require 'localio/processors/xlsx_processor'
4
+ require 'localio/processors/csv_processor'
4
5
 
5
6
  module Processor
6
7
  def self.load_localizables(platform_options, service, options)
@@ -11,8 +12,10 @@ module Processor
11
12
  XlsProcessor.load_localizables platform_options, options
12
13
  when :xlsx
13
14
  XlsxProcessor.load_localizables platform_options, options
15
+ when :csv
16
+ CsvProcessor.load_localizables platform_options, options
14
17
  else
15
- raise ArgumentError, 'Unsupported service! Try with :google_drive, :xlsx or :xls in the source argument'
18
+ raise ArgumentError, 'Unsupported service! Try with :google_drive, :csv, :xlsx or :xls in the source argument'
16
19
  end
17
20
  end
18
21
  end
@@ -0,0 +1,81 @@
1
+ require 'csv'
2
+ require 'localio/term'
3
+
4
+ class CsvProcessor
5
+
6
+ def self.load_localizables(platform_options, options)
7
+
8
+ # Parameter validations
9
+ path = options[:path]
10
+ raise ArgumentError, ':path attribute is missing from the source, and it is required for CSV spreadsheets' if path.nil?
11
+
12
+ override_default = nil
13
+ override_default = platform_options[:override_default] unless platform_options.nil? or platform_options[:override_default].nil?
14
+
15
+ # , is the default separator; we only set this if we specified a different separator
16
+ separator = options[:column_separator] ||= ','
17
+
18
+ csv_file = CSV.read path, col_sep: separator
19
+
20
+ # At this point we have the worksheet, so we want to store all the key / values
21
+ first_valid_row_index = nil
22
+ last_valid_row_index = nil
23
+
24
+ for row in 1..csv_file.length-1
25
+ first_valid_row_index = row if csv_file[row][0].to_s.downcase == '[key]'
26
+ last_valid_row_index = row if csv_file[row][0].to_s.downcase == '[end]'
27
+ end
28
+
29
+ raise IndexError, 'Invalid format: Could not find any [key] keyword in the A column of the CSV file' if first_valid_row_index.nil?
30
+ raise IndexError, 'Invalid format: Could not find any [end] keyword in the A column of the CSV file' if last_valid_row_index.nil?
31
+ raise IndexError, 'Invalid format: [end] must not be before [key] in the A column' if first_valid_row_index > last_valid_row_index
32
+
33
+ languages = Hash.new('languages')
34
+ default_language = nil
35
+
36
+ for column in 1..csv_file[first_valid_row_index].count-1
37
+ col_all = csv_file[first_valid_row_index][column].to_s
38
+ col_all.each_line(' ') do |col_text|
39
+ default_language = col_text.downcase.gsub('*', '') if col_text.include? '*'
40
+ languages.store col_text.downcase.gsub('*', ''), column unless col_text.to_s == ''
41
+ end
42
+ end
43
+
44
+ raise 'There are no language columns in the CSV file' if languages.count == 0
45
+
46
+ default_language = languages[0] if default_language.to_s == ''
47
+ default_language = override_default unless override_default.nil?
48
+
49
+ puts "Languages detected: #{languages.keys.join(', ')} -- using #{default_language} as default."
50
+
51
+ puts 'Building terminology in memory...'
52
+
53
+ terms = []
54
+ first_term_row = first_valid_row_index+1
55
+ last_term_row = last_valid_row_index-1
56
+
57
+ for row in first_term_row..last_term_row
58
+ key = csv_file[row][0]
59
+ unless key.to_s == ''
60
+ term = Term.new(key)
61
+ languages.each do |lang, column_index|
62
+ term_text = csv_file[row][column_index]
63
+ term.values.store lang, term_text
64
+ end
65
+ terms << term
66
+ end
67
+ end
68
+
69
+ puts 'Loaded!'
70
+
71
+ # Return the array of terms, languages and default language
72
+ res = Hash.new
73
+ res[:segments] = terms
74
+ res[:languages] = languages
75
+ res[:default_language] = default_language
76
+
77
+ res
78
+
79
+ end
80
+
81
+ end
@@ -1,3 +1,3 @@
1
1
  module Localio
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: localio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nacho Lopez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-28 00:00:00.000000000 Z
11
+ date: 2015-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -145,6 +145,7 @@ files:
145
145
  - lib/localio/locfile.rb
146
146
  - lib/localio/module.rb
147
147
  - lib/localio/processor.rb
148
+ - lib/localio/processors/csv_processor.rb
148
149
  - lib/localio/processors/google_drive_processor.rb
149
150
  - lib/localio/processors/xls_processor.rb
150
151
  - lib/localio/processors/xlsx_processor.rb