translate_self 0.4.0 → 0.9.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd77e2863ae0072d53dfee777def3764849c33bdeeab16a67e1065f5dce879f7
4
- data.tar.gz: 239bdf82ef13b4b862eae068b36a97ce0c11abd4d4d8dacb0bd5e6147fa3acd8
3
+ metadata.gz: 06cd6e728398f95604296576f5f3712addcaf0f80d35f1ec9a1a0fd85a8f0f12
4
+ data.tar.gz: a0e2990e3b04699338dc65330e8468c126f5b8a06a4aaf8dd858f28307c75b9b
5
5
  SHA512:
6
- metadata.gz: b66c9bad32762a845c6aa417f672b98a6c11a17cc88d09d6cd404aefac2d7500baab438bbbeb1e99f743b1dfa3d0d173ae89cc69084f521268de793facab2304
7
- data.tar.gz: 3d70dd2c9a5394d2f08a436a79b22ee6e257ac6646d93cb570089537b80c6308957816eeff7d64794fe3dadda8f6459f61a81a903b942ca0f6c0d0650bcae8be
6
+ metadata.gz: 87cd44608ef643c1e449c558cccf36772fed93a6427872ea1741c035e6e43c50c8cb26baca94414fd11910ef867a791860289870a6c6b3d3a439c0d816fadd52
7
+ data.tar.gz: bf6cc3feef26ee424b329ab88ee152fd7004608817fa91b3a5789e62e3fcc77097da98f7efadcf463b011d270c6a158e964ae4803ae7f20fe9c7f25dd56775d9
data/.gitignore CHANGED
@@ -7,6 +7,7 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  /.idea/
10
+ .byebug_history
10
11
 
11
12
  # rspec failure tracking
12
13
  .rspec_status
data/CHANGELOG.md CHANGED
@@ -17,4 +17,26 @@ Seems like you need to update it by hand!
17
17
 
18
18
  ## [0.4.0] - 2021-06-05
19
19
 
20
- - The translated new string now knows what language it is.
20
+ - The translated new string now knows what language it is.
21
+
22
+ ## [0.5.0] - 2021-06-06
23
+
24
+ - Add TranslatableString class to work around frozen string literal magic comment.
25
+ Also will not crash with frozen strings now.
26
+
27
+ ## [0.6.0] - 2021-06-08
28
+
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`.
34
+
35
+ ## [0.8.0] - 2021-06-08
36
+
37
+ - Add option to use the shorter `t_to_language_code` alias.
38
+
39
+ ## [0.9.0] - 2021-06-08
40
+
41
+ # Breaking change from 0.8!
42
+ - `t_to_language_code` was too much typing, so changed it to `to_language_code`.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- translate_self (0.4.0)
4
+ translate_self (0.9.0)
5
5
  deepl-rb
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -19,7 +19,11 @@ Just get a DeepL auth key and export it as follows:
19
19
  export DEEPL_AUTH_KEY="your-api-token"
20
20
  ```
21
21
 
22
- This gem currently hardcodes the free DeepL servers. # TODO
22
+ By default this gem uses the free DeepL servers, but you can also configure your own:
23
+
24
+ ```sh
25
+ export DEEPL_HOST="non-free-host"
26
+ ```
23
27
 
24
28
  Now you can translate it to your language of chose with this gem!
25
29
  ```ruby
@@ -27,6 +31,13 @@ Now you can translate it to your language of chose with this gem!
27
31
  # 世界よ、ハローだ
28
32
  ```
29
33
 
34
+ What's even better is that you can also save some typing by using the shortened alias such as below:
35
+
36
+ ```ruby
37
+ "hello world".to_ja # or to_fi, to_ru... endless possibilities!
38
+ # 世界よ、ハローだ
39
+ ```
40
+
30
41
  This gem uses the wonderful DeepL for Ruby as its backend.
31
42
 
32
43
  https://github.com/wikiti/deepl-rb
@@ -77,9 +88,42 @@ finn.translate
77
88
  # I am a Finn
78
89
  ```
79
90
 
80
- ## Beware!
91
+ ## Using with `# frozen_string_literal: true`
92
+
93
+ ~~Does not work if you freeze the strings.~~
94
+
95
+ Now it does!
96
+ Just use the classes `TranslatableString` (aliased to `TString`).
97
+ So even if you have a magic comment making all your strings frozen,
98
+ you can still use the `translate` method like below:
99
+
100
+ ```ruby
101
+ hello = TranslatableString.new('hello', language: 'en', to_language: 'fi')
102
+ hello.translate
103
+ # "Moi"
104
+ ```
105
+
106
+ ## Benchmark
81
107
 
82
- Does not work if you freeze the strings.
108
+ Create a string 5000000 times.
109
+
110
+ ```shell
111
+ C:\home\sampo\translate_self> ruby benchmark.rb
112
+ Rehearsal --------------------------------------------------------
113
+ normal string 0.221517 0.000101 0.221618 ( 0.221677)
114
+ monkeypatched string 0.267748 0.000000 0.267748 ( 0.267865)
115
+ translatable string 1.878466 0.000000 1.878466 ( 1.878497)
116
+ ----------------------------------------------- total: 2.367832sec
117
+
118
+ user system total real
119
+ normal string 0.225292 0.000000 0.225292 ( 0.225289)
120
+ monkeypatched string 0.225690 0.000000 0.225690 ( 0.225740)
121
+ translatable string 1.886653 0.000000 1.886653 ( 1.886735)
122
+
123
+ ```
124
+ So... the overhead might be surprisingly low!
125
+ If you use the TranslatableString class, there is some overhead.
126
+ But if you just hack string it's pretty fast! See `benchmark.rb`.
83
127
 
