translate_self 0.8.0 → 2.0.1

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: 7434934d496f14c0767302238db2351de6ca166dd8a137dbc53dac618198877d
4
- data.tar.gz: b9973d4a3cb4b18129b847aec4cd95dca02ebe59d45c5180623d5d06b3146739
3
+ metadata.gz: 3440e7e27417b8402613c3b0a8b9ffefbbe3af690a6dc9600a847a56266c77b3
4
+ data.tar.gz: fa0310235e61ef1dc3571ef737d124743ef6d85560e8cc5777624e1e9d67c008
5
5
  SHA512:
6
- metadata.gz: b9f482c4f31471092e275417f5449ed87d7527ab85ed0067039b72c138dc1ec73698298621a303fa499279a9072fa4971fa67c39cc949b2acd601be70ad29bbc
7
- data.tar.gz: 00bcd9a92d53dbd96a4672d038bd9fac0820986b03c2f7cf1052a2ee4d6f37fde69fc2d09f36ca2a9a36fe9cc93bb2f0c6c7fe20cede22398bbc695466e41109
6
+ metadata.gz: 8473f84cbb40f018afce32d74ed4bf4a57a51de31d7f893debd079eb36f733bbdbfa2aa5eb2230afa977609b0ee11479a1e7f7b518f25eec3f02d0a59f91bc1c
7
+ data.tar.gz: 9d1d285395839950a413c38b94723392d281701ca1eb3986d5a39f5839bb559d865b85a3c7f78268af55ed5c80cadad63787baebecca42a48ab64f3bca8306b0
data/.rubocop.yml CHANGED
@@ -1,3 +1,5 @@
1
+ require: rubocop-rspec
2
+
1
3
  AllCops:
2
4
  TargetRubyVersion: 2.5
3
5
  NewCops: enable
@@ -13,8 +15,22 @@ Style/StringLiteralsInInterpolation:
13
15
  Style/FrozenStringLiteralComment:
14
16
  Enabled: false
15
17
 
18
+ Metrics/BlockLength:
19
+ Exclude:
20
+ - '*.gemspec'
21
+ - 'spec/**/*'
22
+
16
23
  Layout/LineLength:
17
24
  Max: 120
18
25
 
19
26
  Style/AsciiComments:
20
27
  Enabled: false
