lokale 0.1.4 → 0.1.5

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: 8306f8c00603bf616bf961689c9936e9a28f901a
4
- data.tar.gz: e7d128b7bca1c820bfd51372a688679f1be7cf0e
3
+ metadata.gz: 6425966414bd882a98b6cbb366a9f48df3b0776d
4
+ data.tar.gz: 9b5a5fe54afcbda06146fa65e937dde04c16ece6
5
5
  SHA512:
6
- metadata.gz: e257d6840e3d282a1255a77570eda0db65956dde7fb26a863befd216f6efe5393c00025463611dc4547360a35582e959664be6849258f14fcd531f7ad5085227
7
- data.tar.gz: '09a4e2acce57ba98840c9b097eac20195f6df8ed8933a5547868533cc3b1c5e0469743dbbe8f0a48fbc29bc785cfb6be74627a40c35f6b8a38c03db5c4e3b47f'
6
+ metadata.gz: fd132a13d0249ea7f8c94ddeed7779829180d843dc3436caf2437d3473ade3d54e25d73be77eebd920dd506be983d064d6d8654ba6132945320ccb36f57f3e48
7
+ data.tar.gz: 2ec72066af3f7dfab0fc17543e71c046cca22e10a8aa1fbd20e55f6e2d0371111e7d76cabb1cf67542c6c3d9dbc85b80916dbb356e4f4d176855d2b24a5d8724
@@ -45,6 +45,11 @@ module Lokale
45
45
  print "Creating config file...".blue
46
46
  Config.get.create_default_file
47
47
  end
48
+
49
+ def perform_add_to_proj(agent, reporter)
50
+ print "Creating project file...".blue
51
+ agent.write_to_project_file
52
+ end
48
53
  end
49
54
  end
50
55
 
@@ -1,6 +1,7 @@
1
1
 
2
2
  add_macro "NSLocalizedString" do |m|
3
3
  m.localization_file = "Localizable.strings"
4
+ m.project_file = "Strings.swift"
4
5
 
5
6
  m.regex = /NSLocalizedString\("(.+?)",\s*comment:\s*"(.*?)"\)/
6
7
  m.key_index = 0
@@ -6,6 +6,32 @@ require "xliffle"
6
6
  require "xliffer"
7
7
  require "set"
8
8
 
9
+ class Hash
10
+ def set_by_key_path(key_path, val)
11
+ last_hash = self
12
+ last_key = key_path.pop
13
+ key_path.each do |k|
14
+ if last_hash.has_key? k
15
+ last_hash = last_hash[k]
16
+ else
17
+ new_hash = Hash.new
18
+ last_hash[k] = new_hash
19
+ last_hash = new_hash
20
+ end
21
+ end
22
+
23
+ last_hash[last_key] = val
24
+ end
25
+ end
26
+
27
+ class String
28
+ def camelize(type=:upper)
29
+ case type
30
+ when :upper then return self[0].upcase + self[1..-1]
31
+ when :lower then return self[0].downcase + self[1..-1]
32
+ end
33
+ end
34
+ end
9
35
 
10
36
  module Lokale
11
37
  class LFile
@@ -29,7 +55,10 @@ module Lokale
29
55
 
30
56
  class LString
31
57
  def write_format
32
- note = @note || "(no comment)"
58
+ note = @note unless @note.empty?
59
+ note = @source if note.nil?
60
+ note = "(no comment)" if note.nil?
61
+
33
62
  "/* #{note} */\n\"#{key}\" = \"#{str}\";\n"
34
63
  end
35
64
 
@@ -43,10 +72,6 @@ module Lokale
43
72
  str
44
73
  end
45
74
 
46
-
47
-
48
- attr_accessor :source
49
-
50
75
  def for_export(lang)
51
76
  str = LString.new(@key, nil, @note, lang)
52
77
  str.source = @str
@@ -59,11 +84,12 @@ end
59
84
 
60
85
  module Lokale
61
86
  class Agent
