konjac 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/konjac.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
+ s.add_runtime_dependency "i18n"
21
22
  s.add_runtime_dependency "nokogiri"
22
23
  s.add_runtime_dependency "term-ansicolor"
23
24
  s.add_runtime_dependency "trollop"
data/lib/konjac/cli.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "term/ansicolor"
2
2
  require "trollop"
3
+ require "yaml"
3
4
 
4
5
  module Konjac
5
6
  module CLI
@@ -8,94 +9,108 @@ module Konjac
8
9
  end
9
10
 
10
11
  class << self
11
- SUB_COMMANDS = {
12
- "add" => "Add a definition to translation memory",
13
- "edit" => "Edit the tags file for a .docx file",
14
- "export" => "Export text tags from a .docx file",
15
- "import" => "Import text tags into a .docx file",
16
- "translate" => "Translate a file according to ~/.konjac/dict.yml"
17
- }
12
+ SUB_COMMANDS = ["add", "edit", "export", "import", "language", "translate"]
18
13
  BANNER = <<-eos
14
+ #{Color.bold { Color.underscore { "KONJAC" } }}
15
+
19
16
  #{Color.bold "%s"}
20
17
 
21
- #{Color.bold "Usage:"}
22
- konjac %s [#{Color.underscore "options"}] <filenames>+%s
23
- where [#{Color.underscore "options"}] are:
18
+ #{Color.bold I18n.t :usage}
19
+ konjac %s [#{Color.underscore I18n.t :options}] <#{I18n.t :filenames}>+%s
20
+ #{I18n.t(:where_options) % Color.underscore(I18n.t(:options))}
24
21
  eos
25
22
 
26
23
  def start
24
+ ARGV << "-h" if ARGV.empty?
27
25
  global_opts = Trollop::options do
28
- version "konjac #{Konjac::VERSION} (c) 2012 Bryan McKelvey"
26
+ version "konjac #{Konjac::VERSION} (c) 2012 " + I18n.t(:me)
29
27
  banner BANNER % [
30
- "konjac is a Ruby command-line utility for translating files " +
31
- "using a YAML wordlist",
32
- "[#{Color.underscore "subcommand"}]",
33
- "\n\nwhere [#{Color.underscore "subcommand"}] is any one of:\n%s\n" %
34
- Konjac::CLI.describe_subcommands
28
+ I18n.t(:banner),
29
+ "[#{Color.underscore I18n.t :subcommand}]",
30
+ "\n\n" + (I18n.t(:where_subcommand) % Color.underscore(I18n.t(:subcommand))) + ("\n%s\n" %
31
+ Konjac::CLI.describe_subcommands)
35
32
  ]
36
- opt :dry_run, "Don't actually do anything", :short => "n"
37
- opt :quiet, "Suppress error messages"
38
- stop_on SUB_COMMANDS.keys
33
+ opt :dry_run, I18n.t(:dry_run, :scope => :opts), :short => "n"
34
+ opt :quiet, I18n.t(:quiet, :scope => :opts)
35
+ opt :version, I18n.t(:version, :scope => :opts)
36
+ opt :help, I18n.t(:help, :scope => :opts)
37
+ stop_on SUB_COMMANDS
39
38
  end
40
39
 
41
40
  # Get subcommand
42
41
  cmd = ARGV.shift
43
- sc_banner = BANNER % [SUB_COMMANDS[cmd], cmd, ""]
42
+ sc_banner = BANNER % [I18n.t(cmd, :scope => :subcommands), cmd, "\n"]
44
43
  cmd_opts = case cmd
45
44
  when "add"
46
45
  opts = Trollop::options do
47
46
  banner sc_banner
48
- opt :original, "The original word or phrase", :type => :string
49
- opt :translation, "The translated word or phrase", :type => :string, :short => :r
50
- opt :from, "The language from which to translate", :type => :string
51
- opt :to, "The language into which to translate", :type => :string
52
- opt :using, "The names of dictionaries to use", :type => :string,
47
+ opt :original, I18n.t(:original, :scope => :opts), :type => :string
48
+ opt :translation, I18n.t(:translation, :scope => :opts), :type => :string, :short => :r
49
+ opt :from, I18n.t(:from, :scope => :opts), :type => :string
50
+ opt :to, I18n.t(:to, :scope => :opts), :type => :string
51
+ opt :using, I18n.t(:using, :scope => :opts), :type => :string,
53
52
  :default => "dict", :multi => true
53
+ opt :help, I18n.t(:help, :scope => :opts)
54
54
  end
55
55
  Dictionary.add_word opts
56
56
  when "edit"
57
57
  Trollop::options do
58
58
  banner sc_banner
59
- opt :editor, "A command indicating which editor to use (e.g. vi %s)"
59
+ opt :editor, I18n.t(:editor, :scope => :opts), :type => :string
60
+ opt :help, I18n.t(:help, :scope => :opts)
60
61
  end
61
62
  Word.edit_docx_tags ARGV, opts
62
63
  when "export"
63
64
  opts = Trollop::options do
64
65
  banner sc_banner
65
- opt :from, "The language from which to translate", :type => :string
66
- opt :to, "The language into which to translate", :type => :string
67
- opt :using, "The names of dictionaries to use", :type => :string,
66
+ opt :from, I18n.t(:from, :scope => :opts), :type => :string
67
+ opt :to, I18n.t(:to, :scope => :opts), :type => :string
68
+ opt :using, I18n.t(:using, :scope => :opts), :type => :string,
68
69
  :default => "dict", :multi => true
70
+ opt :help, I18n.t(:help, :scope => :opts)
69
71
  end
70
72
  Word.export_docx_tags ARGV, opts
71
73
  when "import"
72
74
  Trollop::options do
73
75
  banner sc_banner
76
+ opt :help, I18n.t(:help, :scope => :opts)
74
77
  end
75
78
  Word.import_docx_tags ARGV
79
+ when "language"
80
+ Trollop::options do
81
+ banner sc_banner
82
+ opt :help, I18n.t(:help, :scope => :opts)
83
+ end
84
+ Config.language = ARGV[0]
85
+ Config.save
76
86
  when "translate"
77
87
  opts = Trollop::options do
78
88
  banner sc_banner
79
- opt :from, "The language from which to translate", :type => :string
80
- opt :to, "The language into which to translate", :type => :string
81
- opt :using, "The names of dictionaries to use", :type => :string,
89
+ opt :from, I18n.t(:from, :scope => :opts), :type => :string
90
+ opt :to, I18n.t(:to, :scope => :opts), :type => :string
91
+ opt :using, I18n.t(:using, :scope => :opts), :type => :string,
82
92
  :default => "dict", :multi => true
83
- opt :use_cache, "Use cached dictionary", :default => false
84
- opt :word, "Translate a word or phrase", :default => false
93
+ opt :use_cache, I18n.t(:use_cache, :scope => :opts), :default => false
94
+ opt :word, I18n.t(:word, :scope => :opts), :default => false
95
+ opt :help, I18n.t(:help, :scope => :opts)
85
96
  end
86
97
  result = translate(ARGV, opts)
87
98
  puts result if opts[:word]
88
99
  else
89
- Trollop::die "unknown subcommand #{cmd.inspect}"
100
+ if global_opts[:quiet]
101
+ raise SystemExit
102
+ else
103
+ Trollop::die I18n.t(:unknown_subcommand) % cmd.inspect
104
+ end
90
105
  end
91
106
  end
92
107
 
93
108
  # Describe the subcommands available to the command line interface
94
109
  def describe_subcommands
95
110
  text = []
96
- leftcol_width = SUB_COMMANDS.keys.map(&:length).max
97
- SUB_COMMANDS.each do |sc, desc|
98
- text << " %#{leftcol_width}s: %s" % [sc, desc]
111
+ leftcol_width = SUB_COMMANDS.map(&:length).max
112
+ SUB_COMMANDS.each do |sc|
113
+ text << " %#{leftcol_width}s: %s" % [sc, I18n.t(sc, :scope => :subcommands)]
99
114
  end
100
115
  text.join "\n"
101
116
  end
@@ -0,0 +1,48 @@
1
+ require "yaml"
2
+
3
+ module Konjac
4
+ module Config
5
+ class << self
6
+ CONFIG_PATH = File.expand_path("~/.konjac/config.yml")
7
+
8
+ def load
9
+ Utils.verify_file CONFIG_PATH, "--- {}"
10
+ config = YAML.load_file(CONFIG_PATH)
11
+ config = {} unless config.is_a?(Hash)
12
+ @opts = {
13
+ :language => :en
14
+ }.merge config
15
+
16
+ set_language
17
+ save
18
+ end
19
+
20
+ def language
21
+ @opts[:language]
22
+ end
23
+
24
+ def language=(lang)
25
+ @opts[:language] = Language.find(lang).to_sym || @opts[:language]
26
+ save
27
+ end
28
+
29
+ def save
30
+ File.open(CONFIG_PATH, "w") do |file|
31
+ YAML.dump @opts, file
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def set_language
38
+ I18n.load_path = Dir[File.join(File.dirname(__FILE__), "..", "..", "locales", "*.yml")]
39
+ I18n.default_locale = :en
40
+ if I18n.available_locales.include?(@opts[:language])
41
+ I18n.locale = @opts[:language]
42
+ else
43
+ I18n.locale = I18n.default_locale
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
data/lib/konjac/utils.rb CHANGED
@@ -36,6 +36,16 @@ module Konjac
36
36
  end
37
37
  parsed_files.uniq
38
38
  end
39
+
40
+ # Verifies that a file exists, writing initial content to it if not
41
+ def verify_file(path, initial_content = "")
42
+ unless File.exists?(path)
43
+ FileUtils.mkdir_p File.dirname(path)
44
+ File.open(path, "w") do |file|
45
+ file.puts initial_content
46
+ end
47
+ end
48
+ end
39
49
  end
40
50
  end
41
51
  end
@@ -1,3 +1,3 @@
1
1
  module Konjac
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
data/lib/konjac.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  require "konjac/version"
2
2
  require "konjac/exception"
3
3
  autoload :FileUtils, "fileutils"
4
+ autoload :I18n, "i18n"
4
5
  autoload :Nokogiri, "nokogiri"
5
6
 
6
7
  module Konjac
7
8
  # Set up autoload for all modules
8
9
  autoload :CLI, "konjac/cli"
10
+ autoload :Config, "konjac/config"
9
11
  autoload :Dictionary, "konjac/dictionary"
10
12
  autoload :Language, "konjac/language"
11
13
  autoload :Tag, "konjac/tag"
@@ -13,4 +15,6 @@ module Konjac
13
15
  autoload :Translator, "konjac/translator"
14
16
  autoload :Utils, "konjac/utils"
15
17
  autoload :Word, "konjac/word"
18
+
19
+ Config.load
16
20
  end
data/locales/en.yml ADDED
@@ -0,0 +1,30 @@
1
+ en:
2
+ banner: konjac is a Ruby command-line utility for translating files using a YAML wordlist
3
+ filenames: filenames
4
+ me: Bryan McKelvey
5
+ options: options
6
+ subcommand: subcommand
7
+ unknown_subcommand: "Unknown subcommand: %s"
8
+ usage: "Usage:"
9
+ where_options: "where [%s] are:"
10
+ where_subcommand: "where [%s] is any one of:"
11
+ opts:
12
+ dry_run: Don't actually do anything
13
+ editor: A command to run to pass the file to an editor (e.g. vi %s)
14
+ from: The language from which to translate
15
+ help: Show this message
16
+ original: The untranslated original text
17
+ quiet: Suppress error messages
18
+ to: The language into which to translate
19
+ translation: The translated text
20
+ using: The names of dictionaries to use
21
+ use_cache: Use cached dictionary
22
+ version: Print version and exit
23
+ word: Translate a word or phrase
24
+ subcommands:
25
+ add: Add a definition to translation memory
26
+ edit: Edit the tags file for a .docx file
27
+ export: Export text tags from a .docx file
28
+ import: Import text tags into a .docx file
29
+ language: Sets the default language
30
+ translate: Translate a file, word or phrase
data/locales/ja.yml ADDED
@@ -0,0 +1,30 @@
1
+ ja:
2
+ banner: konjacはYAML辞書でファイルを翻訳するRubyのコマンドライン・ユーティリティです。
3
+ filenames: ファイル名
4
+ me: ブライアン・マッケルビー
5
+ options: オプション
6
+ subcommand: サブコマンド
7
+ unknown_subcommand: 不明なサブコマンド:%s
8
+ usage: 使い方:
9
+ where_options: "使用できる[%s]は"
10
+ where_subcommand: "使用できる[%s]は"
11
+ opts:
12
+ dry_run: 何もしない
13
+ editor: 結果ファイルをエディターで開くコマンド(例:vi %s)
14
+ from: 原文の言語
15
+ help: このメッセージを表示する
16
+ original: 翻訳されなかった元の言語の言葉
17
+ quiet: エラーメッセージを抑止する
18
+ to: 訳文の言語
19
+ translation: 翻訳された言葉
20
+ using: 使用したい辞書の名前
21
+ use_cache: キャッシュした辞書を使用する
22
+ version: バーションを表示し終了する
23
+ word: 文字列を翻訳する
24
+ subcommands:
25
+ add: 翻訳メモリに定義を追加する
26
+ edit: .docxファイルに当たるタグファイルを編集する
27
+ export: .docxファイルからタグを抽出する
28
+ import: .docxファイルにタグをインポートする
29
+ language: 言語を設定する
30
+ translate: ファイル・文字列を翻訳する
data/spec/cli_spec.rb CHANGED
@@ -11,7 +11,7 @@ describe CLI do
11
11
  end
12
12
 
13
13
  it "should fail on an invalid subcommand" do
14
- set_argv "invalid"
14
+ set_argv "invalid", "--quiet"
15
15
  lambda { CLI.start }.should raise_error SystemExit
16
16
  end
17
17
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: konjac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,9 +11,20 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-01-16 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: &70296439418000 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70296439418000
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: nokogiri
16
- requirement: &70097243365180 !ruby/object:Gem::Requirement
27
+ requirement: &70296439417580 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ! '>='
@@ -21,10 +32,10 @@ dependencies:
21
32
  version: '0'
22
33
  type: :runtime
23
34
  prerelease: false
24
- version_requirements: *70097243365180
35
+ version_requirements: *70296439417580
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: term-ansicolor
27
- requirement: &70097243364620 !ruby/object:Gem::Requirement
38
+ requirement: &70296439417160 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: '0'
33
44
  type: :runtime
34
45
  prerelease: false
35
- version_requirements: *70097243364620
46
+ version_requirements: *70296439417160
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: trollop
38
- requirement: &70097243364100 !ruby/object:Gem::Requirement
49
+ requirement: &70296439416740 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: '0'
44
55
  type: :runtime
45
56
  prerelease: false
46
- version_requirements: *70097243364100
57
+ version_requirements: *70296439416740
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: autotest
49
- requirement: &70097243363420 !ruby/object:Gem::Requirement
60
+ requirement: &70296439442300 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: '0'
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *70097243363420
68
+ version_requirements: *70296439442300
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: autotest-fsevent
60
- requirement: &70097243362760 !ruby/object:Gem::Requirement
71
+ requirement: &70296439441680 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ! '>='
@@ -65,10 +76,10 @@ dependencies:
65
76
  version: '0'
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *70097243362760
79
+ version_requirements: *70296439441680
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: autotest-growl
71
- requirement: &70097243362340 !ruby/object:Gem::Requirement
82
+ requirement: &70296439441000 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ! '>='
@@ -76,10 +87,10 @@ dependencies:
76
87
  version: '0'
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *70097243362340
90
+ version_requirements: *70296439441000
80
91
  - !ruby/object:Gem::Dependency
81
92
  name: bundler
82
- requirement: &70097243361900 !ruby/object:Gem::Requirement
93
+ requirement: &70296439440460 !ruby/object:Gem::Requirement
83
94
  none: false
84
95
  requirements:
85
96
  - - ! '>='
@@ -87,10 +98,10 @@ dependencies:
87
98
  version: '0'
88
99
  type: :development
89
100
  prerelease: false
90
- version_requirements: *70097243361900
101
+ version_requirements: *70296439440460
91
102
  - !ruby/object:Gem::Dependency
92
103
  name: rspec
93
- requirement: &70097243361280 !ruby/object:Gem::Requirement
104
+ requirement: &70296439439480 !ruby/object:Gem::Requirement
94
105
  none: false
95
106
  requirements:
96
107
  - - ! '>='
@@ -98,10 +109,10 @@ dependencies:
98
109
  version: '0'
99
110
  type: :development
100
111
  prerelease: false
101
- version_requirements: *70097243361280
112
+ version_requirements: *70296439439480
102
113
  - !ruby/object:Gem::Dependency
103
114
  name: sdoc
104
- requirement: &70097243382540 !ruby/object:Gem::Requirement
115
+ requirement: &70296439437560 !ruby/object:Gem::Requirement
105
116
  none: false
106
117
  requirements:
107
118
  - - ! '>='
@@ -109,7 +120,7 @@ dependencies:
109
120
  version: '0'
110
121
  type: :development
111
122
  prerelease: false
112
- version_requirements: *70097243382540
123
+ version_requirements: *70296439437560
113
124
  description: A Ruby command-line utility for translating files using a YAML wordlist
114
125
  email:
115
126
  - bryan.mckelvey@gmail.com
@@ -129,6 +140,7 @@ files:
129
140
  - konjac.gemspec
130
141
  - lib/konjac.rb
131
142
  - lib/konjac/cli.rb
143
+ - lib/konjac/config.rb
132
144
  - lib/konjac/dictionary.rb
133
145
  - lib/konjac/exception.rb
134
146
  - lib/konjac/language.rb
@@ -138,6 +150,8 @@ files:
138
150
  - lib/konjac/utils.rb
139
151
  - lib/konjac/version.rb
140
152
  - lib/konjac/word.rb
153
+ - locales/en.yml
154
+ - locales/ja.yml
141
155
  - spec/cli_spec.rb
142
156
  - spec/dictionary_spec.rb
143
157
  - spec/language_spec.rb