locgen 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/locgen +21 -0
- data/lib/LocGen.rb +119 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9eab769e3bbc156daddacd3430ba1c5c886c7687
|
4
|
+
data.tar.gz: 4470185b49c409b8a804678e71d8bee4482b15e9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9966111a1368af88a0ab0dd9139c8827b8db9aea004931097a08fd222ced5e1bbadad81027f1042ddf3a63f878e410c004c0fbc68831f012dbcf4e2f9c212593
|
7
|
+
data.tar.gz: 3a5e56915ac563691f967a1672d98560f1f0196ce13eb2a7a499d9917143113a29cb57cd815ff00099de5fe6a8de1084e349331e5aabd5ee4271e1ffceb37d67
|
data/bin/locgen
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'locgen'
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
# Options parser
|
7
|
+
|
8
|
+
options = {}
|
9
|
+
OptionParser.new do |opts|
|
10
|
+
opts.banner = "Usage: LocGen.rb [options]"
|
11
|
+
|
12
|
+
opts.on('-u', '--url URL', 'URL path for xlsx file') { |v| options[:url] = v }
|
13
|
+
opts.on('-o', '--output DIR', 'Output directory path') { |v| options[:output] = v }
|
14
|
+
opts.on('-c', '--comment_col NAME', 'Comments column name (default: comment)') { |v| options[:comment] = v }
|
15
|
+
opts.on('-k', '--key_col NAME', 'Keys column name (default: key)') { |v| options[:key] = v }
|
16
|
+
|
17
|
+
end.parse!
|
18
|
+
|
19
|
+
|
20
|
+
gen = LocGen.new(options[:url],options[:output],options[:comment],options[:key])
|
21
|
+
gen.process()
|
data/lib/LocGen.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'rubyXL' # need install
|
2
|
+
require 'iso-639' # need install
|
3
|
+
require 'open-uri'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
class LocGen
|
7
|
+
TEMPORARY_SHEET_FILE = "temp_loc_sheet.xlsx"
|
8
|
+
OUTPUT_FILE_NAME = "Localizable.strings"
|
9
|
+
LOCAL_INDEX_KEY = "localIndex"
|
10
|
+
SHEET_INDEX_KEY = "sheetIndex"
|
11
|
+
LANG_INDEX_KEY = "lang"
|
12
|
+
|
13
|
+
def initialize(url, outputDir, comment, key)
|
14
|
+
@url = url
|
15
|
+
@outputDir = outputDir
|
16
|
+
@commentColumnName = comment || "comment"
|
17
|
+
@keyColumnName = key || "key"
|
18
|
+
end
|
19
|
+
|
20
|
+
def process()
|
21
|
+
downloadFile(@url, TEMPORARY_SHEET_FILE)
|
22
|
+
if File.exists?(TEMPORARY_SHEET_FILE)
|
23
|
+
parseXLSX(TEMPORARY_SHEET_FILE)
|
24
|
+
prepareIndices()
|
25
|
+
readLocalizations()
|
26
|
+
writeFiles(@outputDir, OUTPUT_FILE_NAME)
|
27
|
+
deleteTempFile(TEMPORARY_SHEET_FILE)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def downloadFile(url, tempFileName)
|
33
|
+
File.open(tempFileName, "wb") do |saved_file|
|
34
|
+
# the following "open" is provided by open-uri
|
35
|
+
open(url, "rb") do |read_file|
|
36
|
+
saved_file.write(read_file.read)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def deleteTempFile(tempFileName)
|
43
|
+
File.delete(tempFileName)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def parseXLSX(file)
|
48
|
+
@xlsx = RubyXL::Parser.parse(file)
|
49
|
+
@sheet = @xlsx[0]
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
def prepareIndices()
|
54
|
+
@languages = []
|
55
|
+
@localizations = []
|
56
|
+
|
57
|
+
localIndex = 0
|
58
|
+
firstRow = @sheet[0]
|
59
|
+
|
60
|
+
firstRow && firstRow.cells.each_with_index { |cell,cellIndex|
|
61
|
+
val = cell && cell.value
|
62
|
+
# keyIndex column index
|
63
|
+
if val == @keyColumnName
|
64
|
+
@keyIndex = cellIndex
|
65
|
+
elsif val == @commentColumnName
|
66
|
+
# commentIndex column index
|
67
|
+
@commentIndex = cellIndex
|
68
|
+
else
|
69
|
+
lang = val && ISO_639.find_by_code(val)
|
70
|
+
if lang && lang.length > 0
|
71
|
+
hash = {LANG_INDEX_KEY => val, SHEET_INDEX_KEY => cellIndex, LOCAL_INDEX_KEY => localIndex}
|
72
|
+
if !@languages.include?(hash)
|
73
|
+
@languages.push(hash)
|
74
|
+
@localizations[localIndex] = []
|
75
|
+
localIndex = @localizations.length
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
}
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
def readLocalizations()
|
84
|
+
@sheet.each_with_index { |row,rowIndex|
|
85
|
+
if rowIndex != 0
|
86
|
+
if row
|
87
|
+
# localized key
|
88
|
+
if row.cells[@keyIndex]
|
89
|
+
key = row.cells[@keyIndex].value
|
90
|
+
end
|
91
|
+
# localized comment
|
92
|
+
if row.cells[@commentIndex]
|
93
|
+
comment = row.cells[@commentIndex].value
|
94
|
+
end
|
95
|
+
|
96
|
+
@languages.each_with_index{|hash,index|
|
97
|
+
if row.cells[hash[SHEET_INDEX_KEY]]
|
98
|
+
localization = row.cells[hash[SHEET_INDEX_KEY]].value
|
99
|
+
@localizations[hash[LOCAL_INDEX_KEY]].push("//#{comment}\n\"#{key}\" = \"#{localization}\";")
|
100
|
+
end
|
101
|
+
}
|
102
|
+
end
|
103
|
+
end
|
104
|
+
}
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
def writeFiles(dir, fileName)
|
109
|
+
@languages.each_with_index{ |hash,index|
|
110
|
+
localIndex = hash["localIndex"]
|
111
|
+
langId = hash["lang"]
|
112
|
+
dirName = "#{langId}.lproj"
|
113
|
+
dirPath = File.join(dir, dirName)
|
114
|
+
Dir.mkdir(dirPath) unless File.exists?(dirPath)
|
115
|
+
File.write("#{dirPath}/#{fileName}", @localizations[localIndex].join("\n\n"))
|
116
|
+
}
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: locgen
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marek Lisik
|
8
|
+
- Łukasz Szyszkowski
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2017-07-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rubyXL
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - '='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 3.3.26
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 3.3.26
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: iso-639
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 0.2.8
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 0.2.8
|
42
|
+
description: Generate localizable strings from xlsx file.
|
43
|
+
email: lukasz.szyszkowski@holdapp.pl
|
44
|
+
executables:
|
45
|
+
- locgen
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- lib/LocGen.rb
|
50
|
+
- bin/locgen
|
51
|
+
homepage: http://rubygems.org/gems/locgen
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
metadata: {}
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 2.0.14.1
|
72
|
+
signing_key:
|
73
|
+
specification_version: 4
|
74
|
+
summary: Localizable strings generator from xlsx files.
|
75
|
+
test_files: []
|