Class: ModpackLocalizer::SNBT::Performer

Inherits:
Object
  • Object
show all
Defined in:
lib/modpack_localizer/snbt/performer.rb

Overview

.snbtの翻訳を実行するクラス JpTranslatorFromGptを使用して翻訳を行う

Instance Method Summary collapse

Constructor Details

#initialize(output_logs: true, except_words: [], language: "Japanese", display_help: true) ⇒ ModpackLocalizer::SNBT::Performer

Parameters:

  • output_logs (Boolean) (defaults to: true)

    APIのログを出力するか

  • except_words (Array<String>) (defaults to: [])

    翻訳しない単語

  • language (String) (defaults to: "Japanese")

    言語

  • display_help (Boolean) (defaults to: true)

    ヘルプを表示するか



17
18
19
20
21
22
23
24
25
26
# File 'lib/modpack_localizer/snbt/performer.rb', line 17

def initialize(output_logs: true, except_words: [], language: "Japanese", display_help: true)
  @translator = JpTranslatorFromGpt::Translator.new(
    output_logs: output_logs,
    except_words: except_words,
    exchange_language: language
  )
  @reader, @writer, @progress_bar, @loggable = nil

  ModpackLocalizer.help if display_help
end

Instance Method Details

#perform(file_path, loggable: true) ⇒ void

This method returns an undefined value.

.snbtファイルを翻訳して出力する

Parameters:

  • file_path (String)

    ファイルのパス

  • loggable (Boolean) (defaults to: true)

    翻訳ログを出力するか



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/modpack_localizer/snbt/performer.rb', line 33

def perform(file_path, loggable: true)
  @loggable = loggable
  file_path = File.expand_path(file_path)
  validate_path(file_path)

  @reader, @writer = ModpackLocalizer::SNBT::Reader.new(file_path), ModpackLocalizer::SNBT::Writer.new(file_path)
  results = @reader.extract_all.flatten
  init_progress_bar(file_path, results.length) if @loggable

  results.each do |result|
    result[:text] = @translator.translate(result[:text])
    @writer.overwrites(result)
    @progress_bar.increment if @loggable
  end

  puts "Quest translation completed!"
end

#perform_directory(dir_path: "quests", loggable: true) ⇒ void

This method returns an undefined value.

ディレクトリ内の.snbtファイルを翻訳して出力する

Parameters:

  • dir_path (String) (defaults to: "quests")

    ディレクトリのパス

  • loggable (Boolean) (defaults to: true)

    翻訳ログを出力するか



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/modpack_localizer/snbt/performer.rb', line 56

def perform_directory(dir_path: "quests", loggable: true)
  puts "Performing directory: #{dir_path}" unless loggable
  dir_path = File.expand_path(dir_path)
  validate_path(dir_path)

  # **でサブディレクトリも含めて取得
  snbt_files = Dir.glob("#{dir_path}/**/*.snbt")
  if snbt_files.empty?
    puts "SNBT files not found in: #{dir_path}"
    return
  end

  snbt_files.each { |file_path| perform(file_path, loggable: loggable) }
end

#validate_path(path) ⇒ void

This method returns an undefined value.

ファイルの存在性のバリデーション

Parameters:

  • path (String)

    ファイルのパス

Raises:



75
76
77
78
# File 'lib/modpack_localizer/snbt/performer.rb', line 75

def validate_path(path)
  path = File.expand_path(path)
  raise ModpackLocalizer::PathNotFoundError.new(path) unless File.exist?(path)
end