translate_self 0.3.0 → 0.8.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: 3327edb424a2cb958b0c393f3dfe5bc057cf925d1f92043ec0a6be71df3add4a
4
- data.tar.gz: 3dfd89f27f7c48dccba36900301c76fb7a2adcfe534a4586936185d0fe4a3f89
3
+ metadata.gz: 7434934d496f14c0767302238db2351de6ca166dd8a137dbc53dac618198877d
4
+ data.tar.gz: b9973d4a3cb4b18129b847aec4cd95dca02ebe59d45c5180623d5d06b3146739
5
5
  SHA512:
6
- metadata.gz: 4d66138e6ee261acbb8054501e3d99c15b95f08fc8e0250b96da0448282b7241243bcf83f15429ba316019a1bf0be30987a238083df011391313b89e1e44c984
7
- data.tar.gz: fcce9b4ab0af49522be5e68128dd436ddc16e1fd733440bb4a2ca1ca6b5c01985dd5b45882420909d1d6c42584277606677c031951f1128fa4324ea4104b4091
6
+ metadata.gz: b9f482c4f31471092e275417f5449ed87d7527ab85ed0067039b72c138dc1ec73698298621a303fa499279a9072fa4971fa67c39cc949b2acd601be70ad29bbc
7
+ data.tar.gz: 00bcd9a92d53dbd96a4672d038bd9fac0820986b03c2f7cf1052a2ee4d6f37fde69fc2d09f36ca2a9a36fe9cc93bb2f0c6c7fe20cede22398bbc695466e41109
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/.rubocop.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.5
3
+ NewCops: enable
3
4
 
4
5
  Style/StringLiterals:
5
6
  Enabled: true
data/CHANGELOG.md CHANGED
@@ -15,3 +15,23 @@ You can now translate strings! Be free!
15
15
  - Gemfile.lock was not automatically updated, testing it a bit.
16
16
  Seems like you need to update it by hand!
17
17
 
18
+ ## [0.4.0] - 2021-06-05
19
+
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.
data/Gemfile CHANGED
@@ -3,4 +3,4 @@
3
3
  source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in translate_self.gemspec
6
- gemspec
6
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- translate_self (0.3.0)
4
+ translate_self (0.8.0)
5
5
  deepl-rb
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # TranslateSelf
2
2
 
3
+ [![Gem Version](http://img.shields.io/gem/v/translate_self.svg)](https://rubygems.org/gems/translate_self)
4
+ [![Gem Downloads](https://img.shields.io/gem/dt/translate_self.svg)](https://rubygems.org/gems/translate_self)
5
+
3
6
  Strings in Ruby are a bit hard to understand if you're not an English speaker.
4
7
  What does
5
8
  ```ruby
@@ -8,13 +11,19 @@ What does
8
11
  even mean? I for one didn't study enough English to understand that!
9
12
  But worry no more!
10
13
 
14
+ [![asciicast](https://asciinema.org/a/TJU6H5iPopKrUwqwYnLUX3Y2o.svg)](https://asciinema.org/a/TJU6H5iPopKrUwqwYnLUX3Y2o)
15
+
11
16
  Just get a DeepL auth key and export it as follows:
12
17
 
13
18
  ```sh
14
19
  export DEEPL_AUTH_KEY="your-api-token"
15
20
  ```
16
21
 
17
- 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
+ ```
18
27
 
19
28
  Now you can translate it to your language of chose with this gem!
20
29
  ```ruby
@@ -22,6 +31,13 @@ Now you can translate it to your language of chose with this gem!
22
31
  # 世界よ、ハローだ
23
32
  ```
24
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".t_to_ja # or t_to_fi, t_to_ru... endless possibilities!
38
+ # 世界よ、ハローだ
39
+ ```
40
+
25
41
  This gem uses the wonderful DeepL for Ruby as its backend.
26
42
 
27
43
  https://github.com/wikiti/deepl-rb
@@ -72,9 +88,42 @@ finn.translate
72
88
  # I am a Finn
73
89
  ```
74
90
 
75
- ## Beware!
91
+ ## Using with `# frozen_string_literal: true`
76
92
 
77
- Does not work if you freeze the strings.
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
107
+
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`.
78
127
 
79
128
  ## Development
80
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 "t_to_#{lan}", "translate_to_#{lan}"
46
48
  end
47
49
 
48
50
  private
@@ -51,7 +53,9 @@ 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?
55
- response.text
56
+ self.language = response.detected_source_language.downcase if self.language.nil? && !frozen?
57
+ actual_translation = response.text
58
+ actual_translation.language = to_lan
59
+ actual_translation
56
60
  end
57
61
  end
@@ -1,3 +1,3 @@
1
1
  module TranslateSelf
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.8.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.3.0
4
+ version: 0.8.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-03 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