28
+
29
+ RSpec/ExpectActual:
30
+ Enabled: false
31
+
32
+ RSpec/InstanceVariable:
33
+ Enabled: false
34
+
35
+ RSpec/MultipleExpectations:
36
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,37 +1,57 @@
1
- ## [Unreleased]
2
- - Nothing new yet.
1
+ ## [2.0.1] - 2021-11-07
2
+ - Finally a way to get around those pesky frozen strings!
3
+ - By default, [defrost](https://github.com/tenderlove/defrost) is not loaded. You can turn this off by setting `TranslateSelf.defrosting = true`.
4
+ - See `spec/frozen_strings_spec.rb` for more details and exciting ways of using it.
5
+ - Using with frozen_string_literal: true is a great way to troll your coworkers.
3
6
 
4
- ## [0.1.0] - 2021-06-02
7
+ ## [1.0.1] - 2021-07-13
5
8
 
6
- - Initial release!
7
- You can now translate strings! Be free!
9
+ - I'm reading Polished Ruby, and I just learned that it's faster to pass symbols than strings sometimes,
10
+ so as this library is very performance-oriented, symbols it is!
8
11
 
9
- ## [0.2.0] - 2021-06-03
12
+ ## [1.0.0] - 2021-06-11
10
13
 
11
- - Commentation and cleanup of unneeded files.
14
+ - Refactored the translation part a bit.
15
+ - Now you can translate strings that are over 30 kilobyte!
16
+ I couldn't test this too much because of the limits on the free plan,
17
+ but if you can test this feature, I'd be really happy to hear how it is working for you.
12
18
 
13
- ## [0.3.0] - 2021-06-03
14
19
 
15
- - Gemfile.lock was not automatically updated, testing it a bit.
16
- Seems like you need to update it by hand!
20
+ ## [0.9.0] - 2021-06-08
17
21
 
18
- ## [0.4.0] - 2021-06-05
22
+ ## [0.8.0] - 2021-06-08
19
23
 
20
- - The translated new string now knows what language it is.
24
+ - Add option to use the shorter `t_to_language_code` alias.
25
+ ## [0.7.0] - 2021-06-08
26
+
27
+ - Get rid of duplicated code. Update `benchmark.rb`.
28
+
29
+ ## [0.6.0] - 2021-06-08
30
+
31
+ - Add option to use ENV variable DEEPL_HOST
32
+
33
+ # Breaking change from 0.8!
34
+ - `t_to_language_code` was too much typing, so changed it to `to_language_code`.
21
35
 
22
36
  ## [0.5.0] - 2021-06-06
23
37
 
24
38
  - Add TranslatableString class to work around frozen string literal magic comment.
25
39
  Also will not crash with frozen strings now.
26
40
 
27
- ## [0.6.0] - 2021-06-08
41
+ ## [0.4.0] - 2021-06-05
28
42
 
29
- - Add option to use ENV variable DEEPL_HOST
43
+ - The translated new string now knows what language it is.
44
+ ## [0.3.0] - 2021-06-03
30
45
 
31
- ## [0.7.0] - 2021-06-08
46
+ - Gemfile.lock was not automatically updated, testing it a bit.
47
+ Seems like you need to update it by hand!
32
48
 
33
- - Get rid of duplicated code. Update `benchmark.rb`.
34
49
 
35
- ## [0.8.0] - 2021-06-08
50
+ ## [0.2.0] - 2021-06-03
51
+
52
+ - Commentation and cleanup of unneeded files.
53
+
54
+ ## [0.1.0] - 2021-06-02
36
55
 
37
- - Add option to use the shorter `t_to_language_code` alias.
56
+ - Initial release!
57
+ You can now translate strings! Be free!
data/Gemfile.lock CHANGED
@@ -1,20 +1,27 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- translate_self (0.8.0)
4
+ translate_self (2.0.1)
5
5
  deepl-rb
6
+ defrost (~> 1.0)
7
+ dry-configurable (~> 0.1.3)
6
8
 
7
9
  GEM
8
10
  remote: https://rubygems.org/
9
11
  specs:
10
12
  ast (2.4.2)
11
- deepl-rb (2.3.0)
13
+ byebug (11.1.3)
14
+ concurrent-ruby (1.1.9)
15
+ deepl-rb (2.3.1)
16
+ defrost (1.0.0)
12
17
  diff-lcs (1.4.4)
13
- parallel (1.20.1)
14
- parser (3.0.1.1)
18
+ dry-configurable (0.1.7)
19
+ concurrent-ruby (~> 1.0)
20
+ parallel (1.21.0)
21
+ parser (3.0.2.0)
15
22
  ast (~> 2.4.1)
16
23
  rainbow (3.0.0)
17
- rake (13.0.3)
24
+ rake (13.0.6)
18
25
  regexp_parser (2.1.1)
19
26
  rexml (3.2.5)
20
27
  rspec (3.10.0)
@@ -29,29 +36,33 @@ GEM
29
36
  rspec-mocks (3.10.2)
30
37
  diff-lcs (>= 1.2.0, < 2.0)
31
38
  rspec-support (~> 3.10.0)
32
- rspec-support (3.10.2)
33
- rubocop (1.16.0)
39
+ rspec-support (3.10.3)
40
+ rubocop (1.22.3)
34
41
  parallel (~> 1.10)
35
42
  parser (>= 3.0.0.0)
36
43
  rainbow (>= 2.2.2, < 4.0)
37
44
  regexp_parser (>= 1.8, < 3.0)
38
45
  rexml
39
- rubocop-ast (>= 1.7.0, < 2.0)
46
+ rubocop-ast (>= 1.12.0, < 2.0)
40
47
  ruby-progressbar (~> 1.7)
41
48
  unicode-display_width (>= 1.4.0, < 3.0)
42
- rubocop-ast (1.7.0)
49
+ rubocop-ast (1.12.0)
43
50
  parser (>= 3.0.1.1)
51
+ rubocop-rspec (2.5.0)
52
+ rubocop (~> 1.19)
44
53
  ruby-progressbar (1.11.0)
45
- unicode-display_width (2.0.0)
54
+ unicode-display_width (2.1.0)
46
55
 
47
56
  PLATFORMS
48
57
  x86_64-linux
49
58
 
50
59
  DEPENDENCIES
60
+ byebug
51
61
  rake (~> 13.0)
52
62
  rspec (~> 3.2)
53
63
  rubocop (~> 1.7)
64
+ rubocop-rspec
54
65
  translate_self!
55
66
 
56
67
  BUNDLED WITH
57
- 2.2.17
68
+ 2.2.22
data/README.md CHANGED
@@ -34,7 +34,7 @@ Now you can translate it to your language of chose with this gem!
34
34
  What's even better is that you can also save some typing by using the shortened alias such as below:
35
35
 
36
36
  ```ruby
37
- "hello world".t_to_ja # or t_to_fi, t_to_ru... endless possibilities!
37
+ "hello world".to_ja # or to_fi, to_ru... endless possibilities!
38
38
  # 世界よ、ハローだ
39
39
  ```
40
40
 
@@ -59,11 +59,12 @@ Or install it yourself as:
59
59
 
60
60
  $ gem install translate_self
61
61
 
62
- ## Usage
63
62
 
64
- Currently available languages:
63
+ ## Supported languages:
64
+
65
+ Supported languages are: Bulgarian, Chinese, Czech, Danish, Dutch, English, Estonian, Finnish, French, German, Greek, Hungarian, Italian, Japanese, Latvian, Lithuanian, Polish, Portuguese, Romanian, Russian, Slovak, Slovenian, Spanish, and Swedish.
65
66
 
66
- [:bg, :cs, :da, :de, :el, :en, :es, :et, :fi, :fr, :hu, :it, :ja, :lt, :lv, :nl, :pl, :pt, :ro, :ru, :sk, :sl, :sv, :zh]
67
+ ## Usage
67
68
 
68
69
  You can assign a string a language:
69
70
 
@@ -103,6 +104,13 @@ hello.translate
103
104
  # "Moi"
104
105
  ```
105
106
 
107
+ OR!
108
+ Use it with [defrost](https://github.com/tenderlove/defrost)!
109
+ By default, defrost is not loaded. You can turn this on by setting `TranslateSelf.defrosting = true`, and after that, no need to worry about frozen strings!
110
+ This will defrost strings if they're frozen.
111
+ Might be a bit slower, but it's worth it!
112
+ Interesting debug sessions guaranteed.
113
+
106
114
  ## Benchmark
107
115
 
108
116
  Create a string 5000000 times.
@@ -133,7 +141,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
133
141
 
134
142
  ## Contributing
135
143
 
136
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/translate_self. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/translate_self/blob/master/CODE_OF_CONDUCT.md).
144
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sampokuokkanen/translate_self. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/translate_self/blob/master/CODE_OF_CONDUCT.md).
137
145
 
138
146
  ## License
139
147
 
data/benchmark.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'benchmark'
2
2
 
3
- n = 5000000
3
+ n = 5_000_000
4
4
  Benchmark.bmbm do |x|
5
- x.report("normal string") do
5
+ x.report('normal string') do
6
6
  n.times do
7
7
  'hello'
8
8
  end
@@ -19,4 +19,4 @@ Benchmark.bmbm do |x|
19
19
  TranslatableString.new('hello')
20
20
  end
21
21
  end
22
- end
22
+ end
data/lib/ext/string.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require_relative '../translate_self/translation'
2
-
2
+ # String extension used by TranslateSelf
3
3
  class String
4
4
  include Translation
5
5
  end
@@ -2,11 +2,20 @@ 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
6
- attr_accessor :language, :to_language
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
6
+ attr_accessor :language
7
+
8
+ def to_language
9
+ @to_language || 'fi'
10
+ end
11
+
12
+ def to_language=(language)
13
+ defroster if needs_defrosting?
14
+ @to_language = LANGUAGES.include?(language) ? language : 'fi'
15
+ end
7
16
 
8
17
  def available_languages
9
- @@languages
18
+ LANGUAGES
10
19
  end
11
20
 
12
21
  # Translates self to the desired language. \
@@ -18,7 +27,7 @@ module Translation
18
27
  # # 'Hei'
19
28
  # @return [String] a new and shiny translated string!
20
29
  def translate
21
- call_deepl(self, language, to_language)
30
+ call_deepl(language, to_language)
22
31
  end
23
32
 
24
33
  # Replaces self with the translation. \
@@ -30,6 +39,7 @@ module Translation
30
39
  # # 'Hei'
31
40
  # @return [String] self replaced with the new translation
32
41
  def translate!
42
+ defroster if needs_defrosting?
33
43
  replace translate
34
44
  end
35
45
 
@@ -40,20 +50,58 @@ module Translation
40
50
  #
41
51
  # @param [String] the language to translate to, e.g. "fi"
42
52
  # @return [String] the contents translated to another language
43
- @@languages.each do |lan|
44
- define_method("translate_to_#{lan}") do |language = lan|
45
- call_deepl(self, self.language, language)
53
+ LANGUAGES.each do |lan|
54
+ define_method("translate_to_#{lan}".to_sym) do |language = lan|
55
+ call_deepl(self.language, language)
46
56
  end
47
- alias_method "t_to_#{lan}", "translate_to_#{lan}"
57
+ alias_method "to_#{lan}".to_sym, "translate_to_#{lan}".to_sym
48
58
  end
49
59
 
50
60
  private
51
61
 
52
- def call_deepl(text, language = self.language, to_lan = to_language)
62
+ def needs_defrosting?
63
+ frozen? && TranslateSelf.config.defrosting
64
+ end
65
+
66
+ def defroster
67
+ require 'defrost'
68
+ defrost
69
+ end
70
+
71
+ def break_up(to_lan)
72
+ sentences = break_text_into_sentences
73
+ sentences.length.times.map.with_index do |_i, i|
74
+ sentences[i].send "translate_to_#{to_lan}"
75
+ end.join(' ')
76
+ end
77
+
78
+ def break_text_into_sentences
79
+ last_letter = last_letter_of_self
80
+ tr_count = (bytesize / 25_000.to_f).ceil
81
+ sentences = split('.')
82
+ number_of_sentences = sentences.length / tr_count
83
+ current_count = 0
84
+ tr_count.times.map do |num|
85
+ txt = sentences[current_count..(current_count + number_of_sentences)].join('.')
86
+ current_count += number_of_sentences.next
87
+ if num == tr_count.pred
88
+ txt + last_letter
89
+ else
90
+ txt
91
+ end
92
+ end
93
+ end
94
+
95
+ def last_letter_of_self
96
+ end_with?('.') ? '.' : ''
97
+ end
98
+
99
+ def call_deepl(language = self.language, to_lan = to_language)
53
100
  warn 'No language given!' and return if to_lan.nil?
101
+ return break_up(to_lan) if bytesize > 30_000
54
102
 
55
- response = DeepL.translate text, language, to_lan
56
- self.language = response.detected_source_language.downcase if self.language.nil? && !frozen?
103
+ response = DeepL.translate self, language, to_lan
104
+ self.language = response.detected_source_language.downcase if !frozen? || (needs_defrosting? && defroster)
57
105
  actual_translation = response.text
58
106
  actual_translation.language = to_lan
59
107
  actual_translation
@@ -1,3 +1,3 @@
1
1
  module TranslateSelf
2
- VERSION = '0.8.0'.freeze
2
+ VERSION = '2.0.1'.freeze
3
3
  end
@@ -3,9 +3,12 @@ require_relative 'translate_self/translation'
3
3
  require_relative 'translate_self/translatable_string'
4
4
  require 'ext/string'
5
5
  require 'deepl'
6
+ require 'dry-configurable'
6
7
 
7
8
  # Dangerously include translation methods to strings!
8
9
  module TranslateSelf
10
+ extend Dry::Configurable
11
+ setting :defrosting, default: false
9
12
  DeepL.configure do |config|
10
13
  config.auth_key = ENV['DEEPL_AUTH_KEY']
11
14
  config.host = ENV['DEEPL_HOST'] || 'https://api-free.deepl.com' # Default value is 'https://api.deepl.com'