translate_self 1.0.1 → 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: e2f3c2abce98a2adee409991a78dd742815c7e0b1bfb380505180d4f5c52a5b9
4
- data.tar.gz: 3feb43446a4549581992be68d65389e9d3e9ca2f55381a7a5d2e56df6f1573ea
3
+ metadata.gz: 3440e7e27417b8402613c3b0a8b9ffefbbe3af690a6dc9600a847a56266c77b3
4
+ data.tar.gz: fa0310235e61ef1dc3571ef737d124743ef6d85560e8cc5777624e1e9d67c008
5
5
  SHA512:
6
- metadata.gz: 5518f3c6d39c0d8a2b3e79fbd9c4302c85c714d12a075de90b59fd647444e08902bd317e3e90d20e99d50ed94280833aa75e5beb381498f5f04636f4f63cb7ef
7
- data.tar.gz: 5266142227b3bccaffb4a1d9bee31f7ac27b3d8f8f5779c0a1efd99ed9d9a80b5ced3a1b463a4db373717c34a237d9aa016c6cef12bcbb0d59bfcb383482fea4
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,54 +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!
17
-
18
- ## [0.4.0] - 2021-06-05
20
+ ## [0.9.0] - 2021-06-08
19
21
 
20
- - The translated new string now knows what language it is.
22
+ ## [0.8.0] - 2021-06-08
21
23
 
22
- ## [0.5.0] - 2021-06-06
24
+ - Add option to use the shorter `t_to_language_code` alias.
25
+ ## [0.7.0] - 2021-06-08
23
26
 
24
- - Add TranslatableString class to work around frozen string literal magic comment.
25
- Also will not crash with frozen strings now.
27
+ - Get rid of duplicated code. Update `benchmark.rb`.
26
28
 
27
29
  ## [0.6.0] - 2021-06-08
28
30
 
29
31
  - Add option to use ENV variable DEEPL_HOST
30
32
 
31
- ## [0.7.0] - 2021-06-08
33
+ # Breaking change from 0.8!
34
+ - `t_to_language_code` was too much typing, so changed it to `to_language_code`.
32
35
 
33
- - Get rid of duplicated code. Update `benchmark.rb`.
36
+ ## [0.5.0] - 2021-06-06
34
37
 
35
- ## [0.8.0] - 2021-06-08
38
+ - Add TranslatableString class to work around frozen string literal magic comment.
39
+ Also will not crash with frozen strings now.
36
40
 
37
- - Add option to use the shorter `t_to_language_code` alias.
41
+ ## [0.4.0] - 2021-06-05
38
42
 
39
- ## [0.9.0] - 2021-06-08
43
+ - The translated new string now knows what language it is.
44
+ ## [0.3.0] - 2021-06-03
40
45
 
41
- # Breaking change from 0.8!
42
- - `t_to_language_code` was too much typing, so changed it to `to_language_code`.
46
+ - Gemfile.lock was not automatically updated, testing it a bit.
47
+ Seems like you need to update it by hand!
43
48
 
44
- ## [1.0.0] - 2021-06-11
45
49
 
46
- - Refactored the translation part a bit.
47
- - Now you can translate strings that are over 30 kilobyte!
48
- I couldn't test this too much because of the limits on the free plan,
49
- but if you can test this feature, I'd be really happy to hear how it is working for you.
50
+ ## [0.2.0] - 2021-06-03
50
51
 
51
- ## [1.0.1] - 2021-07-13
52
+ - Commentation and cleanup of unneeded files.
52
53
 
53
- - I'm reading Polished Ruby, and I just learned that it's faster to pass symbols than strings sometimes,
54
- so as this library is very performance-oriented, symbols it is!
54
+ ## [0.1.0] - 2021-06-02
55
+
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 (1.0.1)
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
@@ -104,6 +104,13 @@ hello.translate
104
104
  # "Moi"
