swiftlocalizer 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 31f921e1c965cb474cb9de177d8052f7161d55ff
4
- data.tar.gz: 25dbfcae5576d38be60826faa7c8bf1c954658ba
3
+ metadata.gz: 710f8b2561b1788132643c6bf77e5872e845802a
4
+ data.tar.gz: 8a9b26b615b832fbd2e479e721dae2d924bd6efa
5
5
  SHA512:
6
- metadata.gz: 337b38d9d6e1c5a96845d6557e8ed0671d3784648b889827b12daea0da449bedc256cd1243d782571a47aeaadb879bc574856a9f6ea45b6e51bb7f48d2afa15b
7
- data.tar.gz: 04449e92a87d0dd26bde6895056acfa7081252cf9cdef5e2d10e2d249f0deef086c0ba367ba897a23d4d441e7e444c8851f467c0c85bd5540f6de409c838ce81
6
+ metadata.gz: 4b6299457bebfbaf7a9d38144b55bc4257aa66daec4f9d4a50971425fdb29034039d8b71c6dc772e151818608516cb6d016a7a6c1b1f5a8a8e57b9fd4554672e
7
+ data.tar.gz: 7ef822c673d525f19f5ee04300f91aac67f551b8bae2a753afdb1c1df70c584fafcdd8933efff5946a47ffe7a0789742bf527f54ae8d06b7ee22b6db3f02f2d5
@@ -16,12 +16,43 @@ module Swiftlocalizer
16
16
  "#{@en},#{@ja},#{@filename},#{@lineno}"
17
17
  end
18
18
 
19
+ def str_and_lineno
20
+ "#{@filename}: #{@en} #{@ja}"
21
+ end
22
+
19
23
  def to_short_s
20
24
  "#{@en} #{@ja} #{File.basename(@filename)}:#{@lineno}"
21
- end
25
+ end
26
+
27
+ def key
28
+ @en
29
+ end
30
+ end
31
+
32
+ class LocalizableString
33
+ def initialize(str, filename, lineno)
34
+ @str = str
35
+ @filename = filename
36
+ @lineno = lineno
37
+ end
38
+ attr_accessor :str, :filename, :lineno
39
+ def to_s
40
+ "#{@str},#{@filename},#{@lineno}"
41
+ end
42
+ def str_and_lineno
43
+ "#{@lineno}: #{@str}"
44
+ end
45
+ def to_short_s
46
+ "#{@str} #{File.basename(@filename)}:#{@lineno}"
47
+ end
22
48
  end
23
49
 
24
50
  class Command
51
+
52
+ NSLOCALIZED_STRING_REGEX = /NSLocalizedString\("([^"]+)",\s*comment:\s*"([^"]+)"\)/
53
+ PRINT_REGEX = /print\("([^"]+)"\)/
54
+ COMMENT_REGEX = /\/\/.*$/
55
+
25
56
  def self.get_localized_strings_from_file(file)
26
57
  get_localized_strings_from_lines(File.readlines(file), file)
27
58
  end
@@ -29,20 +60,57 @@ module Swiftlocalizer
29
60
  def self.get_localized_strings_from_lines(lines, file)
30
61
  strings = []
31
62
  lines.each_with_index do |line, index|
32
- strings.concat(get_localized_strings_from_line(line, file, index))
63
+ strings.concat(get_localized_strings_from_line(line, file, index + 1))
33
64
  end
34
65
  strings
35
66
  end
36
-
37
- def self.get_localized_strings_from_line(line, file, index)
67
+
68
+ def self.get_localized_strings_from_line(line, file, lineno)
38
69
  strings = []
