translate_self 0.2.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +1 -0
- data/CHANGELOG.md +27 -1
- data/Gemfile +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +45 -3
- data/benchmark.rb +22 -0
- data/lib/translate_self.rb +3 -4
- data/lib/translate_self/translatable_string.rb +11 -0
- data/lib/translate_self/translation.rb +7 -4
- data/lib/translate_self/version.rb +1 -1
- metadata +4 -3
- data/.byebug_history +0 -48
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 680f027a575420fab6eb29bbdb7a49a0410e8b6e51adb0775c6dc884539d84bc
|
4
|
+
data.tar.gz: 51afe0c1d56642d3a9b0d6b2578de001da729a10f8c05b2f61a8ed04f383d639
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ead7740d0183cb4959789c62ebebd6ed2bc5a3f528d1b1a4b1e97e71e3607b30dbf52686878dc5bb60e566be3516bcbdd23cfc7e9237cc2ddf0647ac0405a9f2
|
7
|
+
data.tar.gz: 42b7fa5b9d7c68b98fd6884c24808834287c2b0ec63b252970c43bb3fe4083bd5c3a6c3aff5d474fac478ae7b33d8de59dc735c9ad42c9014d1f11167682e65b
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -4,4 +4,30 @@
|
|
4
4
|
## [0.1.0] - 2021-06-02
|
5
5
|
|
6
6
|
- Initial release!
|
7
|
-
You can now translate strings! Be free!
|
7
|
+
You can now translate strings! Be free!
|
8
|
+
|
9
|
+
## [0.2.0] - 2021-06-03
|
10
|
+
|
11
|
+
- Commentation and cleanup of unneeded files.
|
12
|
+
|
13
|
+
## [0.3.0] - 2021-06-03
|
14
|
+
|
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
|
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`.
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
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
|
-
|
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
|
@@ -72,9 +81,42 @@ finn.translate
|
|
72
81
|
# I am a Finn
|
73
82
|
```
|
74
83
|
|
75
|
-
##
|
84
|
+
## Using with `# frozen_string_literal: true`
|
85
|
+
|
86
|
+
~~Does not work if you freeze the strings.~~
|
76
87
|
|
77
|
-
|
88
|
+
Now it does!
|
89
|
+
Just use the classes `TranslatableString` (aliased to `TString`).
|
90
|
+
So even if you have a magic comment making all your strings frozen,
|
91
|
+
you can still use the `translate` method like below:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
hello = TranslatableString.new('hello', language: 'en', to_language: 'fi')
|
95
|
+
hello.translate
|
96
|
+
# "Moi"
|
97
|
+
```
|
98
|
+
|
99
|
+
## Benchmark
|
100
|
+
|
101
|
+
Create a string 5000000 times.
|
102
|
+
|
103
|
+
```shell
|
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
|
+
|
116
|
+
```
|
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`.
|
78
120
|
|
79
121
|
## Development
|
80
122
|
|
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
|
data/lib/translate_self.rb
CHANGED
@@ -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.
|
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
|
-
|
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
|
-
|
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
|
@@ -51,7 +52,9 @@ module Translation
|
|
51
52
|
warn 'No language given!' and return if to_lan.nil?
|
52
53
|
|
53
54
|
response = DeepL.translate text, language, to_lan
|
54
|
-
self.language = response.detected_source_language.downcase if self.language.nil?
|
55
|
-
response.text
|
55
|
+
self.language = response.detected_source_language.downcase if self.language.nil? && !frozen?
|
56
|
+
actual_translation = response.text
|
57
|
+
actual_translation.language = to_lan
|
58
|
+
actual_translation
|
56
59
|
end
|
57
60
|
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
|
+
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-
|
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
|