105
105
  ```
106
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
+
107
114
  ## Benchmark
108
115
 
109
116
  Create a string 5000000 times.
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. \
@@ -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,7 +50,7 @@ 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|
53
+ LANGUAGES.each do |lan|
44
54
  define_method("translate_to_#{lan}".to_sym) do |language = lan|
45
55
  call_deepl(self.language, language)
46
56
  end
@@ -49,6 +59,15 @@ module Translation
49
59
 
50
60
  private
51
61
 
62
+ def needs_defrosting?
63
+ frozen? && TranslateSelf.config.defrosting
64
+ end
65
+
66
+ def defroster
67
+ require 'defrost'
68
+ defrost
69
+ end
70
+
52
71
  def break_up(to_lan)
53
72
  sentences = break_text_into_sentences
54
73
  sentences.length.times.map.with_index do |_i, i|
@@ -57,11 +76,7 @@ module Translation
57
76
  end
58
77
 
59
78
  def break_text_into_sentences
60
- last_letter = if self[-1] == '.'
61
- '.'
62
- else
63
- ''
64
- end
79
+ last_letter = last_letter_of_self
65
80
  tr_count = (bytesize / 25_000.to_f).ceil
66
81
  sentences = split('.')
67
82
  number_of_sentences = sentences.length / tr_count
@@ -69,16 +84,24 @@ module Translation
69
84
  tr_count.times.map do |num|
70
85
  txt = sentences[current_count..(current_count + number_of_sentences)].join('.')
71
86
  current_count += number_of_sentences.next
72
- txt + last_letter if num == tr_count.pred
87
+ if num == tr_count.pred
88
+ txt + last_letter
89
+ else
90
+ txt
91
+ end
73
92
  end
74
93
  end
75
94
 
95
+ def last_letter_of_self
96
+ end_with?('.') ? '.' : ''
97
+ end
98
+
76
99
  def call_deepl(language = self.language, to_lan = to_language)
77
100
  warn 'No language given!' and return if to_lan.nil?
78
101
  return break_up(to_lan) if bytesize > 30_000
79
102
 
80
103
  response = DeepL.translate self, language, to_lan
81
- self.language = response.detected_source_language.downcase if self.language.nil? && !frozen?
104
+ self.language = response.detected_source_language.downcase if !frozen? || (needs_defrosting? && defroster)
82
105
  actual_translation = response.text
83
106
  actual_translation.language = to_lan
84
107
  actual_translation
@@ -1,3 +1,3 @@
1
1
  module TranslateSelf
2
- VERSION = '1.0.1'.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'
@@ -29,9 +29,13 @@ Gem::Specification.new do |spec|
29
29
 
30
30
  # Uncomment to register a new dependency of your gem
31
31
  spec.add_dependency 'deepl-rb'
32
+ spec.add_dependency 'defrost', '~> 1.0'
33
+ spec.add_dependency 'dry-configurable', '~> 0.1.3'
32
34
  spec.add_development_dependency 'rake', '~> 13.0'
33
35
  spec.add_development_dependency 'rspec', '~> 3.2'
34
36
  spec.add_development_dependency 'rubocop', '~> 1.7'
37
+ spec.add_development_dependency 'rubocop-rspec'
38
+ spec.add_development_dependency 'byebug'
35
39
 
36
40
  # For more information and examples about making a new gem, checkout our
37
41
  # guide at: https://bundler.io/guides/creating_gem.html
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: 1.0.1
4
+ version: 2.0.1
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-07-13 00:00:00.000000000 Z
11
+ date: 2021-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deepl-rb
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: defrost
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-configurable
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.3
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rake
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +94,34 @@ dependencies:
66
94
  - - "~>"
67
95
  - !ruby/object:Gem::Version
68
96
  version: '1.7'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
69
125
  description: |-
70
126
  Let the strings do the job for you. No need to do any of the manual work! \
71
127
  The strings translate themselves!! Translate to Japanese, English, Finnish...
@@ -118,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
174
  - !ruby/object:Gem::Version
119
175
  version: '0'
120
176
  requirements: []
121
- rubygems_version: 3.2.15
177
+ rubygems_version: 3.2.22
122
178
  signing_key:
123
179
  specification_version: 4
124
180
  summary: TranslateSelf lets strings translate themselves.