translate_self 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 48a0238d6e09ae494d46da95732914c18c77882cc8a077d1c522a124c985cd62
4
- data.tar.gz: 84a3fe6ae48a310dabe1fa8952773dd6234cebd03f7b370fd57b1aca44e8add7
3
+ metadata.gz: 680f027a575420fab6eb29bbdb7a49a0410e8b6e51adb0775c6dc884539d84bc
4
+ data.tar.gz: 51afe0c1d56642d3a9b0d6b2578de001da729a10f8c05b2f61a8ed04f383d639
5
5
  SHA512:
6
- metadata.gz: 13d585250ec562d77cd50d7d175c214ae136069d806d0b584e919a020f7769c1b2df09e7610639d18fd3909065173d02fd44f062f0f6f25636c64b019d4345fc
7
- data.tar.gz: cf6a84eb4c137084fa8b96f3c6e5dbab24e88499da2b638280eceaef615818b175d363d8de9d70529c82f243760a2750a3d8b7081f69a2e56bf583f7553d72a8
6
+ metadata.gz: ead7740d0183cb4959789c62ebebd6ed2bc5a3f528d1b1a4b1e97e71e3607b30dbf52686878dc5bb60e566be3516bcbdd23cfc7e9237cc2ddf0647ac0405a9f2
7
+ data.tar.gz: 42b7fa5b9d7c68b98fd6884c24808834287c2b0ec63b252970c43bb3fe4083bd5c3a6c3aff5d474fac478ae7b33d8de59dc735c9ad42c9014d1f11167682e65b
data/CHANGELOG.md CHANGED
@@ -26,4 +26,8 @@ Seems like you need to update it by hand!
26
26
 
27
27
  ## [0.6.0] - 2021-06-08
28
28
 
29
- - Add option to use ENV variable DEEPL_HOST
29
+ - Add option to use ENV variable DEEPL_HOST
30
+
31
+ ## [0.7.0] - 2021-06-08
32
+
33
+ - Get rid of duplicated code. Update `benchmark.rb`.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- translate_self (0.6.0)
4
+ translate_self (0.7.0)
5
5
  deepl-rb
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -101,17 +101,22 @@ hello.translate
101
101
  Create a string 5000000 times.
102
102
 
103
103
  ```shell
104
- C:\home\sampo\translate_self> ruby benchmark.rb
105
- Rehearsal -------------------------------------------------------
106
- normal string 0.252435 0.000000 0.252435 ( 0.252490)
107
- translatable string 0.299736 0.000000 0.299736 ( 0.299789)
108
- ---------------------------------------------- total: 0.552171sec
109
-
110
- user system total real
111
- normal string 0.237091 0.000000 0.237091 ( 0.237091)
112
- translatable string 0.237607 0.000000 0.237607 ( 0.237611)
104
+ C:\home\sampo\translate_self> ruby benchmark.rb
105
+ Rehearsal --------------------------------------------------------
106
+ normal string 0.221517 0.000101 0.221618 ( 0.221677)
107
+ monkeypatched string 0.267748 0.000000 0.267748 ( 0.267865)
108
+ translatable string 1.878466 0.000000 1.878466 ( 1.878497)
109
+ ----------------------------------------------- total: 2.367832sec
110
+
111
+ user system total real
112
+ normal string 0.225292 0.000000 0.225292 ( 0.225289)
113
+ monkeypatched string 0.225690 0.000000 0.225690 ( 0.225740)
114
+ translatable string 1.886653 0.000000 1.886653 ( 1.886735)
115
+
113
116
  ```
114
117
  So... the overhead might be surprisingly low!
118
+ If you use the TranslatableString class, there is some overhead.
119
+ But if you just hack string it's pretty fast! See `benchmark.rb`.
115
120
 
116
121
  ## Development
117
122
 
data/benchmark.rb CHANGED
@@ -2,15 +2,21 @@ require 'benchmark'
2
2
 
3
3
  n = 5000000
4
4
  Benchmark.bmbm do |x|
5
- x.report("string") do
5
+ x.report("normal string") do
6
6
  n.times do
7
7
  'hello'
8
8
  end
9
9
  end
10
- x.report('translatable string') do
10
+ x.report('monkeypatched string') do
11
11
  require 'translate_self'
12
12
  n.times do
13
13
  'hello'
14
14
  end
15
15
  end
16
+ x.report('translatable string') do
17
+ require 'translate_self'
18
+ n.times do
19
+ TranslatableString.new('hello')
20
+ end
21
+ end
16
22
  end
@@ -6,11 +6,8 @@ require 'deepl'
6
6
 
7
7
  # Dangerously include translation methods to strings!
8
8
  module TranslateSelf
9
- include Translation
10
- class Error < StandardError; end
11
9
  DeepL.configure do |config|
12
10
  config.auth_key = ENV['DEEPL_AUTH_KEY']
13
11
  config.host = ENV['DEEPL_HOST'] || 'https://api-free.deepl.com' # Default value is 'https://api.deepl.com'
14
12
  end
15
- AVAILABLE_LANGUAGES = %w[bg cs da de el en es et fi fr hu it ja lt lv nl pl pt ro ru sk sl sv zh].freeze
16
13
  end
@@ -3,7 +3,7 @@
3
3
  # freeze them. Aliased to TString.
4
4
  class TranslatableString < String
5
5
  def initialize(str, language: nil, to_language: nil)
6
- replace str
6
+ super str
7
7
  self.language = language
8
8
  self.to_language = to_language
9
9
  end
@@ -2,10 +2,11 @@ require 'deepl'
2
2
 
3
3
  # The part where the actual translation happens.
4
4
  module Translation
5
+ @@languages = %w[bg cs da de el en es et fi fr hu it ja lt lv nl pl pt ro ru sk sl sv zh].freeze
5
6
  attr_accessor :language, :to_language
6
7
 
7
8
  def available_languages
8
- TranslateSelf::AVAILABLE_LANGUAGES
9
+ @@languages
9
10
  end
10
11
 
11
12
  # Translates self to the desired language. \
@@ -39,7 +40,7 @@ module Translation
39
40
  #
40
41
  # @param [String] the language to translate to, e.g. "fi"
41
42
  # @return [String] the contents translated to another language
42
- %w[bg cs da de el en es et fi fr hu it ja lt lv nl pl pt ro ru sk sl sv zh].each do |lan|
43
+ @@languages.each do |lan|
43
44
  define_method("translate_to_#{lan}") do |language = lan|
44
45
  call_deepl(self, self.language, language)
45
46
  end
@@ -1,3 +1,3 @@
1
1
  module TranslateSelf
2
- VERSION = '0.6.0'.freeze
2
+ VERSION = '0.7.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translate_self
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sampo Kuokkanen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-07 00:00:00.000000000 Z
11
+ date: 2021-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deepl-rb