39
- while line =~ /NSLocalizedString\("([^"]+)",\s*comment:\s*"([^"]+)"\)/
40
- string = LocalizedString.new($1, $2, file, index)
70
+ while line =~ NSLOCALIZED_STRING_REGEX
71
+ string = LocalizedString.new($1, $2, file, lineno)
41
72
  strings << string
42
73
  line = $'
43
74
  end
44
75
  strings
45
76
  end
77
+
78
+ def self.get_localizable_strings_from_file(file)
79
+ get_localizable_strings_from_lines(File.readlines(file), file)
80
+ end
81
+
82
+ def self.get_localizable_strings_from_lines(lines, file)
83
+ strings = []
84
+ lines.each_with_index do |line, index|
85
+ strings.concat(get_localizable_strings_from_line(line, file, index + 1))
86
+ end
87
+ strings
88
+ end
89
+
90
+ def self.get_localizable_strings_from_line(line, file, lineno)
91
+ strings = []
92
+ matched = true
93
+ while matched
94
+ if line =~ NSLOCALIZED_STRING_REGEX || line =~ PRINT_REGEX || line =~ COMMENT_REGEX
95
+ line = $'
96
+ next
97
+ end
98
+ if line =~ /"([^"]+)"/
99
+ if jstring?($1)
100
+ string = LocalizableString.new($1, file, lineno)
101
+ strings << string
102
+ end
103
+ line = $'
104
+ else
105
+ matched = false
106
+ end
107
+ end
108
+ strings
109
+ end
110
+
111
+ def self.jstring?(str)
112
+ str.bytesize > str.size
113
+ end
46
114
 
47
115
  def self.run(argv)
48
116
  STDOUT.sync = true
@@ -58,7 +126,7 @@ module Swiftlocalizer
58
126
  end
59
127
  opt.on('-v', '--verbose', 'Verbose message') {|v| opts[:v] = v}
60
128
  opt.on('-n', '--dry-run', 'Message only') {|v| opts[:n] = v}
61
- # opt.on('-f', '--force-import', 'Force import') {|v| opts[:f] = v}
129
+ opt.on('-c', '--check', 'Check localizable strings') {|v| opts[:c] = v}
62
130
  opt.parse!(argv)
63
131
  dir = ARGV.shift || '.'
64
132
  command = Command.new(opts, dir)
@@ -71,7 +139,24 @@ module Swiftlocalizer
71
139
  end
72
140
 
73
141
  def run
74
- strings = scan_sources
142
+ if @opts[:c]
143
+ check_strings
144
+ else
145
+ scan_and_write
146
+ end
147
+ end
148
+
149
+ private
150
+ def check_strings
151
+ strings = scan_sources do |f|
152
+ Command.get_localizable_strings_from_file(f)
153
+ end
154
+ end
155
+
156
+ def scan_and_write
157
+ strings = scan_sources do |f|
158
+ Command.get_localized_strings_from_file(f)
159
+ end
75
160
 
76
161
  check_duplicate(strings)
77
162
 
@@ -81,18 +166,17 @@ module Swiftlocalizer
81
166
  write_localizable_strings(strings, path, :en)
82
167
 
83
168
  path = File.join(@dir, 'ja.lproj', basename)
84
- write_localizable_strings(strings, path, :ja)
169
+ write_localizable_strings(strings, path, :ja)
85
170
  end
86
-
87
- private
171
+
88
172
  def scan_sources
89
173
  puts "Scan #{@dir}"
90
174
  strings = []
91
175
  Dir.glob(@dir + '/**/*.swift').each do |f|
92
176
  puts f
93
- file_strings = Command.get_localized_strings_from_file(f)
177
+ file_strings = yield(f)
94
178
  puts "retrieve #{file_strings.size} strings"
95
- file_strings.each{|str| puts "\t#{str.en} #{str.ja} #{str.lineno}\n"}
179
+ file_strings.each{|str| puts "\t#{str.str_and_lineno}\n"}
96
180
  strings.concat(file_strings)
97
181
  end
98
182
  strings
@@ -117,7 +201,7 @@ module Swiftlocalizer
117
201
  raise RuntimeError, "#{path} doesn't exist."
118
202
  end
119
203
  File.open(path, 'w') do |f|
120
- strings.each do |str|
204
+ strings.sort_by{|a| a.key }.each do |str|
121
205
  en = str.send(:en)
122
206
  localized = str.send(sym.to_sym)
123
207
  f.puts "\"#{en}\" = \"#{localized}\";"
@@ -1,3 +1,3 @@
1
1
  module Swiftlocalizer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swiftlocalizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - src
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-19 00:00:00.000000000 Z
11
+ date: 2016-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler