translate_self 0.1.0 → 0.6.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: a1bdc270fdc320b15fa9fd8249fa24c38591c2aa1210bb6622a37b7268781636
4
- data.tar.gz: 854e6cbb249caf59708b4ba93dd5ee1f085996bfdd14b377163ef579bf68c650
3
+ metadata.gz: 48a0238d6e09ae494d46da95732914c18c77882cc8a077d1c522a124c985cd62
4
+ data.tar.gz: 84a3fe6ae48a310dabe1fa8952773dd6234cebd03f7b370fd57b1aca44e8add7
5
5
  SHA512:
6
- metadata.gz: 37922d9018c6e61366b60896c1475d21149c290a8a29c95fe5d74c79d568313b63b5013a6f5af91eb4bc251d256ca8d2755acd330fd446808c3b608f42e77a91
7
- data.tar.gz: 9bd11a1a8952635f2a3db3628b8d7a26593fe39096cfd59a8234f6db59ab3da7b6327f91e914644536a9c4019467edbb60c3651248f69fbd3800356d82b542f0
6
+ metadata.gz: 13d585250ec562d77cd50d7d175c214ae136069d806d0b584e919a020f7769c1b2df09e7610639d18fd3909065173d02fd44f062f0f6f25636c64b019d4345fc
7
+ data.tar.gz: cf6a84eb4c137084fa8b96f3c6e5dbab24e88499da2b638280eceaef615818b175d363d8de9d70529c82f243760a2750a3d8b7081f69a2e56bf583f7553d72a8
@@ -2,6 +2,9 @@ name: Ruby
2
2
 
3
3
  on: [push,pull_request]
4
4
 
5
+ env:
6
+ DEEPL_AUTH_KEY: ${{ secrets.DEEPL_AUTH_KEY }}
7
+
5
8
  jobs:
6
9
  build:
7
10
  runs-on: ubuntu-latest
@@ -12,5 +15,5 @@ jobs:
12
15
  with:
13
16
  ruby-version: 3.0.1
14
17
  bundler-cache: true
15
- - name: Run the default task
16
- run: bundle exec rake
18
+ - name: Run the RSpec tests
19
+ run: bundle exec rake spec
data/.gitignore CHANGED
@@ -6,6 +6,8 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /.idea/
10
+ .byebug_history
9
11
 
10
12
  # rspec failure tracking
11
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
@@ -1,5 +1,29 @@
1
1
  ## [Unreleased]
2
+ - Nothing new yet.
2
3
 
3
4
  ## [0.1.0] - 2021-06-02
4
5
 
5
- - Initial release
6
+ - Initial release!
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
data/Gemfile CHANGED
@@ -4,11 +4,3 @@ source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in translate_self.gemspec
6
6
  gemspec
7
-
8
- gem 'byebug'
9
-
10
- gem 'rake', '~> 13.0'
11
-
12
- gem 'rspec', '~> 3.0'
13
-
14
- gem 'rubocop', '~> 1.7'
data/Gemfile.lock CHANGED
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- translate_self (0.1.0)
4
+ translate_self (0.6.0)
5
+ deepl-rb
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
9
10
  ast (2.4.2)
10
- byebug (11.1.3)
11
11
  deepl-rb (2.3.0)
12
12
  diff-lcs (1.4.4)
13
13
  parallel (1.20.1)
@@ -48,10 +48,8 @@ PLATFORMS
48
48
  x86_64-linux
49
49
 
50
50
  DEPENDENCIES
51
- byebug
52
- deepl-rb
53
51
  rake (~> 13.0)
54
- rspec (~> 3.0)
52
+ rspec (~> 3.2)
55
53
  rubocop (~> 1.7)
56
54
  translate_self!
57
55
 
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
@@ -72,9 +81,37 @@ finn.translate
72
81
  # I am a Finn
73
82
  ```
74
83
 
75
- ## Beware!
84
+ ## Using with `# frozen_string_literal: true`
85
+
86
+ ~~Does not work if you freeze the strings.~~
87
+
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
76
100
 
77
- Does not work if you freeze the strings.
101
+ Create a string 5000000 times.
102
+
103
+ ```shell
104
+ C:\home\sampo\translate_self> ruby benchmark.rb
105
+ Rehearsal -------------------------------------------------------
106
+ normal string 0.252435 0.000000 0.252435 ( 0.252490)
107
+ translatable string 0.299736 0.000000 0.299736 ( 0.299789)
108
+ ---------------------------------------------- total: 0.552171sec
109
+
110
+ user system total real
111
+ normal string 0.237091 0.000000 0.237091 ( 0.237091)
112
+ translatable string 0.237607 0.000000 0.237607 ( 0.237611)
113
+ ```
114
+ So... the overhead might be surprisingly low!
78
115
 
79
116
  ## Development
