class Swiftlocalizer::Command
Constants
- COMMENT_REGEX
- NSLOCALIZED_STRING_REGEX
- PRINT_REGEX
Public Class Methods
get_localizable_strings_from_file(file)
click to toggle source
# File lib/swiftlocalizer.rb, line 78 def self.get_localizable_strings_from_file(file) get_localizable_strings_from_lines(File.readlines(file), file) end
get_localizable_strings_from_line(line, file, lineno)
click to toggle source
# File lib/swiftlocalizer.rb, line 90 def self.get_localizable_strings_from_line(line, file, lineno) strings = [] matched = true while matched if line =~ NSLOCALIZED_STRING_REGEX || line =~ PRINT_REGEX || line =~ COMMENT_REGEX line = $' next end if line =~ /"([^"]+)"/ if jstring?($1) string = LocalizableString.new($1, file, lineno) strings << string end line = $' else matched = false end end strings end
get_localizable_strings_from_lines(lines, file)
click to toggle source
# File lib/swiftlocalizer.rb, line 82 def self.get_localizable_strings_from_lines(lines, file) strings = [] lines.each_with_index do |line, index| strings.concat(get_localizable_strings_from_line(line, file, index + 1)) end strings end
get_localized_strings_from_file(file)
click to toggle source
# File lib/swiftlocalizer.rb, line 56 def self.get_localized_strings_from_file(file) get_localized_strings_from_lines(File.readlines(file), file) end
get_localized_strings_from_line(line, file, lineno)
click to toggle source
# File lib/swiftlocalizer.rb, line 68 def self.get_localized_strings_from_line(line, file, lineno) strings = [] while line =~ NSLOCALIZED_STRING_REGEX string = LocalizedString.new($1, $2, file, lineno) strings << string line = $' end strings end
get_localized_strings_from_lines(lines, file)
click to toggle source
# File lib/swiftlocalizer.rb, line 60 def self.get_localized_strings_from_lines(lines, file) strings = [] lines.each_with_index do |line, index| strings.concat(get_localized_strings_from_line(line, file, index + 1)) end strings end
jstring?(str)
click to toggle source
# File lib/swiftlocalizer.rb, line 111 def self.jstring?(str) str.bytesize > str.size end
new(opts, dir)
click to toggle source
# File lib/swiftlocalizer.rb, line 136 def initialize(opts, dir) @opts = opts @dir = dir end
run(argv)
click to toggle source
# File lib/swiftlocalizer.rb, line 115 def self.run(argv) STDOUT.sync = true opts = {} opt = OptionParser.new(argv) opt.banner = "Usage: #{opt.program_name} [-h|--help] [config.yml]" opt.version = Swiftlocalizer::VERSION opt.separator('') opt.separator "Options:" opt.on_head('-h', '--help', 'Show this message') do |v| puts opt.help exit end opt.on('-v', '--verbose', 'Verbose message') {|v| opts[:v] = v} opt.on('-n', '--dry-run', 'Message only') {|v| opts[:n] = v} opt.on('-c', '--check', 'Check localizable strings') {|v| opts[:c] = v} opt.parse!(argv) dir = ARGV.shift || '.' command = Command.new(opts, dir) command.run end
Public Instance Methods
run()
click to toggle source
# File lib/swiftlocalizer.rb, line 141 def run if @opts[:c] check_strings else scan_and_write end end
Private Instance Methods
check_duplicate(strings)
click to toggle source
# File lib/swiftlocalizer.rb, line 185 def check_duplicate(strings) puts "Check duplicate" hash_en = {} strings.each do |str| exists = hash_en[str.en] if exists puts "\t" + exists.to_short_s + " <=> " + str.to_short_s + "\n" else hash_en[str.en] = str end end end
check_strings()
click to toggle source
# File lib/swiftlocalizer.rb, line 150 def check_strings strings = scan_sources do |f| Command.get_localizable_strings_from_file(f) end end
scan_and_write()
click to toggle source
# File lib/swiftlocalizer.rb, line 156 def scan_and_write strings = scan_sources do |f| Command.get_localized_strings_from_file(f) end check_duplicate(strings) basename = 'Localizable.strings' path = File.join(@dir, 'Base.lproj', basename) write_localizable_strings(strings, path, :en) path = File.join(@dir, 'ja.lproj', basename) write_localizable_strings(strings, path, :ja) end
scan_sources() { |f| ... }
click to toggle source
# File lib/swiftlocalizer.rb, line 172 def scan_sources puts "Scan #{@dir}" strings = [] Dir.glob(@dir + '/**/*.swift').each do |f| puts f file_strings = yield(f) puts "retrieve #{file_strings.size} strings" file_strings.each{|str| puts "\t#{str.str_and_lineno}\n"} strings.concat(file_strings) end strings end
write_localizable_strings(strings, path, sym)
click to toggle source
# File lib/swiftlocalizer.rb, line 198 def write_localizable_strings(strings, path, sym) puts "Write #{path}" unless FileTest.file?(path) raise RuntimeError, "#{path} doesn't exist." end File.open(path, 'w') do |f| strings.sort_by{|a| a.key }.each do |str| en = str.send(:en) localized = str.send(sym.to_sym) f.puts "\"#{en}\" = \"#{localized}\";" end end end