remote-i18n 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: a40989d72c8d0c558e46cfbd77c5a17881c0964f44cea2a1b5fba2494d2cac9d
4
+ data.tar.gz: acd3c0300c419eb1702c14b02855e407e6bb3970569edadbc29af8ee091ee756
5
+ SHA512:
6
+ metadata.gz: 19c11e384431185fb8f049774cfbadb78885fc5660a22f3866f702f1f1859c5772a60564636c9282c0613bb7aff6d2c29b018641b92751377910ec6ffe7e00dd
7
+ data.tar.gz: 1778102ab8567f211cf3dacd851506d482a081957de4a8c025cfaf11353d60a9fdad06bd8fdd614ad10917ff1faada92f50437534978f40b00f7e6fa7f867375
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,2 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-07-29
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 schnmudgal@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,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in remote_i18n.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+ gem 'rspec', '~> 3.0'
10
+ gem 'standard', '~> 1.3'
11
+
12
+ gem 'httparty', '~> 0.21.0'
13
+ gem 'i18n', '~> 1.14', '>= 1.14.1'
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ remote_i18n (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ concurrent-ruby (1.2.2)
11
+ diff-lcs (1.5.0)
12
+ httparty (0.21.0)
13
+ mini_mime (>= 1.0.0)
14
+ multi_xml (>= 0.5.2)
15
+ i18n (1.14.1)
16
+ concurrent-ruby (~> 1.0)
17
+ json (2.6.3)
18
+ language_server-protocol (3.17.0.3)
19
+ lint_roller (1.1.0)
20
+ mini_mime (1.1.2)
21
+ multi_xml (0.6.0)
22
+ parallel (1.23.0)
23
+ parser (3.2.2.3)
24
+ ast (~> 2.4.1)
25
+ racc
26
+ racc (1.7.1)
27
+ rainbow (3.1.1)
28
+ rake (13.0.6)
29
+ regexp_parser (2.8.1)
30
+ rexml (3.2.6)
31
+ rspec (3.12.0)
32
+ rspec-core (~> 3.12.0)
33
+ rspec-expectations (~> 3.12.0)
34
+ rspec-mocks (~> 3.12.0)
35
+ rspec-core (3.12.2)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-expectations (3.12.3)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.12.0)
40
+ rspec-mocks (3.12.6)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.12.0)
43
+ rspec-support (3.12.1)
44
+ rubocop (1.52.1)
45
+ json (~> 2.3)
46
+ parallel (~> 1.10)
47
+ parser (>= 3.2.2.3)
48
+ rainbow (>= 2.2.2, < 4.0)
49
+ regexp_parser (>= 1.8, < 3.0)
50
+ rexml (>= 3.2.5, < 4.0)
51
+ rubocop-ast (>= 1.28.0, < 2.0)
52
+ ruby-progressbar (~> 1.7)
53
+ unicode-display_width (>= 2.4.0, < 3.0)
54
+ rubocop-ast (1.29.0)
55
+ parser (>= 3.2.1.0)
56
+ rubocop-performance (1.18.0)
57
+ rubocop (>= 1.7.0, < 2.0)
58
+ rubocop-ast (>= 0.4.0)
59
+ ruby-progressbar (1.13.0)
60
+ standard (1.30.1)
61
+ language_server-protocol (~> 3.17.0.2)
62
+ lint_roller (~> 1.0)
63
+ rubocop (~> 1.52.0)
64
+ standard-custom (~> 1.0.0)
65
+ standard-performance (~> 1.1.0)
66
+ standard-custom (1.0.2)
67
+ lint_roller (~> 1.0)
68
+ rubocop (~> 1.50)
69
+ standard-performance (1.1.2)
70
+ lint_roller (~> 1.1)
71
+ rubocop-performance (~> 1.18.0)
72
+ unicode-display_width (2.4.2)
73
+
74
+ PLATFORMS
75
+ x86_64-darwin-21
76
+
77
+ DEPENDENCIES
78
+ httparty (~> 0.21.0)
79
+ i18n (~> 1.14, >= 1.14.1)
80
+ rake (~> 13.0)
81
+ remote_i18n!
82
+ rspec (~> 3.0)
83
+ standard (~> 1.3)
84
+
85
+ BUNDLED WITH
86
+ 2.2.32
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Sachin Mudgal
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,66 @@
1
+ # RemoteI18n
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/remote_i18n`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'remote_i18n'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install remote_i18n
22
+
23
+ ## Usage
24
+
25
+ - This gem is made on top of existing `I18n` gem.
26
+ - This gem uses existing "Simple" backend as a base for new "Remote" backend. It needs to be extended further to use "Key-Pair" backend with the remote support (TODO for future versions)
27
+ - To use it, manually provide the "remote locations URLs" from where translations are to be fetched.
28
+ - You can also set the "fallback locations" as you simply do with the normal `I18n` implementation.
29
+ - Generate a config file using `rake generate remote_i18n_config`, or create your own initializer.
30
+ - Example code is below:
31
+ ```
32
+ # Remote URLs where the translation files are hosted
33
+ I18n.config.remote_urls = [
34
+ 'https://raw.githubusercontent.com/g1stavo/rails-i18n/master/config/locales/en.yml',
35
+ 'https://raw.githubusercontent.com/g1stavo/rails-i18n/master/config/locales/pt-BR.yml'
36
+ ]
37
+
38
+ # Fallback location for local translation file(s)
39
+ I18n.load_path = [
40
+ File.expand_path('./locales/en.yml')
41
+ ]
42
+
43
+ # Example - translate in English
44
+ puts I18n.translate(:confirm, locale: :en)
45
+
46
+ # Example - transalte in Portuguese
47
+ puts I18n.translate(:confirm, locale: 'pt-BR')
48
+ ```
49
+
50
+ ## Development
51
+
52
+ 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.
53
+
54
+ 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).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/remote_i18n. 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/[USERNAME]/remote_i18n/blob/master/CODE_OF_CONDUCT.md).
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
63
+
64
+ ## Code of Conduct
65
+
66
+ Everyone interacting in the RemoteI18n project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/remote_i18n/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "remote_i18n"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ INITIALIZER_CONTENT = "# Remote URLs where the translation files are hosted
4
+ I18n.config.remote_urls = [
5
+ 'https://raw.githubusercontent.com/g1stavo/rails-i18n/master/config/locales/en.yml',
6
+ 'https://raw.githubusercontent.com/g1stavo/rails-i18n/master/config/locales/pt-BR.yml'
7
+ ]
8
+
9
+ ## Fallback location for local translation file(s)
10
+ # I18n.load_path = [
11
+ # File.expand_path('./locales/en.yml')
12
+ # ]
13
+
14
+ ## Example - translate in English
15
+ # puts I18n.translate(:confirm, locale: :en)
16
+
17
+ ## Example - transalte in Portuguese
18
+ # puts I18n.translate(:confirm, locale: 'pt-BR')
19
+ "
20
+
21
+ class RemoteI18nConfigGenerator < Rails::Generators::Base
22
+ desc "This generator creates an initializer file at config/initializers"
23
+ def create_initializer_file
24
+ create_file("config/initializers/remote_i18n.rb", INITIALIZER_CONTENT)
25
+ end
26
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'i18n'
4
+ require 'httparty'
5
+ require 'tempfile'
6
+
7
+ module I18n
8
+ module Backend
9
+ # We will use I18n Simple Backend which utilises in-memory store for now
10
+ class Remote < I18n::Backend::Simple
11
+ module Implementation
12
+ include HTTParty
13
+
14
+ # Constants
15
+ JSON_CONTENT_TYPES = ['application/json', 'application/x-json'].freeze
16
+ YAML_CONTENT_TYPES = ['application/yaml', 'application/x-yaml', 'text/yaml', 'text/x-yaml', 'text/plain'].freeze
17
+
18
+ protected
19
+
20
+ def load_translations(urls: [])
21
+ super([]) if urls.empty?
22
+
23
+ remote_fetched_tempfile_paths = []
24
+
25
+ urls.flatten.each do |url|
26
+ begin
27
+ tempfile = resolve_remote_data(url)
28
+ rescue StandardError => e
29
+ super([])
30
+ end
31
+
32
+ next unless tempfile
33
+
34
+ remote_fetched_tempfile_paths << tempfile.path
35
+ end
36
+
37
+ super(*remote_fetched_tempfile_paths)
38
+ end
39
+
40
+ def resolve_remote_data(url)
41
+ response = HTTParty.get(url)
42
+ content_type = response.content_type
43
+ tempfile = nil
44
+
45
+ # For JSON response
46
+ if JSON_CONTENT_TYPES.include?(content_type)
47
+ tempfile = Tempfile.new(["remote_I18n-fetched-#{Time.now}", '.json'])
48
+
49
+ # For YAML response
50
+ elsif YAML_CONTENT_TYPES.include?(content_type)
51
+ tempfile = Tempfile.new(["remote_I18n-fetched-#{Time.now}", '.yml'])
52
+ end
53
+
54
+ return unless tempfile
55
+
56
+ File.open(tempfile.path,'w') do |f|
57
+ f.write response.body
58
+ end
59
+
60
+ tempfile
61
+ end
62
+
63
+ def init_translations
64
+ load_translations(urls: I18n.config.remote_urls.to_a)
65
+
66
+ @initialized = true
67
+ end
68
+
69
+ end
70
+
71
+ include Implementation
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'backend/remote'
4
+ require_relative 'exceptions'
5
+ require 'i18n'
6
+ require 'uri'
7
+
8
+ module I18n
9
+ class Config
10
+ def backend
11
+ @@backend ||= I18n::Backend::Remote.new
12
+ end
13
+
14
+ attr_reader :remote_urls
15
+
16
+ def remote_urls=(value)
17
+ if value.is_a? Array
18
+ value.each do |url|
19
+ raise I18n::InvalidLocaleRemoteLocation.new(url, 'is not a valid URL') unless url =~ /\A#{URI::regexp}\z/
20
+ end
21
+
22
+ elsif value.is_a? String
23
+ raise I18n::InvalidLocaleRemoteLocation.new(value, 'is not a valid URL') unless value =~ /\A#{URI::regexp}\z/
24
+ value = [value]
25
+
26
+ else
27
+ raise I18n::InvalidLocaleRemoteLocation.new(value, 'value must be an Array or a String')
28
+ end
29
+
30
+ @remote_urls = value
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18n
4
+ class InvalidLocaleRemoteLocation < ArgumentError
5
+ attr_reader :remote_url
6
+
7
+ def initialize(remote_url, exception_message)
8
+ @remote_url, @exception_message = remote_url, exception_message
9
+ super "can not load translations from remote location '#{remote_url}': #{exception_message}"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RemoteI18n
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "remote_i18n/version"
4
+ require_relative 'remote_i18n/config'
5
+
6
+ module RemoteI18n
7
+ # Does nothing
8
+ ## This gem monkey patches the existing "I18n" module with a new Backend and related config.
9
+ ## So code under it's own named module is not needed
10
+
11
+ ## Go to "lib" directory to have a look at the code
12
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: remote-i18n
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sachin Mudgal
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-08-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: I18n with support of remote files for translations.
14
+ email:
15
+ - schnmudgal@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".standard.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - lib/generators/remote_i18n_config_generator.rb
32
+ - lib/remote_i18n.rb
33
+ - lib/remote_i18n/backend/remote.rb
34
+ - lib/remote_i18n/config.rb
35
+ - lib/remote_i18n/exceptions.rb
36
+ - lib/remote_i18n/version.rb
37
+ homepage: https://github.com/schnmudgal/remote_i18n
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/schnmudgal/remote_i18n
42
+ source_code_uri: https://github.com/schnmudgal/remote_i18n
43
+ changelog_uri: https://github.com/schnmudgal/remote_i18n
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.6.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.2.32
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: I18n with support of remote files for translations.
63
+ test_files: []