62
- attr_reader :proj_path, :macros, :lfiles, :sfiles_proceeded
87
+ attr_reader :proj_path, :macros, :lfiles, :sfiles_proceeded, :project_lfiles
63
88
 
64
89
  def initialize(proj_path, macros)
65
90
  @proj_path = proj_path
66
91
  @macros = macros
92
+ @project_lfiles = []
67
93
 
68
94
  @writer = Writer.new
69
95
  @exporter = Exporter.new
@@ -83,6 +109,10 @@ module Lokale
83
109
  end
84
110
  end
85
111
 
112
+ def self.source_file?(path)
113
+ (File.directory?(path) == false) && (path =~ /\/Pods\//).nil? && ((path =~ /\.(swift|h|m)$/) != nil)
114
+ end
115
+
86
116
  def get_localization_files
87
117
  return @lfiles unless @lfiles.nil?
88
118
  @lfiles = proj_files.map { |file| LFile.try_to_read(file) }.compact
@@ -93,7 +123,7 @@ module Lokale
93
123
 
94
124
  @sfiles_proceeded = 0
95
125
  proj_files do |file|
96
- next unless file.source_file?
126
+ next unless Agent.source_file? file
97
127
 
98
128
  file_content = File.read(file)
99
129
  @macros.each { |macro| macro.read_from file_content }
@@ -101,9 +131,25 @@ module Lokale
101
131
  end
102
132
  end
103
133
 
134
+ def find_project_lfiles
135
+ macro_proj_files = @macros.map { |m| m.project_file }.compact
136
+ return if macro_proj_files.empty?
137
+
138
+ h = Hash.new
139
+ proj_files do |f|
140
+ macro_proj_files.each do |pf|
141
+ h[pf] = f if f.chomp(pf) != f
142
+ end
143
+ end
144
+
145
+ return h
146
+ end
147
+
104
148
  ###
105
149
 
106
150
  def copy_base
151
+ return if Config.get.base_lang.nil? || Config.get.main_lang.nil?
152
+
107
153
  main_lfiles = @lfiles.group_by { |f| f.lang }[Config.get.main_lang].select { |f| f.strings_file? }
108
154
  base_lfiles = @lfiles.group_by { |f| f.lang }[Config.get.base_lang].select { |f| f.strings_file? }
109
155
 
@@ -142,6 +188,20 @@ module Lokale
142
188
  def try_to_import
143
189
  @importer.import_strings(self, @writer)
144
190
  end
191
+
192
+ def write_to_project_file
193
+ strings_to_write = Hash.new { |h, k| h[k] = [] }
194
+ @macros.each do |m|
195
+ next if m.project_file.nil?
196
+ next if m.found_strings.nil?
197
+ strings_to_write[m.project_file] += m.found_strings.keys
198
+ end
199
+
200
+ pfiles_pathes = find_project_lfiles
201
+ strings_to_write.each do |file, lstrings|
202
+ @writer.write_to_project_file(lstrings, pfiles_pathes[file])
203
+ end
204
+ end
145
205
  end
146
206
 
147
207
  class Writer
@@ -175,6 +235,35 @@ module Lokale
175
235
  content.insert(append_at, data_to_append)
176
236
  file.write(content)
177
237
  end
238
+
239
+ def hash_string(hash, depth)
240
+ total_string = ""
241
+ tab = " " * depth
242
+ hash.each do |k, v|
243
+ case v
244
+ when LString
245
+ total_string += tab + "static let #{v.key.split(".")[-1].camelize(:lower)} = NSLocalizedString(\"#{v.key}\", comment:\"#{v.note}\")\n"
246
+ when Hash
247
+ total_string += tab + "\n"
248
+ total_string += tab + "class #{k.camelize} {\n"
249
+ total_string += hash_string(v, depth + 1)
250
+ total_string += tab + "}\n\n"
251
+ end
252
+ end
253
+
254
+ total_string
255
+ end
256
+
257
+ def write_to_project_file(lstrings, file)
258
+ root = Hash.new
259
+ lstrings.each do |ls|
260
+ root.set_by_key_path ls.key.split("."), ls
261
+ end
262
+
263
+ content = "\nextension String {\n\n" + hash_string(root, 1) + "}\n"
264
+ File.write(file, content)
265
+
266
+ end
178
267
  end
179
268
 
180
269
 
@@ -16,6 +16,7 @@ module Lokale
16
16
  def self.export; Action.new(:export) end
17
17
  def self.import; Action.new(:import) end
18
18
  def self.create_config; Action.new(:create_config) end
19
+ def self.add_to_proj; Action.new(:add_to_proj) end
19
20
  end
20
21
 
21
22
  class Config
@@ -54,6 +55,10 @@ module Lokale
54
55
  actions << Action.create_config
55
56
  end
56
57
 
58
+ opts.on("-p", "--add-to-project", "Creates project strings file") do |n|
59
+ actions << Action.add_to_proj
60
+ end
61
+
57
62
  opts.on("-h", "--help", "Prints this help") do
58
63
  puts opts
59
64
  exit
@@ -7,6 +7,7 @@ module Lokale
7
7
 
8
8
  add_macro "NSLocalizedString" do |m|
9
9
  m.localization_file = "Localizable.strings"
10
+ m.project_file = "Strings.swift"
10
11
 
11
12
  m.regex = /NSLocalizedString\\("(.+?)",\\s*comment:\\s*"(.*?)"\\)/
12
13
  m.key_index = 0
@@ -1,21 +1,7 @@
1
1
 
2
- class String
3
- def localization_file?
4
- File.directory?(self) == false &&
5
- (self =~ /\/Pods\//) == nil &&
6
- (self =~ /\.bundle\//) == nil &&
7
- (self =~ /\/(.{1,8})\.lproj\//)
8
- end
9
-
10
- def source_file?
11
- (File.directory?(self) == false) && (self =~ /\/Pods\//).nil? && ((self =~ /\.(swift|h|m)$/) != nil)
12
- end
13
- end
14
-
15
-
16
2
  module Lokale
17
3
  class LString
18
- attr_accessor :key, :str, :note, :target
4
+ attr_accessor :key, :str, :note, :target, :source
19
5
 
20
6
  def initialize(key, str, note, target)
21
7
  @key = key; @str = str; @note = note; @target = target
@@ -46,12 +32,19 @@ module Lokale
46
32
  @lang = $1
47
33
  end
48
34
 
35
+ def self.localization_file?(path)
36
+ File.directory?(path) == false &&
37
+ (path =~ /\/Pods\//) == nil &&
38
+ (path =~ /\.bundle\//) == nil &&
39
+ (path =~ /\/(.{1,8})\.lproj\//)
40
+ end
41
+
49
42
  def self.try_to_read(file_path)
50
- return nil unless file_path.localization_file?
43
+ return nil unless localization_file? file_path
51
44
  LFile.new(file_path)
52
45
  end
53
46
 
54
- def inspect
47
+ def inspect
55
48
  "<LF:#{@lang}/#{full_name}>"
56
49
  end
57
50
 
@@ -84,7 +77,7 @@ module Lokale
84
77
  #
85
78
 
86
79
  class Macro
87
- attr_accessor :regex, :name, :localization_file, :key_index, :note_index
80
+ attr_accessor :regex, :name, :localization_file, :key_index, :note_index, :project_file
88
81
  attr_reader :found_strings
89
82
 
90
83
  def initialize(name)
@@ -113,7 +106,6 @@ module Lokale
113
106
  def total_count
114
107
  @found_strings.values.reduce(:+) || 0
115
108
  end
116
-
117
109
  end
118
110
  end
119
111
 
@@ -6,6 +6,10 @@ class String
6
6
  def lpadded(count=20)
7
7
  "%#{count}.#{count}s" % self
8
8
  end
9
+
10
+ def nil?
11
+ empty?
12
+ end
9
13
  end
10
14
 
11
15
  module Then
@@ -1,3 +1,3 @@
1
1
  module Lokale
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lokale
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Onizhuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-19 00:00:00.000000000 Z
11
+ date: 2017-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xliffle