localise_rails 0.1.3

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: 2e6005d06a4ae06da165393d5b5dbde2f81bafd6fe325340c5c294ee372659b8
4
+ data.tar.gz: e2c5030c305683536e7ad5e1dc2018d1b8c044b2cfecf7a2ad20e83e29348b02
5
+ SHA512:
6
+ metadata.gz: 5b3640408e57005c6f9b618eaa1d81d5cf220bd395f78e999e7ba592cb5f180ec13c6bda7b44b23f36bb087fd7cb597c7378633fc77f5be5dd168c44481b4d56
7
+ data.tar.gz: 606bdcfc749f6e44be94c6b84ceaf158d280e00c2817aef0a30c7b6738fc6ee8b59a4c85917fd853a9149325a56ba25f8942ad3792f9a743915f9e4fbed688bf
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in localise_rails.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Osman Baliev
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,60 @@
1
+ # LocaliseRails
2
+
3
+ Easy localise for your Rails app from localise.biz (I18n)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'localise_rails'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install localise_rails
20
+
21
+ ## Usage
22
+
23
+ ### Configuration
24
+ ```ruby
25
+ # config/initializers/localise_rails.rb
26
+ LocaliseRails.configure do |config|
27
+ config.api_key = "LOCALISE_KEY"
28
+
29
+ ### all options https://localise.biz/api/docs/export/exportall
30
+ config.options = {
31
+ filter: 'backend',
32
+ fallback: 'en-US'
33
+ }
34
+
35
+ config.rename_locales = {
36
+ 'en-US' => 'en'
37
+ }
38
+
39
+ ### all options https://github.com/redis/redis-rb
40
+ # config.redis_options = {
41
+ # host: "127.0.0.1",
42
+ # port: 6379,
43
+ # db: 1,
44
+ # url: "redis://:p4ssw0rd@127.0.0.1:6379/1"
45
+ # }
46
+ end
47
+ ```
48
+ ### How to update locales
49
+ To crontab
50
+ ```ruby
51
+ LocaliseRails.update
52
+ ```
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/OnlyReFLeX/localise_rails.
57
+
58
+ ## License
59
+
60
+ 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,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "localise_rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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,12 @@
1
+ module LocaliseRails
2
+ # Class used to initialize configuration object.
3
+ class Config
4
+ attr_accessor :api_key, :options, :redis_options, :redis, :rename_locales
5
+
6
+ def initialize
7
+ @options = {}
8
+ @rename_locales = {}
9
+ @redis_options = {}
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ module LocaliseRails
2
+ class FetchLocaliseCache
3
+ def call
4
+ CachedTranslations.new(fetch_translations)
5
+ end
6
+
7
+ private
8
+
9
+ def fetch_translations
10
+ LocaliseRails.logger.info('Localise: start getting locales from redis')
11
+ timestamp = LocaliseRails.config.redis.get(UPDATED_AT_CACHE_KEY)
12
+ if timestamp.present?
13
+ data = LocaliseRails.config.redis.get(DATA_CACHE_KEY)
14
+ {
15
+ data: JSON.parse(data),
16
+ timestamp: timestamp
17
+ }
18
+ else
19
+ {}
20
+ end
21
+ end
22
+
23
+ class CachedTranslations
24
+ include ActiveModel::Model
25
+ attr_accessor :data, :timestamp
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,8 @@
1
+ require 'logger'
2
+
3
+ module LocaliseRails
4
+ class << self
5
+ attr_accessor :logger
6
+ end
7
+ self.logger = Logger.new($stdout)
8
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails'
2
+ require 'i18n'
3
+
4
+ module LocaliseRails
5
+ class Railtie < ::Rails::Railtie
6
+ initializer 'localise_rails.setup_logging' do
7
+ LocaliseRails.logger = Rails.logger
8
+ end
9
+
10
+ config.i18n.backend = I18n::Backend::Chain.new(
11
+ I18n::Backend::KeyValue.new(LocaliseRails::RequestStoreTranslations.new),
12
+ I18n.backend
13
+ )
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module LocaliseRails
2
+ class RequestStoreTranslations
3
+ def [](key)
4
+ seed_translations if RequestStore.store[:translations].nil?
5
+ RequestStore.store.dig(:translations, key)
6
+ end
7
+
8
+ def []=(key, value)
9
+ RequestStore.store[:translations][key] = value
10
+ end
11
+
12
+ def keys
13
+ seed_translations if RequestStore.store[:translations].nil?
14
+ RequestStore.store[:translations].keys
15
+ end
16
+
17
+ private
18
+
19
+ def seed_translations
20
+ RequestStore.store[:translations] = {}
21
+ LocaliseRails::SeedTranslationsToRequestStore.new.call
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,54 @@
1
+ module LocaliseRails
2
+ class SeedTranslationsToRequestStore
3
+ def call
4
+ if fresh_data_in_memory?
5
+ seed_translations_from_memory
6
+ else
7
+ seed_translations_from_cache
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ def seed_translations_from_memory
14
+ RequestStore.store[:translations] = Thread.current[:translations]
15
+ end
16
+
17
+ def seed_translations_from_cache
18
+ cached = LocaliseRails::FetchLocaliseCache.new.call
19
+ return unless cached.data
20
+
21
+ prepared_locales = prepare_locales(cached.data)
22
+ prepared_locales.map { |locale, value| I18n.backend.store_translations(locale, value) }
23
+ copy_request_store_to_memory(cached.timestamp)
24
+ end
25
+
26
+ def prepare_locales(locales)
27
+ LocaliseRails.config.rename_locales.each do |old_key, new_key|
28
+ locales[new_key] = locales.delete old_key if locales[old_key]
29
+ end
30
+ locales
31
+ end
32
+
33
+ def copy_request_store_to_memory(timestamp)
34
+ Thread.current[:translations] = RequestStore.store[:translations]
35
+ Thread.current[:translations_timestamp] = timestamp
36
+ end
37
+
38
+ def fresh_data_in_memory?
39
+ Thread.current[:translations].present? && fresh_memory_data?
40
+ end
41
+
42
+ def fresh_memory_data?
43
+ cached_timestamp.present? && memory_timestamp.present? && cached_timestamp == memory_timestamp
44
+ end
45
+
46
+ def cached_timestamp
47
+ @cached_timestamp ||= LocaliseRails.config.redis.get(LocaliseRails::UPDATED_AT_CACHE_KEY)
48
+ end
49
+
50
+ def memory_timestamp
51
+ @memory_timestamp ||= Thread.current[:translations_timestamp]
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,25 @@
1
+ module LocaliseRails
2
+ class Update
3
+ class << self
4
+ def call
5
+ headers = { Authorization: "Loco #{LocaliseRails.config.api_key}", Accept: 'application/json' }
6
+ query = LocaliseRails.config.options.map { |k, v| "#{k}=#{v}" }.join('&')
7
+ uri = "https://localise.biz/api/export/all?#{query}"
8
+
9
+ LocaliseRails.logger.info('Localise: start getting locales from localise')
10
+ response = HTTParty.get(uri, headers: headers)
11
+
12
+ if response.success?
13
+ LocaliseRails.logger.info('Localise: locales got successfully')
14
+ LocaliseRails.config.redis.set(LocaliseRails::DATA_CACHE_KEY, response.body)
15
+ LocaliseRails.config.redis.set(LocaliseRails::UPDATED_AT_CACHE_KEY, Time.current.to_i)
16
+ else
17
+ LocaliseRails.logger.error "Something went wrong downloading translations: #{response.parsed_response}"
18
+ end
19
+ rescue StandardError => e
20
+ LocaliseRails.logger.error("LocaliseRails: #{e.message}")
21
+ LocaliseRails.logger.error(e.backtrace.join("\n"))
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module LocaliseRails
2
+ VERSION = '0.1.3'.freeze
3
+ end
@@ -0,0 +1,59 @@
1
+ # requires
2
+ require 'localise_rails/version'
3
+ require 'localise_rails/logger'
4
+ require 'localise_rails/config'
5
+ require 'localise_rails/update'
6
+ require 'localise_rails/seed_translations_to_request_store'
7
+ require 'localise_rails/request_store_translations'
8
+ require 'localise_rails/fetch_localise_cache'
9
+
10
+ # gems
11
+ require 'redis'
12
+ require 'request_store'
13
+
14
+ module LocaliseRails
15
+ extend ActiveSupport::Autoload
16
+
17
+ DATA_CACHE_KEY = 'localise/data'.freeze
18
+ UPDATED_AT_CACHE_KEY = 'localise/updated_at'.freeze
19
+
20
+ def self.update
21
+ LocaliseRails::Update.call
22
+ end
23
+
24
+ def self.config
25
+ @config ||= LocaliseRails::Config.new
26
+ end
27
+
28
+ # Lets you set global configuration options.
29
+ #
30
+ # All available options and their defaults are in the example below:
31
+ # @example Initializer for Rails
32
+ # LocaliseRails.configure do |config|
33
+ # config.api_key = "LOCALISE_KEY"
34
+ #
35
+ # # all options https://localise.biz/api/docs/export/exportall
36
+ # config.options = {
37
+ # filter: 'backend',
38
+ # fallback: 'en-US'
39
+ # }
40
+ #
41
+ # config.rename_locales = {
42
+ # 'en-US' => 'en'
43
+ # }
44
+ #
45
+ # # all options https://github.com/redis/redis-rb
46
+ # # config.redis_options = {
47
+ # # host: "127.0.0.1",
48
+ # # port: 6379,
49
+ # # db: 1,
50
+ # # url: "redis://:p4ssw0rd@127.0.0.1:6379/1"
51
+ # # }
52
+ # end
53
+ def self.configure
54
+ yield(config) if block_given?
55
+ config.redis = Redis.new(config.redis_options)
56
+ end
57
+ end
58
+
59
+ require 'localise_rails/railtie' if defined?(Rails)
@@ -0,0 +1,46 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'localise_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'localise_rails'
8
+ spec.version = LocaliseRails::VERSION
9
+ spec.authors = ['Osman Baliev']
10
+ spec.email = ['reflexonly@gmail.com']
11
+
12
+ spec.summary = 'localise.biz to I18n'
13
+ spec.description = 'Easy localise your Rails app from localise.biz'
14
+ spec.homepage = 'https://github.com/OnlyReFLeX/localise_rails'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ spec.metadata['source_code_uri'] = spec.homepage
22
+ else
23
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
24
+ 'public gem pushes.'
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = 'exe'
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ['lib']
35
+
36
+ spec.add_development_dependency 'bundler', '~> 1.17'
37
+ spec.add_development_dependency 'rake', '~> 10.0'
38
+
39
+ spec.add_dependency 'activerecord'
40
+ spec.add_dependency 'i18n', '>= 1.9.0'
41
+ spec.add_dependency 'activesupport'
42
+ spec.add_dependency 'httparty'
43
+ spec.add_dependency 'railties'
44
+ spec.add_dependency 'request_store'
45
+ spec.add_dependency 'redis'
46
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: localise_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Osman Baliev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
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: i18n
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.9.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.9.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httparty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: railties
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: request_store
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: redis
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Easy localise your Rails app from localise.biz
140
+ email:
141
+ - reflexonly@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - Gemfile
148
+ - LICENSE.txt
149
+ - README.md
150
+ - Rakefile
151
+ - bin/console
152
+ - bin/setup
153
+ - lib/localise_rails.rb
154
+ - lib/localise_rails/config.rb
155
+ - lib/localise_rails/fetch_localise_cache.rb
156
+ - lib/localise_rails/logger.rb
157
+ - lib/localise_rails/railtie.rb
158
+ - lib/localise_rails/request_store_translations.rb
159
+ - lib/localise_rails/seed_translations_to_request_store.rb
160
+ - lib/localise_rails/update.rb
161
+ - lib/localise_rails/version.rb
162
+ - localise_rails.gemspec
163
+ homepage: https://github.com/OnlyReFLeX/localise_rails
164
+ licenses:
165
+ - MIT
166
+ metadata:
167
+ homepage_uri: https://github.com/OnlyReFLeX/localise_rails
168
+ source_code_uri: https://github.com/OnlyReFLeX/localise_rails
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubygems_version: 3.0.9
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: localise.biz to I18n
188
+ test_files: []