i18n_remote 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 28a29bf8a35c5bf960bfe62ecd7bf016e43da02d4eabfd10be5f444a3bbc3f75
4
+ data.tar.gz: 998757960d572ca88ed59bedae6fc58cc492cb9db88ccf66cc0c26a858710e86
5
+ SHA512:
6
+ metadata.gz: a05d9571d5af6018d1a6ce124952f404b0957ef13d4c658d066db77f93dfb56b2e0ef4b949c237094f9ddd654b70e43703b3f7199250ca03cdbd85aec68fbab5
7
+ data.tar.gz: 508f07b56830277176a0250668a10fe45e577422e61c7f3cde32c55fb25e82de8638fc67df1f8eea030218b60085630a12636e0b84d5e1a64dcc0fd544482cb1
@@ -0,0 +1,68 @@
1
+ require "uri"
2
+ require "net/http"
3
+
4
+ # This is a basic backend for supporting remote files. It receives on
5
+ # initialization the server where the remote translations reside, the
6
+ # remote files and the authentication token to access the remote files.
7
+ # It fetaches the translations from the remote files and stores them in
8
+ # an in-memory hash. Inherits from the Simple backend and should be chained
9
+ # to the simple backend store so the local translations are used in case
10
+ # of Network errors.
11
+ #
12
+ # I18n.backend = I18n::Backend::Chain.new(I18n::Backend::RemoteFile.new(params), I18n.backend)
13
+ # The remote files to be fetched should have the following JSON format
14
+ # "translation_file_1": {
15
+ # "en":
16
+ # {
17
+ # "name": "Name",
18
+ # "title": "Title",
19
+ # "content": "Content"
20
+ # }
21
+ # }
22
+
23
+ module I18nRemote
24
+ module Backend
25
+ class RemoteFile < I18n::Backend::Simple
26
+ def initialize(params)
27
+ @translations_server = params[:translations_server]
28
+ @filenames = params[:filenames]
29
+ end
30
+
31
+ private
32
+
33
+ def init_translations
34
+ fetch_remote_translations
35
+ @initialized = true
36
+ end
37
+
38
+ def fetch_remote_translations
39
+ @filenames.flatten.each do |filename|
40
+ load_translation(filename)
41
+ end
42
+ end
43
+
44
+ def load_translation(filename)
45
+ data = load_content(filename)
46
+ raise InvalidLocaleData.new(filename, "expects it to return a hash, but does not") unless data.is_a?(Hash)
47
+
48
+ data.each { |locale, d| store_translations(locale, d || {}) }
49
+
50
+ data
51
+ end
52
+
53
+ def load_content(filename)
54
+ ## Authentication should be set up to access the server securely.
55
+ ## For the sake of using this gem for testing purposes, it is here skipped.
56
+ uri = URI(@translations_server + filename)
57
+ response = Net::HTTP.get_response(uri)
58
+ begin
59
+ response.is_a?(Net::HTTPSuccess)
60
+ JSON.parse(response.body)
61
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
62
+ Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError
63
+ raise InvalidLocaleData.new(filename, "The remote translations could not be loaded")
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1 @@
1
+ require "i18n_remote/backend/remote_file"
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_remote
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Roya Anane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: In combination with the i18n gem Simple backend, using the i18n_remote
14
+ allows fetching data remotely
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/i18n_remote.rb
21
+ - lib/i18n_remote/backend/remote_file.rb
22
+ homepage:
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.1.6
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: This gem builds on top of the i18n library to support Fetching remote translations
45
+ with HTTP
46
+ test_files: []