Sha256: 0b3235d7eb789e17295d933af9e8ce9050a771b16423ad042520679e53bab8c1

Contents?: true

Size: 1.58 KB

Versions: 144

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

144 entries across 144 versions & 1 rubygems

Version Path
lhj-tools-0.2.25 lib/lhj/command/trans.rb
lhj-tools-0.2.24 lib/lhj/command/trans.rb
lhj-tools-0.2.23 lib/lhj/command/trans.rb
lhj-tools-0.2.21 lib/lhj/command/trans.rb
lhj-tools-0.2.20 lib/lhj/command/trans.rb
lhj-tools-0.2.19 lib/lhj/command/trans.rb
lhj-tools-0.2.18 lib/lhj/command/trans.rb
lhj-tools-0.2.17 lib/lhj/command/trans.rb
lhj-tools-0.2.16 lib/lhj/command/trans.rb
lhj-tools-0.2.15 lib/lhj/command/trans.rb
lhj-tools-0.2.14 lib/lhj/command/trans.rb
lhj-tools-0.2.13 lib/lhj/command/trans.rb
lhj-tools-0.2.12 lib/lhj/command/trans.rb
lhj-tools-0.2.11 lib/lhj/command/trans.rb
lhj-tools-0.2.10 lib/lhj/command/trans.rb
lhj-tools-0.2.9 lib/lhj/command/trans.rb
lhj-tools-0.2.8 lib/lhj/command/trans.rb
lhj-tools-0.2.7 lib/lhj/command/trans.rb
lhj-tools-0.2.6 lib/lhj/command/trans.rb
lhj-tools-0.2.5 lib/lhj/command/trans.rb