Strings in Ruby are a bit hard to understand if you're not an English speaker. What does
"hello world"
even mean? I for one didn't study enough English to understand that! But worry no more!
Just get a DeepL auth key and export it as follows:
export DEEPL_AUTH_KEY="your-api-token"
By default this gem uses the free DeepL servers, but you can also configure your own:
export DEEPL_HOST="non-free-host"
Now you can translate it to your language of chose with this gem!
"hello world".translate_to_ja
# 世界よ、ハローだ
What's even better is that you can also save some typing by using the shortened alias such as below:
"hello world".to_ja # or to_fi, to_ru... endless possibilities!
# 世界よ、ハローだ
This gem uses the wonderful DeepL for Ruby as its backend.
https://github.com/wikiti/deepl-rb
Add this line to your application's Gemfile:
gem 'translate_self'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install translate_self
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.
You can assign a string a language:
finn = "Oon suomalainen"
finn.language = 'fi'
This language will be passed on to DeepL when translating.
You can also assign a to-language parameter:
finn = "Oon suomalainen"
finn.to_language = 'en'
This way you can just use translate
to get the string translated to English.
finn.translate
# I am a Finn
Does not work if you freeze the strings.
Now it does!
Just use the classes TranslatableString
(aliased to TString
).
So even if you have a magic comment making all your strings frozen,
you can still use the translate
method like below:
hello = TranslatableString.new('hello', language: 'en', to_language: 'fi')
hello.translate
# "Moi"
OR!
Use it with defrost!
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!
This will defrost strings if they're frozen.
Might be a bit slower, but it's worth it!
Interesting debug sessions guaranteed.
Create a string 5000000 times.
C:\home\sampo\translate_self> ruby benchmark.rb
Rehearsal --------------------------------------------------------
normal string 0.221517 0.000101 0.221618 ( 0.221677)
monkeypatched string 0.267748 0.000000 0.267748 ( 0.267865)
translatable string 1.878466 0.000000 1.878466 ( 1.878497)
----------------------------------------------- total: 2.367832sec
user system total real
normal string 0.225292 0.000000 0.225292 ( 0.225289)
monkeypatched string 0.225690 0.000000 0.225690 ( 0.225740)
translatable string 1.886653 0.000000 1.886653 ( 1.886735)
So... the overhead might be surprisingly low!
If you use the TranslatableString class, there is some overhead.
But if you just hack string it's pretty fast! See benchmark.rb
.
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
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.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the TranslateSelf project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.