84
128
  ## Development
85
129
 
data/benchmark.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'benchmark'
2
+
3
+ n = 5000000
4
+ Benchmark.bmbm do |x|
5
+ x.report("normal string") do
6
+ n.times do
7
+ 'hello'
8
+ end
9
+ end
10
+ x.report('monkeypatched string') do
11
+ require 'translate_self'
12
+ n.times do
13
+ 'hello'
14
+ end
15
+ end
16
+ x.report('translatable string') do
17
+ require 'translate_self'
18
+ n.times do
19
+ TranslatableString.new('hello')
20
+ end
21
+ end
22
+ end
@@ -1,14 +1,13 @@
1
1
  require_relative 'translate_self/version'
2
2
  require_relative 'translate_self/translation'
3
+ require_relative 'translate_self/translatable_string'
3
4
  require 'ext/string'
4
5
  require 'deepl'
5
6
 
6
7
  # Dangerously include translation methods to strings!
7
8
  module TranslateSelf
8
- include Translation
9
- class Error < StandardError; end
10
9
  DeepL.configure do |config|
11
- config.host = 'https://api-free.deepl.com' # Default value is 'https://api.deepl.com'
10
+ config.auth_key = ENV['DEEPL_AUTH_KEY']
11
+ config.host = ENV['DEEPL_HOST'] || 'https://api-free.deepl.com' # Default value is 'https://api.deepl.com'
12
12
  end
13
- 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
14
13
  end
@@ -0,0 +1,11 @@
1
+ # A class that let's you translate strings with
2
+ # the language and to_language params even if you happen to
3
+ # freeze them. Aliased to TString.
4
+ class TranslatableString < String
5
+ def initialize(str, language: nil, to_language: nil)
6
+ super str
7
+ self.language = language
8
+ self.to_language = to_language
9
+ end
10
+ end
11
+ TString = TranslatableString
@@ -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. \
@@ -32,17 +33,18 @@ module Translation
32
33
  replace translate
33
34
  end
34
35
 
35
- # Translates the string itself to a language the user wants to translate it to. \
36
+ # Translates the string itself to a language the user wants to translate it to.
36
37
  # Sample usage:
37
38
  # 'hello'.translate_to_fi
38
39
  # # Hei
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
47
+ alias_method "to_#{lan}", "translate_to_#{lan}"
46
48
  end
47
49
 
48
50
  private
@@ -51,7 +53,7 @@ module Translation
51
53
  warn 'No language given!' and return if to_lan.nil?
52
54
 
53
55
  response = DeepL.translate text, language, to_lan
54
- self.language = response.detected_source_language.downcase if self.language.nil?
56
+ self.language = response.detected_source_language.downcase if self.language.nil? && !frozen?
55
57
  actual_translation = response.text
56
58
  actual_translation.language = to_lan
57
59
  actual_translation
@@ -1,3 +1,3 @@
1
1
  module TranslateSelf
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '0.9.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.4.0
4
+ version: 0.9.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-05 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
@@ -75,7 +75,6 @@ executables: []
75
75
  extensions: []
76
76
  extra_rdoc_files: []
77
77
  files:
78
- - ".byebug_history"
79
78
  - ".github/workflows/main.yml"
80
79
  - ".gitignore"
81
80
  - ".rspec"
@@ -87,10 +86,12 @@ files:
87
86
  - LICENSE.txt
88
87
  - README.md
89
88
  - Rakefile
89
+ - benchmark.rb
90
90
  - bin/console
91
91
  - bin/setup
92
92
  - lib/ext/string.rb
93
93
  - lib/translate_self.rb
94
+ - lib/translate_self/translatable_string.rb
94
95
  - lib/translate_self/translation.rb
95
96
  - lib/translate_self/version.rb
96
97
  - translate_self.gemspec
data/.byebug_history DELETED
@@ -1,48 +0,0 @@
1
- c
2
- @language
3
- c
4
- @language
5
- self
6
- c
7
- self
8
- c
9
- self
10
- c
11
- self
12
- c
13
- suomalainen.language
14
- suomalainen.language = 'fi'
15
- suomalainen.language
16
- c
17
- suomalainen.language
18
- c
19
- self.language
20
- c
21
- self.language
22
- self.language = 'fi'
23
- response.detected_source_language
24
- self.language.nil?
25
- self.language
26
- self
27
- c
28
- available_languages.include?(name.to_s[-2..-1])
29
- available_languages.include?(name.to_s[-2..-1].to_sym)
30
- exit
31
- c
32
- available_languages.include?(name.to_s[-2..-1].to_sym)
33
- available_languages.include?(name.to_s[-2..-1])
34
- name.to_s[-2..-1]
35
- name
36
- c
37
- languages.map(&:code).map(&:downcase)
38
- languages.map(&:code)
39
- languages.map(&:code.downcase.to_sym)
40
- languages = DeepL.languages
41
- c
42
- self
43
- self.replace 'kon'
44
- self.instance_methods(false)
45
- self.attributes
46
- self = 'kon'
47
- language
48
- self