Sha256: 0b3235d7eb789e17295d933af9e8ce9050a771b16423ad042520679e53bab8c1

Contents?: true

Size: 1.58 KB

Versions: 142

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true
require 'lhj/helper/trans_helper'

module Lhj
  class Command
    class Trans < Command
      self.summary = '源码中的简繁体转换'
      self.description = '当前目录简繁体转换 `lhj trans --zh-cn`'

      def self.options
        [
          %w[--file-type 文件扩展名,默认为m,h,pch,xib],
          %w[--zh-cn 转成简体中文,默认转成繁体]
        ]
      end

      def initialize(argv)
        @current_path = argv.shift_argument || Dir.pwd
        @file_type = argv.option('file-type', 'm,h,pch,xib')
        @zh_cn = argv.flag?('zh-cn', false)
        super
      end

      def handle
        handler_files
      end

      def handler_files
        search_type = @file_type.gsub('.', '')
        Dir.glob("#{@current_path}/**/*.{#{search_type}}").each do |f|
          handler_file f
        end
      end

      def handler_file(file)
        File.chmod(0o644, file) unless File.writable?(file)
        str = format_file_string(file)
        File.open(file, 'w+') do |f|
          f.write(str)
        end
      end

      def format_file_string(file)
        str = ''
        File.open(file, 'r+') do |f|
          f.each_line do |line|
            str += format_line_string(line)
          end
        end
        str
      end

      def format_line_string(line)
        result = line
        if line =~ /[\u4e00-\u9fa5]/
          result = Lhj::Trans::Helper.instance.trans_zh_cn_str(line) if @zh_cn
          result = Lhj::Trans::Helper.instance.trans_zh_hk_str(line) unless @zh_cn
        end
        result
      end

    end
  end
end

Version data entries

142 entries across 142 versions & 1 rubygems

Version Path
lhj-tools-0.2.83 lib/lhj/command/trans.rb
lhj-tools-0.2.82 lib/lhj/command/trans.rb
lhj-tools-0.2.81 lib/lhj/command/trans.rb
lhj-tools-0.2.80 lib/lhj/command/trans.rb
lhj-tools-0.2.79 lib/lhj/command/trans.rb
lhj-tools-0.2.78 lib/lhj/command/trans.rb
lhj-tools-0.2.77 lib/lhj/command/trans.rb
lhj-tools-0.2.76 lib/lhj/command/trans.rb
lhj-tools-0.2.75 lib/lhj/command/trans.rb
lhj-tools-0.2.74 lib/lhj/command/trans.rb
lhj-tools-0.2.73 lib/lhj/command/trans.rb
lhj-tools-0.2.72 lib/lhj/command/trans.rb
lhj-tools-0.2.71 lib/lhj/command/trans.rb
lhj-tools-0.2.70 lib/lhj/command/trans.rb
lhj-tools-0.2.69 lib/lhj/command/trans.rb
lhj-tools-0.2.68 lib/lhj/command/trans.rb
lhj-tools-0.2.67 lib/lhj/command/trans.rb
lhj-tools-0.2.66 lib/lhj/command/trans.rb
lhj-tools-0.2.65 lib/lhj/command/trans.rb
lhj-tools-0.2.64 lib/lhj/command/trans.rb