moped-i18n 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +5 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/lib/i18n/backend/moped.rb +50 -0
- data/lib/moped/i18n.rb +2 -0
- data/lib/moped/i18n/version.rb +5 -0
- data/moped-i18n.gemspec +24 -0
- data/test/api_test.rb +49 -0
- data/test/test_helper.rb +62 -0
- metadata +108 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Josef Šimánek
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Moped::I18n [![Build Status](https://travis-ci.org/simi/moped-i18n.png?branch=master)](https://travis-ci.org/simi/moped-i18n)
|
2
|
+
|
3
|
+
Simple Moped (Mongoid) i18n flattened basic backend implementation.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'moped-i18n'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install moped-i18n
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: soon
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'i18n/backend/base'
|
2
|
+
|
3
|
+
module I18n
|
4
|
+
module Backend
|
5
|
+
class Moped
|
6
|
+
module Implementation
|
7
|
+
attr_accessor :collection
|
8
|
+
include Base, Flatten
|
9
|
+
|
10
|
+
def initialize(collection, options={})
|
11
|
+
@collection, @options = collection, options
|
12
|
+
end
|
13
|
+
|
14
|
+
def available_locales
|
15
|
+
collection.find.distinct(:locale).map {|l| l.to_sym}
|
16
|
+
end
|
17
|
+
|
18
|
+
def store_translations(locale, data, options = {})
|
19
|
+
escape = options.fetch(:escape, true)
|
20
|
+
flatten_translations(locale, data, escape, false).each do |key, value|
|
21
|
+
collection.find(:key => key.to_s, :locale => locale.to_s).remove_all
|
22
|
+
doc = {:key => key.to_s, :value => value, :locale => locale.to_s}
|
23
|
+
collection.insert(doc)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
def lookup(locale, key, scope = [], options = {})
|
29
|
+
key = normalize_flat_keys(locale, key, scope, options[:separator])
|
30
|
+
result = collection.find(:locale => locale.to_s, :key => /^#{Regexp.quote(key.to_s)}(\.|$)/).entries
|
31
|
+
|
32
|
+
if result.empty?
|
33
|
+
nil
|
34
|
+
elsif result.first["key"] == key.to_s
|
35
|
+
result.first["value"]
|
36
|
+
else
|
37
|
+
chop_range = (key.size + FLATTEN_SEPARATOR.size)..-1
|
38
|
+
result = result.inject({}) do |hash, r|
|
39
|
+
hash[r["key"].slice(chop_range)] = r["value"]
|
40
|
+
hash
|
41
|
+
end
|
42
|
+
result.deep_symbolize_keys
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
include Implementation
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/moped/i18n.rb
ADDED
data/moped-i18n.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'moped/i18n/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "moped-i18n"
|
8
|
+
spec.version = Moped::I18n::VERSION
|
9
|
+
spec.authors = ["Josef Šimánek"]
|
10
|
+
spec.email = ["retro@ballgag.cz"]
|
11
|
+
spec.description = %q{I18n backend for moped}
|
12
|
+
spec.summary = %q{Store your dynamic translations in moped}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "i18n"
|
22
|
+
spec.add_dependency "moped"
|
23
|
+
spec.add_dependency "activesupport"
|
24
|
+
end
|
data/test/api_test.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class I18nMopedApiTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
I18n.backend = I18n::Backend::Moped.new(COLLECTION)
|
6
|
+
super
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.can_store_procs?
|
10
|
+
false
|
11
|
+
end
|
12
|
+
|
13
|
+
include I18n::Tests::Basics
|
14
|
+
include I18n::Tests::Defaults
|
15
|
+
include I18n::Tests::Interpolation
|
16
|
+
include I18n::Tests::Link
|
17
|
+
include I18n::Tests::Lookup
|
18
|
+
include I18n::Tests::Pluralization
|
19
|
+
include I18n::Tests::Procs if can_store_procs?
|
20
|
+
|
21
|
+
include I18n::Tests::Localization::Date
|
22
|
+
include I18n::Tests::Localization::DateTime
|
23
|
+
include I18n::Tests::Localization::Time
|
24
|
+
include I18n::Tests::Localization::Procs if can_store_procs?
|
25
|
+
|
26
|
+
test "make sure we use an Moped backend" do
|
27
|
+
assert_equal I18n::Backend::Moped, I18n.backend.class
|
28
|
+
end
|
29
|
+
|
30
|
+
test "return only on full hash match" do
|
31
|
+
I18n.backend.store_translations(:en, :short_text => 'ABC you know ME')
|
32
|
+
assert_raises(I18n::MissingTranslationData) {I18n.translate!(:sh)}
|
33
|
+
end
|
34
|
+
|
35
|
+
test "changing current locale" do
|
36
|
+
I18n.backend.store_translations(:en, :text => 'Hi')
|
37
|
+
I18n.backend.store_translations(:cs, :text => 'Zdary')
|
38
|
+
|
39
|
+
original = I18n.locale
|
40
|
+
|
41
|
+
I18n.locale = :en
|
42
|
+
assert_equal 'Hi', I18n.t(:text)
|
43
|
+
|
44
|
+
I18n.locale = :cs
|
45
|
+
assert_equal 'Zdary', I18n.t(:text)
|
46
|
+
|
47
|
+
I18n.locale = original
|
48
|
+
end
|
49
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
|
2
|
+
# Do not load the i18n gem from libraries like active_support.
|
3
|
+
#
|
4
|
+
# This is required for testing against Rails 2.3 because active_support/vendor.rb#24 tries
|
5
|
+
# to load I18n using the gem method. Instead, we want to test the local library of course.
|
6
|
+
require 'bundler/setup'
|
7
|
+
require 'i18n'
|
8
|
+
require 'moped/i18n'
|
9
|
+
require 'test_declarative'
|
10
|
+
require 'test/unit'
|
11
|
+
require 'mocha/setup'
|
12
|
+
|
13
|
+
|
14
|
+
def database_id
|
15
|
+
"moped_i18n_test"
|
16
|
+
end
|
17
|
+
|
18
|
+
@@session = Moped::Session.new(["127.0.0.1:27017"])
|
19
|
+
@@session.use(database_id)
|
20
|
+
|
21
|
+
COLLECTION = @@session["i18n"]
|
22
|
+
|
23
|
+
class Test::Unit::TestCase
|
24
|
+
|
25
|
+
# TODO poor's man moped DB cleaner
|
26
|
+
def truncate_test_db
|
27
|
+
collections = @@session['system.namespaces'].find(:name => { '$not' => /system|\$/ }).to_a.map do |collection|
|
28
|
+
_, name = collection['name'].split('.', 2)
|
29
|
+
name
|
30
|
+
end
|
31
|
+
collections.each { |c| @@session[c].find.remove_all }
|
32
|
+
end
|
33
|
+
|
34
|
+
def setup
|
35
|
+
truncate_test_db
|
36
|
+
end
|
37
|
+
|
38
|
+
def teardown
|
39
|
+
I18n.locale = nil
|
40
|
+
I18n.default_locale = :en
|
41
|
+
I18n.load_path = []
|
42
|
+
I18n.available_locales = nil
|
43
|
+
I18n.backend = nil
|
44
|
+
truncate_test_db
|
45
|
+
end
|
46
|
+
|
47
|
+
def translations
|
48
|
+
I18n.backend.instance_variable_get(:@translations)
|
49
|
+
end
|
50
|
+
|
51
|
+
def store_translations(*args)
|
52
|
+
data = args.pop
|
53
|
+
locale = args.pop || :en
|
54
|
+
I18n.backend.store_translations(locale, data)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
Object.class_eval do
|
59
|
+
def meta_class
|
60
|
+
class << self; self; end
|
61
|
+
end
|
62
|
+
end unless Object.method_defined?(:meta_class)
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: moped-i18n
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Josef Šimánek
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: i18n
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: moped
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activesupport
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: I18n backend for moped
|
63
|
+
email:
|
64
|
+
- retro@ballgag.cz
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .travis.yml
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/i18n/backend/moped.rb
|
76
|
+
- lib/moped/i18n.rb
|
77
|
+
- lib/moped/i18n/version.rb
|
78
|
+
- moped-i18n.gemspec
|
79
|
+
- test/api_test.rb
|
80
|
+
- test/test_helper.rb
|
81
|
+
homepage: ''
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.8.24
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: Store your dynamic translations in moped
|
106
|
+
test_files:
|
107
|
+
- test/api_test.rb
|
108
|
+
- test/test_helper.rb
|