i18n-remote_backend 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: 63e0370549d377339d8478e713df25e4fdcf78cc375639dc474e7c53a79fb57c
4
+ data.tar.gz: d9165bdf584bf69e76eb110e0d9fcafc18518a9605e986c85df9e49396f7b7f3
5
+ SHA512:
6
+ metadata.gz: a931dd0853154d803e82d93fa4488ea00970d96c993e736dcb7b75ab68f319ed550567a4166b78307d3ea645aa22f69362f94beae5cda86bf446a17d961b8e52
7
+ data.tar.gz: '084f45906d8342a95ea31e1c668f0ba1de59a8e27b325c0e4ef76ea0b8daf5c71be475f82c7d94748a9d679ebe8a3d76890368a7a23cccc7e82c3d71472ed264'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 TODO: Write your name
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,45 @@
1
+ # I18n::RemoteBackend
2
+
3
+ I18n backend for loading remote locales via HTTP.
4
+
5
+ ## Installation
6
+
7
+ Put the following in your Gemfile:
8
+
9
+ ```ruby
10
+ gem 'i18n-remote_backend', require: 'i18n/remote_backend', git: 'git@github.com:mudasirraza/i18n-remote_backend.git'
11
+ ```
12
+
13
+ After updating your bundle, run the installer
14
+
15
+ $ rails g i18n:remote_backend:install
16
+
17
+ It generates a new file in `config/initializers` named `i18n_remote_backend.rb` with the following content.
18
+
19
+ ```ruby
20
+ require 'i18n/backend/remote_backend'
21
+
22
+ I18n.backend = I18n::Backend::Chain.new(I18n::Backend::RemoteBackend.new, I18n::Backend::Simple.new)
23
+
24
+ I18n::Backend::RemoteBackend.configure do |config|
25
+ # config.http_url = ''
26
+ end
27
+ ```
28
+
29
+ Set the http_url accordingly. e.g `https://example-url/locales`
30
+ The corresponding generated requests will look like: `https://example-url/locales/en` or `https://example-url/locales/fr` etc.
31
+ And it should respond back with the yaml file content.
32
+
33
+ If a remote translation is unavailable, it will fall back to a local translation.
34
+
35
+ ## Usage
36
+
37
+ You can then use `I18n.t('text')`.
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mudasirraza/i18n-remote_backend.
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18n
4
+ module RemoteBackend
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ desc 'This generator creates an initializer file at config/initializers'
8
+
9
+ source_root File.expand_path('templates', __dir__)
10
+
11
+ def copy_initializer
12
+ template 'initializer.rb', 'config/initializers/i18n_remote_backend.rb'
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'i18n/backend/remote_backend'
4
+
5
+ I18n.backend = I18n::Backend::Chain.new(I18n::Backend::RemoteBackend.new, I18n::Backend::Simple.new)
6
+
7
+ I18n::Backend::RemoteBackend.configure do |config|
8
+ # config.http_url = ''
9
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+
5
+ module I18n
6
+ module Backend
7
+ class RemoteBackend
8
+ class Configuration
9
+ attr_reader :http_url
10
+
11
+ def http_url=(http_url)
12
+ raise RemoteBackend::InvalidURLException unless http_url =~ URI::DEFAULT_PARSER.regexp[:ABS_URI]
13
+
14
+ @http_url = http_url
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18n
4
+ module Backend
5
+ class RemoteBackend
6
+ class InvalidURLException < StandardError; end
7
+ class BlankURLException < StandardError; end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'i18n/backend/base'
4
+
5
+ module I18n
6
+ module Backend
7
+ class RemoteBackend
8
+ module Implementation
9
+ include Base
10
+
11
+ attr_accessor :store
12
+
13
+ def initialize
14
+ @store = {}
15
+ I18n.enforce_available_locales = false
16
+ end
17
+
18
+ def initialized?
19
+ !@store.nil?
20
+ end
21
+
22
+ def store_translations(locale, data, _options = {})
23
+ @store[locale] = data
24
+ end
25
+
26
+ def available_locales
27
+ @store.keys.map(&:to_sym)
28
+ end
29
+
30
+ def translations
31
+ @store
32
+ end
33
+
34
+ def init_translations
35
+ @store
36
+ end
37
+
38
+ protected
39
+
40
+ def lookup(locale, key, scope = [], options = {})
41
+ return unless fetch_remote_translations(locale)
42
+
43
+ keys = I18n.normalize_keys(locale, key, scope, options[:separator])
44
+ keys.inject(translations) do |result, k|
45
+ return nil unless result.is_a?(Hash)
46
+
47
+ unless result.key?(k)
48
+ k = k.to_s.to_sym
49
+ return nil unless result.key?(k)
50
+ end
51
+ result[k]
52
+ end
53
+ end
54
+
55
+ def fetch_remote_translations(locale)
56
+ return @store[locale] if @store[locale]
57
+
58
+ data = Loader.new(locale).fetch_locale
59
+ @store[locale] = data[locale] if data
60
+
61
+ @store[locale]
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open-uri'
4
+
5
+ module I18n
6
+ module Backend
7
+ class RemoteBackend
8
+ class Loader
9
+ attr_reader :locale
10
+ attr_accessor :yaml
11
+
12
+ def initialize(locale = default_locale)
13
+ @locale = locale
14
+ end
15
+
16
+ def fetch_locale
17
+ raise RemoteBackend::BlankURLException if RemoteBackend.configuration.http_url.nil?
18
+
19
+ begin
20
+ yaml = YAML.safe_load(fetch_remote_file)
21
+ Utils.deep_symbolize_keys(yaml) if yaml.instance_of?(Hash)
22
+ rescue StandardError => e
23
+ RemoteBackend.logger.error "Error occured while reading remote locale : #{RemoteBackend.configuration.http_url}/#{locale} error: " + e.inspect
24
+ nil
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def fetch_remote_file
31
+ URI.parse("#{RemoteBackend.configuration.http_url}/#{@locale}").open.read
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+ Dir["#{File.dirname(__FILE__)}/remote_backend/*.rb"].sort.each { |file| require file }
5
+
6
+ module I18n
7
+ module Backend
8
+ class RemoteBackend
9
+ include Implementation
10
+
11
+ class << self
12
+ def configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ def configure
17
+ yield(configuration) if block_given?
18
+ end
19
+
20
+ def logger
21
+ @logger ||= defined?(Rails) ? Rails.logger : Logger.new($stdout)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18n
4
+ module RemoteBackend
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'i18n'
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n-remote_backend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mudasir Raza
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: rspec
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
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - i18n-remote@googlegroups.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - lib/generators/i18n/remote_backend/install_generator.rb
94
+ - lib/generators/i18n/remote_backend/templates/initializer.rb
95
+ - lib/i18n/backend/remote_backend.rb
96
+ - lib/i18n/backend/remote_backend/configuration.rb
97
+ - lib/i18n/backend/remote_backend/exceptions.rb
98
+ - lib/i18n/backend/remote_backend/implementation.rb
99
+ - lib/i18n/backend/remote_backend/loader.rb
100
+ - lib/i18n/remote_backend.rb
101
+ - lib/i18n/remote_backend/version.rb
102
+ homepage: https://github.com/mudasirraza/i18n-remote_backend
103
+ licenses:
104
+ - MIT
105
+ metadata:
106
+ homepage_uri: https://github.com/mudasirraza/i18n-remote_backend
107
+ source_code_uri: https://github.com/mudasirraza/i18n-remote_backend
108
+ changelog_uri: https://github.com/mudasirraza/i18n-remote_backend/blob/main/CHANGELOG.md
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 2.6.0
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubygems_version: 3.4.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Adds support for fetching translation from a remote resource
128
+ test_files: []