i18n_backend_mongoid 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +1 -0
- data/LICENSE.txt +21 -0
- data/README.md +46 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/i18n_backend_mongoid.gemspec +28 -0
- data/lib/generators/i18n_backend_mongoid/install_generator.rb +28 -0
- data/lib/generators/templates/locale.rb.erb +1 -0
- data/lib/generators/templates/translation.rb.erb +6 -0
- data/lib/generators/utils.rb +77 -0
- data/lib/i18n_backend_mongoid.rb +13 -0
- data/lib/i18n_backend_mongoid/i18n/backend/mongoid.rb +91 -0
- data/lib/i18n_backend_mongoid/translateble.rb +15 -0
- data/lib/i18n_backend_mongoid/version.rb +3 -0
- data/lib/tasks/i18n_backend_mongoid.rake +11 -0
- data/spec/i18n_backend_mongoid_spec.rb +7 -0
- data/spec/spec_helper.rb +4 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 93fea5a9ab62b8f821275ecea2b4f56f95fc6eb0
|
4
|
+
data.tar.gz: 0f87be98b29aa663a0faedce12fd093a4131edb1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3bf1cc45cddf867c0446dfdd6e4b0d13f22cb36b94a844a52eefa4c3589b8c4a3c400928434f40f2786002118344094ee0a06eb45ede784dfe97ebb7087918cb
|
7
|
+
data.tar.gz: b489b454f8b59b4d990b5414bc3e64caec84fd9cc9e93c06600589e329ea72b2f00c87137c770bfbb3e22a91e686c987846bfc8b1c0a0486fbf66264504e1750
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
source 'https://rubygems.org'
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Alexandr Turchyn, Victor Rubych
|
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,46 @@
|
|
1
|
+
# i18n_backend_mongoid
|
2
|
+
|
3
|
+
Store I18n translations in MongoDB
|
4
|
+
|
5
|
+
## Dependency
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'rails-i18n'
|
9
|
+
gem 'mongoid'
|
10
|
+
```
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'i18n_backend_mongoid'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install i18n_backend_mongoid
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
rails g i18n_backend_mongoid:install TRANSLATION_TABLE_NAME
|
31
|
+
|
32
|
+
## Uninstall
|
33
|
+
|
34
|
+
rails d i18n_backend_mongoid:install TRANSLATION_TABLE_NAME
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it ( https://github.com/webmil/i18n_backend_mongoid/fork )
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create a new Pull Request
|
43
|
+
|
44
|
+
## License
|
45
|
+
|
46
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'i18n_backend_mongoid'
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'i18n_backend_mongoid/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'i18n_backend_mongoid'
|
8
|
+
spec.version = I18nBackendMongoid::VERSION
|
9
|
+
spec.authors = ['Alexandr Turchyn', 'Victor Rubych']
|
10
|
+
spec.email = ['lexfox777@gmail.com', 'rubych@webmil.eu']
|
11
|
+
|
12
|
+
spec.summary = 'United I18n backend simple with mongoid provider.'
|
13
|
+
spec.description = 'United I18n backend simple with mongoid provider'
|
14
|
+
spec.homepage = 'https://github.com/webmil/i18n_backend_mongoid'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = %w(lib)
|
20
|
+
|
21
|
+
spec.add_development_dependency 'rspec', '~> 3.4.0'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
|
24
|
+
spec.add_dependency 'bundler', '~> 1.7'
|
25
|
+
# spec.add_dependency 'rails_admin', '~> 1.0'
|
26
|
+
spec.add_dependency 'mongoid'
|
27
|
+
spec.add_dependency 'rails-i18n'
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
require 'generators/utils'
|
3
|
+
|
4
|
+
module I18nBackendMongoid
|
5
|
+
module Generators
|
6
|
+
class InstallGenerator < Rails::Generators::Base
|
7
|
+
include I18nBackendMongoid::Generators::Utils::InstanceMethods
|
8
|
+
source_root File.expand_path('../../templates', __FILE__)
|
9
|
+
|
10
|
+
argument :translation_table_name, type: :string
|
11
|
+
|
12
|
+
def copy_initializer
|
13
|
+
if libraries_available?('mongoid', 'rails-i18n')
|
14
|
+
template 'translation.rb.erb', "app/models/#{table_name.downcase}.rb"
|
15
|
+
template 'locale.rb.erb', 'config/initializers/locale.rb'
|
16
|
+
else
|
17
|
+
say('Mongoid or rails-i18n aren\'t installed!', :yellow)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def table_name
|
24
|
+
translation_table_name.downcase.capitalize
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Mongoid.new(<%= table_name %>), I18n::Backend::Simple.new)
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'tempfile'
|
4
|
+
include FileUtils
|
5
|
+
|
6
|
+
module I18nBackendMongoid
|
7
|
+
module Generators
|
8
|
+
module Utils
|
9
|
+
module InstanceMethods
|
10
|
+
@@gemfile_path = "#{Rails.root}/Gemfile"
|
11
|
+
|
12
|
+
def yes_no(question)
|
13
|
+
is_valid = true
|
14
|
+
question += ' [Y/N] '
|
15
|
+
while is_valid
|
16
|
+
answer = ask(question, :yellow) do |yn|
|
17
|
+
yn.limit = 1, yn.validate = /[yn]/i
|
18
|
+
end
|
19
|
+
answer.downcase!
|
20
|
+
is_valid = (answer == 'y' || answer == 'n') ? false : true
|
21
|
+
end
|
22
|
+
answer
|
23
|
+
end
|
24
|
+
|
25
|
+
def library_available?(gem_name)
|
26
|
+
require gem_name
|
27
|
+
return true
|
28
|
+
rescue LoadError
|
29
|
+
return false
|
30
|
+
end
|
31
|
+
|
32
|
+
def libraries_available?(*gems)
|
33
|
+
is_available = true
|
34
|
+
gems.each do |gem|
|
35
|
+
return false unless library_available?(gem)
|
36
|
+
end
|
37
|
+
is_available
|
38
|
+
end
|
39
|
+
|
40
|
+
def file?(path)
|
41
|
+
File.exist?(path)
|
42
|
+
end
|
43
|
+
|
44
|
+
def install_gem(_gem_name, version = nil)
|
45
|
+
gem = "gem '" + gem_configurename + "'"
|
46
|
+
gem += (", '~> " + version + "'") if version
|
47
|
+
|
48
|
+
unless file_include?(@@gemfile_path, gem)
|
49
|
+
open(@@gemfile_path, 'a') { |f| f.puts gem }
|
50
|
+
end
|
51
|
+
|
52
|
+
system 'bundle install'
|
53
|
+
end
|
54
|
+
|
55
|
+
def replace(path, pattern, new_line)
|
56
|
+
t_file = Tempfile.new('temp.rb')
|
57
|
+
File.open(path, 'r') do |f|
|
58
|
+
f.each_line do |line|
|
59
|
+
t_file.puts(line.include?(pattern)) ? new_line : line
|
60
|
+
end
|
61
|
+
end
|
62
|
+
t_file.close
|
63
|
+
FileUtils.mv(t_file.path, path)
|
64
|
+
end
|
65
|
+
|
66
|
+
def file_include?(path, include)
|
67
|
+
File.open(path, 'r') do |f|
|
68
|
+
f.each_line do |line|
|
69
|
+
return true if line.include? include
|
70
|
+
end
|
71
|
+
end
|
72
|
+
false
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'i18n/backend/base'
|
2
|
+
|
3
|
+
module I18n
|
4
|
+
module Backend
|
5
|
+
class Mongoid
|
6
|
+
module Implementation
|
7
|
+
attr_accessor :model
|
8
|
+
include Base, Flatten
|
9
|
+
|
10
|
+
def initialize(model)
|
11
|
+
@model = model
|
12
|
+
init_translations
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialized?
|
16
|
+
@initialized ||= false
|
17
|
+
end
|
18
|
+
|
19
|
+
# Stores translations for the given locale in memory.
|
20
|
+
# This uses a deep merge for the translations hash, so existing
|
21
|
+
# translations will be overwritten by new ones only at the deepest
|
22
|
+
# level of the hash.
|
23
|
+
def store_translations(locale, data, options = {})
|
24
|
+
locale = locale.to_sym
|
25
|
+
translations[locale] ||= {}
|
26
|
+
data = data.deep_symbolize_keys
|
27
|
+
translations[locale].deep_merge!(data)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Get available locales from the translations hash
|
31
|
+
def available_locales
|
32
|
+
init_translations unless initialized?
|
33
|
+
translations.inject([]) do |locales, (locale, data)|
|
34
|
+
locales << locale unless (data.keys - [:i18n]).empty?
|
35
|
+
locales
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Clean up translations hash and set initialized to false on reload!
|
40
|
+
def reload!
|
41
|
+
@initialized = false
|
42
|
+
@translations = nil
|
43
|
+
init_translations
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
def init_translations
|
49
|
+
load_translations
|
50
|
+
@initialized = true
|
51
|
+
end
|
52
|
+
|
53
|
+
def load_translations
|
54
|
+
@translations = {}
|
55
|
+
@model.all.each do |record|
|
56
|
+
I18n.available_locales.each do |loc|
|
57
|
+
next unless record.value_translations.key?(loc)
|
58
|
+
k = I18n.normalize_keys(loc, record.key, [])
|
59
|
+
k.shift
|
60
|
+
store_translations(loc, k.reverse.inject(record.value_translations[loc]) { |a, n| { n => a } })
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def translations
|
66
|
+
@translations ||= {}
|
67
|
+
end
|
68
|
+
|
69
|
+
# Looks up a translation from the translations hash. Returns nil if
|
70
|
+
# eiher key is nil, or locale, scope or key do not exist as a key in the
|
71
|
+
# nested translations hash. Splits keys or scopes containing dots
|
72
|
+
# into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
|
73
|
+
# <tt>%w(currency format)</tt>.
|
74
|
+
def lookup(locale, key, scope = [], options = {})
|
75
|
+
init_translations unless initialized?
|
76
|
+
keys = I18n.normalize_keys(locale, key, scope, options[:separator])
|
77
|
+
|
78
|
+
keys.inject(translations) do |result, k|
|
79
|
+
k = k.to_sym
|
80
|
+
return nil unless result.is_a?(Hash) && result.key?(k)
|
81
|
+
result = result.is_a?(Symbol) ? resolve(locale, k, result, options.merge(scope: nil)) : result[k]
|
82
|
+
|
83
|
+
result
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
include Implementation
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module I18nBackendMongoid
|
2
|
+
module Translateble
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
included do
|
5
|
+
after_save do
|
6
|
+
I18n.backend.reload!
|
7
|
+
end
|
8
|
+
|
9
|
+
field :key, type: String
|
10
|
+
field :value, type: String, localize: true
|
11
|
+
|
12
|
+
validates :key, presence: true, uniqueness: true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
namespace :i18n_backend_mongoid do
|
2
|
+
desc 'Install i18n_backend_mongoid'
|
3
|
+
task :install do
|
4
|
+
system 'rails g i18n_backend_mongoid:install'
|
5
|
+
end
|
6
|
+
|
7
|
+
desc 'Uninstall i18n_backend_mongoid'
|
8
|
+
task :uninstall do
|
9
|
+
system 'rails d i18n_backend_mongoid:install'
|
10
|
+
end
|
11
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: i18n_backend_mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexandr Turchyn
|
8
|
+
- Victor Rubych
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-12-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 3.4.0
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 3.4.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: bundler
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '1.7'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1.7'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: mongoid
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rails-i18n
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :runtime
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
description: United I18n backend simple with mongoid provider
|
85
|
+
email:
|
86
|
+
- lexfox777@gmail.com
|
87
|
+
- rubych@webmil.eu
|
88
|
+
executables:
|
89
|
+
- console
|
90
|
+
- setup
|
91
|
+
extensions: []
|
92
|
+
extra_rdoc_files: []
|
93
|
+
files:
|
94
|
+
- ".gitignore"
|
95
|
+
- ".rspec"
|
96
|
+
- ".travis.yml"
|
97
|
+
- Gemfile
|
98
|
+
- LICENSE.txt
|
99
|
+
- README.md
|
100
|
+
- Rakefile
|
101
|
+
- bin/console
|
102
|
+
- bin/setup
|
103
|
+
- i18n_backend_mongoid.gemspec
|
104
|
+
- lib/generators/i18n_backend_mongoid/install_generator.rb
|
105
|
+
- lib/generators/templates/locale.rb.erb
|
106
|
+
- lib/generators/templates/translation.rb.erb
|
107
|
+
- lib/generators/utils.rb
|
108
|
+
- lib/i18n_backend_mongoid.rb
|
109
|
+
- lib/i18n_backend_mongoid/i18n/backend/mongoid.rb
|
110
|
+
- lib/i18n_backend_mongoid/translateble.rb
|
111
|
+
- lib/i18n_backend_mongoid/version.rb
|
112
|
+
- lib/tasks/i18n_backend_mongoid.rake
|
113
|
+
- spec/i18n_backend_mongoid_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
homepage: https://github.com/webmil/i18n_backend_mongoid
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata: {}
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 2.5.1
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: United I18n backend simple with mongoid provider.
|
139
|
+
test_files: []
|
140
|
+
has_rdoc:
|