libretranslate 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 484471670192869da27023adcfcad1d719f5186c21dd7c3938b048611d9462a3
4
+ data.tar.gz: 7348c725478fe107d7648cb7fc5489a77ec1d4f2664fa7f648b4a0cecffe554f
5
+ SHA512:
6
+ metadata.gz: f47069190928c1c292a9b0615e57f10fa76253142b4345d1ee2d09820206d9af2e3655c0122288514d70fc9d4021bc9c749b0285f9950a9ce62f3fcd3757beec
7
+ data.tar.gz: aa4c7db67647b89fe83f13f741bbb5c572553c10d3debe7acf72119e04ea480e8078fffc7648bf13322da0a9130bcb1e3d25bc9d8f22b09182174f0b0bc12c22
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-12-26
4
+
5
+ - Initial release
@@ -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 sebastien.gaya@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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Sébastien Gaya
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,77 @@
1
+ # LibreTranslate
2
+
3
+ Ruby wrapper to communicate with [LibreTranslate](https://libretranslate.com) API.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add libretranslate
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install libretranslate
14
+
15
+ If you're working on a Ruby on Rails application, generate the initializer to set your configuration:
16
+
17
+ $ rails generate libretranslate:install
18
+
19
+ Else, add the following in your code and modify it to your configuration:
20
+
21
+ ```rb
22
+ require "libretranslate"
23
+
24
+ LibreTranslate.configure do |config|
25
+ config.base_url = "https://libretranslate.com"
26
+ config.api_key = ""
27
+ end
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ ### Detect the language of a single text
33
+
34
+ ```rb
35
+ LibreTranslate.detect("Hello world!")
36
+ ```
37
+
38
+ ### Translate text from one language to another
39
+
40
+ ```rb
41
+ # With source and target languages
42
+ LibreTranslate.translate("Hello world!", source: "en", target: "fr")
43
+ # With source detection
44
+ LibreTranslate.translate("Hello world!", source: "auto", target: "fr")
45
+ # With HTML
46
+ LibreTranslate.translate("Hello <b>world</b>!", source: "en", target: "fr", format: "html")
47
+ ```
48
+
49
+ ### Recover the list of supported languages
50
+
51
+ ```rb
52
+ LibreTranslate.languages
53
+ ```
54
+
55
+ ### Recover interface-specific settings
56
+
57
+ ```rb
58
+ LibreTranslate.frontend_settings
59
+ ```
60
+
61
+ ## Development
62
+
63
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
64
+
65
+ 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).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/noesya/libretranslate. 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/noesya/libretranslate/blob/main/CODE_OF_CONDUCT.md).
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
74
+
75
+ ## Code of Conduct
76
+
77
+ Everyone interacting in the Libretranslate project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/noesya/libretranslate/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/base'
4
+
5
+ module Libretranslate
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("../../templates", __FILE__)
9
+
10
+ desc "Creates a LibreTranslate initializer."
11
+
12
+ def copy_initializer
13
+ template "libretranslate.rb", "config/initializers/libretranslate.rb"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ LibreTranslate.configure do |config|
4
+ # Configure the base URL of your LibreTranslate provider.
5
+ config.base_url = "https://libretranslate.com"
6
+
7
+ # Configure the API key that will be used in requests.
8
+ config.api_key = ""
9
+ end
@@ -0,0 +1,37 @@
1
+ require_relative "error"
2
+
3
+ module LibreTranslate
4
+ class Api
5
+ def self.get(path)
6
+ uri = uri_from_path(path)
7
+ body = Net::HTTP.get(uri)
8
+ JSON.parse(body)
9
+ end
10
+
11
+ def self.post(path, params = {})
12
+ uri = uri_from_path(path)
13
+ params.merge!({ api_key: LibreTranslate.api_key })
14
+
15
+ response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
16
+ request = Net::HTTP::Post.new(uri)
17
+ request["Content-Type"] = "application/json"
18
+ request.body = params.to_json
19
+ http.request(request)
20
+ end
21
+
22
+ raise_error(response) unless response.code == "200"
23
+ JSON.parse(response.body)
24
+ end
25
+
26
+ private
27
+
28
+ def self.raise_error(response)
29
+ error_message = JSON.parse(response.body)["error"]
30
+ raise LibreTranslate::Error.new(response.code.to_i, error_message)
31
+ end
32
+
33
+ def self.uri_from_path(path)
34
+ URI(LibreTranslate.base_url + path)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,14 @@
1
+ module LibreTranslate
2
+ class Error < StandardError
3
+ attr_reader :code, :message
4
+
5
+ def initialize(code, message)
6
+ @code = code
7
+ @message = message
8
+ end
9
+
10
+ def to_s
11
+ message
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LibreTranslate
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "open-uri"
6
+
7
+ require_relative "libre_translate/api"
8
+ require_relative "libre_translate/version"
9
+
10
+ module LibreTranslate
11
+ class << self
12
+ # Base URL of the LibreTranslate API provider.
13
+ attr_writer :base_url
14
+ def base_url
15
+ @base_url ||= "https://libretranslate.com"
16
+ end
17
+
18
+ # API key to use in LibreTranslate API requests.
19
+ attr_writer :api_key
20
+ def api_key
21
+ @api_key ||= ""
22
+ end
23
+ end
24
+
25
+ def self.languages
26
+ Api.get("/languages")
27
+ end
28
+
29
+ def self.frontend_settings
30
+ Api.get("/frontend/settings")
31
+ end
32
+
33
+ def self.detect(text)
34
+ Api.post("/detect", { q: text })
35
+ end
36
+
37
+ def self.translate(text, source:, target:, format: "text")
38
+ Api.post("/translate", {
39
+ q: text,
40
+ source: source,
41
+ target: target,
42
+ format: format
43
+ })
44
+ end
45
+
46
+ def self.suggest(text, translation, source:, target:)
47
+ Api.post("/suggest", {
48
+ q: text,
49
+ s: translation,
50
+ source: source,
51
+ target: target
52
+ })
53
+ end
54
+
55
+ def self.configure
56
+ yield self
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "libre_translate"
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libretranslate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sébastien Gaya
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-12-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
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: net-http
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: open-uri
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: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Ruby wrapper to communicate with LibreTranslate API.
70
+ email:
71
+ - sebastien.gaya@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - CHANGELOG.md
77
+ - CODE_OF_CONDUCT.md
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/generators/libretranslate/install_generator.rb
82
+ - lib/generators/templates/libretranslate.rb
83
+ - lib/libre_translate.rb
84
+ - lib/libre_translate/api.rb
85
+ - lib/libre_translate/error.rb
86
+ - lib/libre_translate/version.rb
87
+ - lib/libretranslate.rb
88
+ homepage: https://github.com/noesya/libretranslate
89
+ licenses:
90
+ - MIT
91
+ metadata:
92
+ homepage_uri: https://github.com/noesya/libretranslate
93
+ source_code_uri: https://github.com/noesya/libretranslate
94
+ changelog_uri: https://github.com/noesya/libretranslate/blob/main/CHANGELOG.md
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.6.0
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.4.22
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Ruby wrapper to communicate with LibreTranslate API.
114
+ test_files: []