instant18n 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.env +1 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +125 -0
- data/LICENSE.txt +21 -0
- data/README.md +118 -0
- data/Rakefile +12 -0
- data/instant18n.gemspec +41 -0
- data/lib/i18n_extensions.rb +102 -0
- data/lib/instant18n/version.rb +5 -0
- data/lib/instant18n.rb +9 -0
- data/log/development.log +0 -0
- data/sig/instant18n.rbs +4 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 465dcacde7df00da6da5fc38d2f586ed6b2b5b64fd1ab766806019a635e097c4
|
4
|
+
data.tar.gz: 6c3d7cd6b3a276dddb874a7cddfcb299d4239b576ab194b17bb5be5929dff017
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cfe944f349d421c653ac423ef61bfb355a4f3c6c3f805a9a620d34f5b6bea44c20cfb04d212754d0a46f2db74f1f50df26bce32e0417778e317c4cfdeeffcaea
|
7
|
+
data.tar.gz: 94499545a01fd9ba0946a1675530532a093416147eeab06be1027b765db6ea9c403f33d76f1a8769651715549f8df9382ba3c9c22602b6c0e681f22795dd3806
|
data/.env
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
OPENAI_ACCESS_TOKEN=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
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at obiefernandez@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in instant18n.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rspec", "~> 3.0"
|
11
|
+
|
12
|
+
gem "rubocop", "~> 1.21"
|
13
|
+
|
14
|
+
group :development, :test do
|
15
|
+
gem 'dotenv'
|
16
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
instant18n (0.1.0)
|
5
|
+
actionview
|
6
|
+
activesupport
|
7
|
+
railties
|
8
|
+
ruby-openai (~> 4.0.0)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
actionpack (7.0.4.3)
|
14
|
+
actionview (= 7.0.4.3)
|
15
|
+
activesupport (= 7.0.4.3)
|
16
|
+
rack (~> 2.0, >= 2.2.0)
|
17
|
+
rack-test (>= 0.6.3)
|
18
|
+
rails-dom-testing (~> 2.0)
|
19
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
20
|
+
actionview (7.0.4.3)
|
21
|
+
activesupport (= 7.0.4.3)
|
22
|
+
builder (~> 3.1)
|
23
|
+
erubi (~> 1.4)
|
24
|
+
rails-dom-testing (~> 2.0)
|
25
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
26
|
+
activesupport (7.0.4.3)
|
27
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
28
|
+
i18n (>= 1.6, < 2)
|
29
|
+
minitest (>= 5.1)
|
30
|
+
tzinfo (~> 2.0)
|
31
|
+
ast (2.4.2)
|
32
|
+
builder (3.2.4)
|
33
|
+
concurrent-ruby (1.2.2)
|
34
|
+
crass (1.0.6)
|
35
|
+
diff-lcs (1.5.0)
|
36
|
+
dotenv (2.8.1)
|
37
|
+
erubi (1.12.0)
|
38
|
+
faraday (2.7.4)
|
39
|
+
faraday-net_http (>= 2.0, < 3.1)
|
40
|
+
ruby2_keywords (>= 0.0.4)
|
41
|
+
faraday-multipart (1.0.4)
|
42
|
+
multipart-post (~> 2)
|
43
|
+
faraday-net_http (3.0.2)
|
44
|
+
i18n (1.12.0)
|
45
|
+
concurrent-ruby (~> 1.0)
|
46
|
+
json (2.6.3)
|
47
|
+
loofah (2.20.0)
|
48
|
+
crass (~> 1.0.2)
|
49
|
+
nokogiri (>= 1.5.9)
|
50
|
+
method_source (1.0.0)
|
51
|
+
minitest (5.18.0)
|
52
|
+
multipart-post (2.3.0)
|
53
|
+
nokogiri (1.14.3-arm64-darwin)
|
54
|
+
racc (~> 1.4)
|
55
|
+
parallel (1.23.0)
|
56
|
+
parser (3.2.2.1)
|
57
|
+
ast (~> 2.4.1)
|
58
|
+
racc (1.6.2)
|
59
|
+
rack (2.2.6.4)
|
60
|
+
rack-test (2.1.0)
|
61
|
+
rack (>= 1.3)
|
62
|
+
rails-dom-testing (2.0.3)
|
63
|
+
activesupport (>= 4.2.0)
|
64
|
+
nokogiri (>= 1.6)
|
65
|
+
rails-html-sanitizer (1.5.0)
|
66
|
+
loofah (~> 2.19, >= 2.19.1)
|
67
|
+
railties (7.0.4.3)
|
68
|
+
actionpack (= 7.0.4.3)
|
69
|
+
activesupport (= 7.0.4.3)
|
70
|
+
method_source
|
71
|
+
rake (>= 12.2)
|
72
|
+
thor (~> 1.0)
|
73
|
+
zeitwerk (~> 2.5)
|
74
|
+
rainbow (3.1.1)
|
75
|
+
rake (13.0.6)
|
76
|
+
regexp_parser (2.8.0)
|
77
|
+
rexml (3.2.5)
|
78
|
+
rspec (3.12.0)
|
79
|
+
rspec-core (~> 3.12.0)
|
80
|
+
rspec-expectations (~> 3.12.0)
|
81
|
+
rspec-mocks (~> 3.12.0)
|
82
|
+
rspec-core (3.12.2)
|
83
|
+
rspec-support (~> 3.12.0)
|
84
|
+
rspec-expectations (3.12.3)
|
85
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
86
|
+
rspec-support (~> 3.12.0)
|
87
|
+
rspec-mocks (3.12.5)
|
88
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
89
|
+
rspec-support (~> 3.12.0)
|
90
|
+
rspec-support (3.12.0)
|
91
|
+
rubocop (1.50.2)
|
92
|
+
json (~> 2.3)
|
93
|
+
parallel (~> 1.10)
|
94
|
+
parser (>= 3.2.0.0)
|
95
|
+
rainbow (>= 2.2.2, < 4.0)
|
96
|
+
regexp_parser (>= 1.8, < 3.0)
|
97
|
+
rexml (>= 3.2.5, < 4.0)
|
98
|
+
rubocop-ast (>= 1.28.0, < 2.0)
|
99
|
+
ruby-progressbar (~> 1.7)
|
100
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
101
|
+
rubocop-ast (1.28.0)
|
102
|
+
parser (>= 3.2.1.0)
|
103
|
+
ruby-openai (4.0.0)
|
104
|
+
faraday (>= 1)
|
105
|
+
faraday-multipart (>= 1)
|
106
|
+
ruby-progressbar (1.13.0)
|
107
|
+
ruby2_keywords (0.0.5)
|
108
|
+
thor (1.2.1)
|
109
|
+
tzinfo (2.0.6)
|
110
|
+
concurrent-ruby (~> 1.0)
|
111
|
+
unicode-display_width (2.4.2)
|
112
|
+
zeitwerk (2.6.7)
|
113
|
+
|
114
|
+
PLATFORMS
|
115
|
+
arm64-darwin-21
|
116
|
+
|
117
|
+
DEPENDENCIES
|
118
|
+
dotenv
|
119
|
+
instant18n!
|
120
|
+
rake (~> 13.0)
|
121
|
+
rspec (~> 3.0)
|
122
|
+
rubocop (~> 1.21)
|
123
|
+
|
124
|
+
BUNDLED WITH
|
125
|
+
2.4.12
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Obie Fernandez
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
# Instant18n Gem for Rails
|
2
|
+
|
3
|
+
Use OpenAI's GPT large-language model to power internationalization of the text in your Rails application. Extracted from real-world usage in [MagmaChat](https://github.com/magma-labs/magma-chat).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add instant18n
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install instant18n
|
14
|
+
|
15
|
+
Make sure to set `OPENAI_ACCESS_TOKEN` in your environment so that the library is able to access GPT.
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Invoke with `I18n.it` or simply `it` in your view templates. (Method is short for _instant translation_). Use in place of the standard `t` method for translating text.
|
20
|
+
|
21
|
+
The `it` method provides translation using the GPT-3 language model and caching the results to improve performance.
|
22
|
+
|
23
|
+
```
|
24
|
+
>> I18n.it('Hello world!', lang: 'español')
|
25
|
+
=> Hola mundo!
|
26
|
+
```
|
27
|
+
|
28
|
+
This will attempt to translate the text associated with the key hello_world to the English language using the GPT-3 language model. If the translation is successful, the translated text will be returned. If the translation fails, the original text (or GPT error) will be returned.
|
29
|
+
|
30
|
+
### Options
|
31
|
+
|
32
|
+
The `it` method accepts the following parameters:
|
33
|
+
|
34
|
+
- key (required): The key associated with the text to be translated.
|
35
|
+
- lang (required): The language to translate the text to. Defaults to the default language set in the I18n module.
|
36
|
+
- class: if you pass in css classes with the `classes` option, the method will return the translation wrapped in a `div` tag, instead of plain text.
|
37
|
+
|
38
|
+
Additional options that affect caching:
|
39
|
+
- force: force a cache miss
|
40
|
+
- expires_in: (seconds) how long to cache the translation
|
41
|
+
|
42
|
+
Additional options that are passed to the GPT-3 API:
|
43
|
+
|
44
|
+
- model: defaults to gpt-3.5-turbo
|
45
|
+
- temperature: defaults to 0.25
|
46
|
+
- max_tokens: defaults to 64
|
47
|
+
- top_p: defaults to 0.1
|
48
|
+
- frequency_penalty: defaults to 0
|
49
|
+
- presence_penalty: defaults to 0
|
50
|
+
|
51
|
+
Full description of these options is available [here](https://platform.openai.com/docs/api-reference/chat/create).
|
52
|
+
|
53
|
+
### Default Language
|
54
|
+
|
55
|
+
The default language is set to `English`. For performance and practical reasons, if you pass in the default language, GPT is not invoked. Change the default language in an initializer or at runtime by changing the value of the `default_language` property on the `I18n` module.
|
56
|
+
|
57
|
+
```
|
58
|
+
I18n.default_language = "Spanish"
|
59
|
+
```
|
60
|
+
|
61
|
+
### Anything Goes
|
62
|
+
|
63
|
+
Because GPT is smart and can translate into almost anything that resembles a language, all of the following options are known to work:
|
64
|
+
* Español
|
65
|
+
* Baby Talk
|
66
|
+
* Baseldeutsch
|
67
|
+
* Braille
|
68
|
+
* Ebonics
|
69
|
+
* Emoji
|
70
|
+
* Esperanto
|
71
|
+
* Gregg Shorthand
|
72
|
+
* हिन्दी
|
73
|
+
* 日本語
|
74
|
+
* Klingon
|
75
|
+
* 1337 Speak (Leetspeak)
|
76
|
+
* 한국어
|
77
|
+
* 中文
|
78
|
+
* Newspeak
|
79
|
+
* Morse Code
|
80
|
+
* Rhyming Cockney Slang
|
81
|
+
* Sindarin
|
82
|
+
* Singlish
|
83
|
+
* Spanglish
|
84
|
+
* العربية
|
85
|
+
* Trumpisms
|
86
|
+
* Türkçe
|
87
|
+
* Uwu
|
88
|
+
|
89
|
+
The limit is your imagination!
|
90
|
+
## Development
|
91
|
+
|
92
|
+
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.
|
93
|
+
|
94
|
+
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](https://rubygems.org).
|
95
|
+
|
96
|
+
## Testing
|
97
|
+
|
98
|
+
The I18n Extensions gem can be tested using the RSpec testing framework. The tests are located in the spec directory and can be run using the following command:
|
99
|
+
|
100
|
+
```
|
101
|
+
bundle exec rspec
|
102
|
+
```
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/obie/instant18n. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/obie/instant18n/blob/main/CODE_OF_CONDUCT.md).
|
107
|
+
|
108
|
+
## License
|
109
|
+
|
110
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
111
|
+
|
112
|
+
## Code of Conduct
|
113
|
+
|
114
|
+
Everyone interacting in the Instant18n project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/obie/instant18n/blob/main/CODE_OF_CONDUCT.md).
|
115
|
+
|
116
|
+
## Acknowledgments
|
117
|
+
|
118
|
+
The I18n Extensions gem uses the GPT-3 language model API provided by OpenAI.
|
data/Rakefile
ADDED
data/instant18n.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/instant18n/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "instant18n"
|
7
|
+
spec.version = Instant18n::VERSION
|
8
|
+
spec.authors = ["Obie Fernandez"]
|
9
|
+
spec.email = ["obiefernandez@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Instant18n is a gem that makes it drop-dead simple to internationalize your application."
|
12
|
+
spec.description = "Uses the power of OpenAI's GPT large language AI models to generate translations for your application."
|
13
|
+
spec.homepage = "https://github.com/obie/instant18n"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/obie/instant18n"
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/obie/instant18n/changelog.md"
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(__dir__) do
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
# Uncomment to register a new dependency of your gem
|
34
|
+
spec.add_dependency "actionview"
|
35
|
+
spec.add_dependency "activesupport"
|
36
|
+
spec.add_dependency "railties"
|
37
|
+
spec.add_dependency "ruby-openai", "~> 4.0.0"
|
38
|
+
|
39
|
+
# For more information and examples about making a new gem, check out our
|
40
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
41
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require "action_view/helpers/tag_helper"
|
2
|
+
require "openai"
|
3
|
+
require "rails"
|
4
|
+
|
5
|
+
module I18n
|
6
|
+
extend ActionView::Helpers::TagHelper
|
7
|
+
|
8
|
+
DIRECTIVE = "You are not a helper anymmore, you are now a reliable translation web service."
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_accessor :default_language
|
12
|
+
end
|
13
|
+
|
14
|
+
self.default_language = "English"
|
15
|
+
|
16
|
+
##
|
17
|
+
# This method, `it`, instantly translates a given text based on the provided language code.
|
18
|
+
# It accepts a key, language code, and an optional hash of additional options.
|
19
|
+
#
|
20
|
+
# @param key [String] the text to be translated.
|
21
|
+
# @param lang [String] the target language or language code for translation (e.g., 'English', 'Español', 'fr').
|
22
|
+
# @param options [Hash] optional hash of additional configuration options.
|
23
|
+
# - :class [Array<String>] an array of CSS class names to be applied to the translated
|
24
|
+
# text when wrapped in a div.
|
25
|
+
# - :expires_in [Integer] the number of seconds to cache the translation.
|
26
|
+
# - :force [Boolean] whether to force a cache miss and re-translate the text.
|
27
|
+
# - :model [String] the name of the GPT-3 language model to use for translation.
|
28
|
+
# - :temperature [Float] the temperature to use for translation.
|
29
|
+
# - :max_tokens [Integer] the maximum number of tokens to use for translation.
|
30
|
+
# - :top_p [Float] the top p value to use for translation.
|
31
|
+
# - :frequency_penalty [Float] the frequency penalty to use for translation.
|
32
|
+
# - :presence_penalty [Float] the presence penalty to use for translation.
|
33
|
+
#
|
34
|
+
# The method fetches the translated text from cache if available, otherwise it uses the `chat`
|
35
|
+
# completion method of OpenAI to translate the text using the GPT-3 language model.
|
36
|
+
# The translated text is then cleaned up by removing any wrapper double quotes.
|
37
|
+
# If the :classes option is provided, the translated text is wrapped in a div with the
|
38
|
+
# specified css classes (in addition to `instant18n` and the language supplied, i.e. `espanol`).
|
39
|
+
# Note that the language will be parameterized, so that `Español` becomes `espanol`.
|
40
|
+
#
|
41
|
+
# @return [String] the translated text.
|
42
|
+
##
|
43
|
+
def self.it(key, lang, **opts)
|
44
|
+
opts && opts.symbolize_keys!
|
45
|
+
Rails.cache.fetch(cache_key(key, lang, opts), expires_in: (opts[:expires_in] || 1.year) , force: (opts[:force] || false)) do
|
46
|
+
if lang.casecmp(default_language).zero?
|
47
|
+
key
|
48
|
+
else
|
49
|
+
chat(prompt % { key: key, lang: lang }, directive: DIRECTIVE, **opts)
|
50
|
+
end
|
51
|
+
end.then do |text|
|
52
|
+
text = text.gsub(/^"+|"+$/, '') # remove wrapper double quotes
|
53
|
+
if opts[:class].present?
|
54
|
+
tag.div(text, class: "instant18n #{lang.parameterize} #{opts[:class]}")
|
55
|
+
else
|
56
|
+
text
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def self.cache_key(key, lang, options)
|
64
|
+
"instant18n_#{key}_#{lang}_#{options.values.join("_")}"
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.chat(prompt, directive:, **options)
|
68
|
+
messages = [ gpt_message(:system, directive) ]
|
69
|
+
messages += [ gpt_message(:user, prompt) ]
|
70
|
+
|
71
|
+
params = {
|
72
|
+
messages: messages,
|
73
|
+
model: options[:model] || "gpt-3.5-turbo",
|
74
|
+
temperature: options[:temperature] || 0.25,
|
75
|
+
max_tokens: options[:max_tokens] || 64,
|
76
|
+
top_p: options[:top_p] || 0.1,
|
77
|
+
frequency_penalty: options[:frequency_penalty] || 0,
|
78
|
+
presence_penalty: options[:presence_penalty] || 0,
|
79
|
+
}
|
80
|
+
|
81
|
+
openai_client.chat(parameters: params).then do |response|
|
82
|
+
response.dig("error","message") || response.dig("choices", 0, "message", "content")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.prompt
|
87
|
+
<<-END
|
88
|
+
How do you write the text "%{key}" in clean, well-written %{lang} suitable for use as copy in a web application?
|
89
|
+
Do not add any commentary or punctuation! Only respond with the answer, unquoted. If your response is just a single word
|
90
|
+
or short phrase then do not add extra punctuation. The length of the response should match the length
|
91
|
+
of the prompt. If you have very low confidence in the translation, then just respond with the original text.
|
92
|
+
END
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.openai_client
|
96
|
+
@client ||= OpenAI::Client.new(access_token: ENV.fetch("OPENAI_ACCESS_TOKEN"))
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.gpt_message(role, content)
|
100
|
+
{ role: role.to_s, content: content }
|
101
|
+
end
|
102
|
+
end
|
data/lib/instant18n.rb
ADDED
data/log/development.log
ADDED
File without changes
|
data/sig/instant18n.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: instant18n
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Obie Fernandez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-04-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionview
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: railties
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: ruby-openai
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.0.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.0.0
|
69
|
+
description: Uses the power of OpenAI's GPT large language AI models to generate translations
|
70
|
+
for your application.
|
71
|
+
email:
|
72
|
+
- obiefernandez@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".env"
|
78
|
+
- ".rspec"
|
79
|
+
- ".rubocop.yml"
|
80
|
+
- CHANGELOG.md
|
81
|
+
- CODE_OF_CONDUCT.md
|
82
|
+
- Gemfile
|
83
|
+
- Gemfile.lock
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
87
|
+
- instant18n.gemspec
|
88
|
+
- lib/i18n_extensions.rb
|
89
|
+
- lib/instant18n.rb
|
90
|
+
- lib/instant18n/version.rb
|
91
|
+
- log/development.log
|
92
|
+
- sig/instant18n.rbs
|
93
|
+
homepage: https://github.com/obie/instant18n
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata:
|
97
|
+
allowed_push_host: https://rubygems.org
|
98
|
+
homepage_uri: https://github.com/obie/instant18n
|
99
|
+
source_code_uri: https://github.com/obie/instant18n
|
100
|
+
changelog_uri: https://github.com/obie/instant18n/changelog.md
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 2.6.0
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubygems_version: 3.4.10
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Instant18n is a gem that makes it drop-dead simple to internationalize your
|
120
|
+
application.
|
121
|
+
test_files: []
|