80
117
 
data/benchmark.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'benchmark'
2
+
3
+ n = 5000000
4
+ Benchmark.bmbm do |x|
5
+ x.report("string") do
6
+ n.times do
7
+ 'hello'
8
+ end
9
+ end
10
+ x.report('translatable string') do
11
+ require 'translate_self'
12
+ n.times do
13
+ 'hello'
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,6 @@
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
 
@@ -8,7 +9,8 @@ module TranslateSelf
8
9
  include Translation
9
10
  class Error < StandardError; end
10
11
  DeepL.configure do |config|
11
- config.host = 'https://api-free.deepl.com' # Default value is 'https://api.deepl.com'
12
+ config.auth_key = ENV['DEEPL_AUTH_KEY']
13
+ config.host = ENV['DEEPL_HOST'] || 'https://api-free.deepl.com' # Default value is 'https://api.deepl.com'
12
14
  end
13
15
  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
16
  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
+ replace str
7
+ self.language = language
8
+ self.to_language = to_language
9
+ end
10
+ end
11
+ TString = TranslatableString
@@ -1,4 +1,3 @@
1
- require 'byebug'
2
1
  require 'deepl'
3
2
 
4
3
  # The part where the actual translation happens.
@@ -9,14 +8,37 @@ module Translation
9
8
  TranslateSelf::AVAILABLE_LANGUAGES
10
9
  end
11
10
 
11
+ # Translates self to the desired language. \
12
+ # Sample usage:
13
+ # hello = 'hello'\
14
+ # hello.to_language = 'fi'
15
+ # moi = hello.translate
16
+ # pp moi
17
+ # # 'Hei'
18
+ # @return [String] a new and shiny translated string!
12
19
  def translate
13
20
  call_deepl(self, language, to_language)
14
21
  end
15
22
 
23
+ # Replaces self with the translation. \
24
+ # Sample usage:
25
+ # hello = 'hello'\
26
+ # hello.to_language = 'fi'
27
+ # hello.translate!
28
+ # pp hello
29
+ # # 'Hei'
30
+ # @return [String] self replaced with the new translation
16
31
  def translate!
17
32
  replace translate
18
33
  end
19
34
 
35
+ # Translates the string itself to a language the user wants to translate it to. \
36
+ # Sample usage:
37
+ # 'hello'.translate_to_fi
38
+ # # Hei
39
+ #
40
+ # @param [String] the language to translate to, e.g. "fi"
41
+ # @return [String] the contents translated to another language
20
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|
21
43
  define_method("translate_to_#{lan}") do |language = lan|
22
44
  call_deepl(self, self.language, language)
@@ -26,10 +48,12 @@ module Translation
26
48
  private
27
49
 
28
50
  def call_deepl(text, language = self.language, to_lan = to_language)
29
- raise 'No language given!' if to_lan.nil?
51
+ warn 'No language given!' and return if to_lan.nil?
30
52
 
31
53
  response = DeepL.translate text, language, to_lan
32
- self.language = response.detected_source_language.downcase if self.language.nil?
33
- response.text
54
+ self.language = response.detected_source_language.downcase if self.language.nil? && !frozen?
55
+ actual_translation = response.text
56
+ actual_translation.language = to_lan
57
+ actual_translation
34
58
  end
35
59
  end
@@ -1,3 +1,3 @@
1
1
  module TranslateSelf
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.6.0'.freeze
3
3
  end
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.summary = 'TranslateSelf lets strings translate themselves. '
12
12
  spec.description = 'Let the strings do the job for you. No need to do any of the manual work! \
13
13
  The strings translate themselves!! Translate to Japanese, English, Finnish...'
14
- spec.homepage = 'https://github.com/sampokuokkanen/rails_material_design_icons'
14
+ spec.homepage = 'https://github.com/sampokuokkanen/translate_self'
15
15
  spec.license = 'MIT'
16
16
  spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
17
17
 
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.1.0
4
+ version: 0.6.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-02 00:00:00.000000000 Z
11
+ date: 2021-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deepl-rb
@@ -75,14 +75,8 @@ 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
- - ".idea/.gitignore"
82
- - ".idea/misc.xml"
83
- - ".idea/modules.xml"
84
- - ".idea/translate_self.iml"
85
- - ".idea/vcs.xml"
86
80
  - ".rspec"
87
81
  - ".rubocop.yml"
88
82
  - CHANGELOG.md
@@ -92,20 +86,22 @@ files:
92
86
  - LICENSE.txt
93
87
  - README.md
94
88
  - Rakefile
89
+ - benchmark.rb
95
90
  - bin/console
96
91
  - bin/setup
97
92
  - lib/ext/string.rb
98
93
  - lib/translate_self.rb
94
+ - lib/translate_self/translatable_string.rb
99
95
  - lib/translate_self/translation.rb
100
96
  - lib/translate_self/version.rb
101
97
  - translate_self.gemspec
102
- homepage: https://github.com/sampokuokkanen/rails_material_design_icons
98
+ homepage: https://github.com/sampokuokkanen/translate_self
103
99
  licenses:
104
100
  - MIT
105
101
  metadata:
106
- homepage_uri: https://github.com/sampokuokkanen/rails_material_design_icons
107
- source_code_uri: https://github.com/sampokuokkanen/rails_material_design_icons
108
- changelog_uri: https://github.com/sampokuokkanen/rails_material_design_icons
102
+ homepage_uri: https://github.com/sampokuokkanen/translate_self
103
+ source_code_uri: https://github.com/sampokuokkanen/translate_self
104
+ changelog_uri: https://github.com/sampokuokkanen/translate_self
109
105
  post_install_message:
110
106
  rdoc_options: []
111
107
  require_paths:
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
data/.idea/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- # Default ignored files
2
- /shelf/
3
- /workspace.xml
4
- # Datasource local storage ignored files
5
- /dataSources/
6
- /dataSources.local.xml
7
- # Editor-based HTTP Client requests
8
- /httpRequests/
data/.idea/misc.xml DELETED
@@ -1,4 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectRootManager" version="2" project-jdk-name="rbenv: 3.0.1" project-jdk-type="RUBY_SDK" />
4
- </project>
data/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/translate_self.iml" filepath="$PROJECT_DIR$/.idea/translate_self.iml" />
6
- </modules>
7
- </component>
8
- </project>
@@ -1,36 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="RUBY_MODULE" version="4">
3
- <component name="ModuleRunConfigurationManager">
4
- <shared />
5
- </component>
6
- <component name="NewModuleRootManager">
7
- <content url="file://$MODULE_DIR$">
8
- <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
9
- <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
10
- <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
- </content>
12
- <orderEntry type="inheritedJdk" />
13
- <orderEntry type="sourceFolder" forTests="false" />
14
- <orderEntry type="library" scope="PROVIDED" name="ast (v2.4.2, rbenv: 3.0.1) [gem]" level="application" />
15
- <orderEntry type="library" scope="PROVIDED" name="bundler (v2.2.17, rbenv: 3.0.1) [gem]" level="application" />
16
- <orderEntry type="library" scope="PROVIDED" name="byebug (v11.1.3, rbenv: 3.0.1) [gem]" level="application" />
17
- <orderEntry type="library" scope="PROVIDED" name="deepl-rb (v2.3.0, rbenv: 3.0.1) [gem]" level="application" />
18
- <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.4.4, rbenv: 3.0.1) [gem]" level="application" />
19
- <orderEntry type="library" scope="PROVIDED" name="parallel (v1.20.1, rbenv: 3.0.1) [gem]" level="application" />
20
- <orderEntry type="library" scope="PROVIDED" name="parser (v3.0.1.1, rbenv: 3.0.1) [gem]" level="application" />
21
- <orderEntry type="library" scope="PROVIDED" name="rainbow (v3.0.0, rbenv: 3.0.1) [gem]" level="application" />
22
- <orderEntry type="library" scope="PROVIDED" name="rake (v13.0.3, rbenv: 3.0.1) [gem]" level="application" />
23
- <orderEntry type="library" scope="PROVIDED" name="rbs (v1.0.4, rbenv: 3.0.1) [gem]" level="application" />
24
- <orderEntry type="library" scope="PROVIDED" name="regexp_parser (v2.1.1, rbenv: 3.0.1) [gem]" level="application" />
25
- <orderEntry type="library" scope="PROVIDED" name="rexml (v3.2.5, rbenv: 3.0.1) [gem]" level="application" />
26
- <orderEntry type="library" scope="PROVIDED" name="rspec (v3.10.0, rbenv: 3.0.1) [gem]" level="application" />
27
- <orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.10.1, rbenv: 3.0.1) [gem]" level="application" />
28
- <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.10.1, rbenv: 3.0.1) [gem]" level="application" />
29
- <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.10.2, rbenv: 3.0.1) [gem]" level="application" />
30
- <orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.10.2, rbenv: 3.0.1) [gem]" level="application" />
31
- <orderEntry type="library" scope="PROVIDED" name="rubocop (v1.16.0, rbenv: 3.0.1) [gem]" level="application" />
32
- <orderEntry type="library" scope="PROVIDED" name="rubocop-ast (v1.7.0, rbenv: 3.0.1) [gem]" level="application" />
33
- <orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.11.0, rbenv: 3.0.1) [gem]" level="application" />
34
- <orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v2.0.0, rbenv: 3.0.1) [gem]" level="application" />
35
- </component>
36
- </module>
data/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
- </component>
6
- </project>